2015-10-17 26 views
2

我嘗試使用UNIX域套接字MongoDB的C++舊版驅動程序 - 如何使用UNIX域套接字

mongo::DBClientConnection connection(true); 
connection.connect("mongodb:///tmp/mongodb-27017.sock"); 

連接來連接和我有一個例外:

壞位數 「/」,而解析/ //tmp/mongodb-27017.sock

也許有人知道如何使用C++驅動程序?

UPDATE

這也正是我的代碼

std::string errmsg; 
mongo::ConnectionString cs = mongo::ConnectionString::parse("mongodb:///tmp/mongodb-27017.sock", errmsg); 

if (!cs.isValid()) { 
    std::cout << "Error parsing connection string " << uri << ": " << errmsg << std::endl; 
    return; 
} 

std::shared_ptr<mongo::DBClientBase> conn(cs.connect(errmsg)); 
if (!conn) { 
    std::cout << "couldn't connect : " << errmsg << std::endl; 
    return; 
} 

我已經試過版本1.0.6和1.0.5,我的輸出

無法連接:couldn」 t連接到服務器 /tmp/mongodb-27017.sock:27017(/tmp/mongodb-27017.sock),連接 嘗試失敗

我建的司機,如:

$scons --prefix=$HOME/libs/mongo install 

我的工作enveronment:

$ uname -a 
Linux roman-nout 3.19.0-30-generiC#34-Ubuntu SMP Fri Oct 2 22:08:41 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux 
$ g++ -v 
Using built-in specs. 
COLLECT_GCC=g++ 
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.9/lto-wrapper 
Target: x86_64-linux-gnu 
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.9.2-10ubuntu13' --with-bugurl=file:///usr/share/doc/gcc-4.9/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.9 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.9 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.9-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu 
Thread model: posix 
gcc version 4.9.2 (Ubuntu 4.9.2-10ubuntu13) 
$ ls /tmp/mongodb-27017.sock 
/tmp/mongodb-27017.sock 

我下載的源代碼從該頁面https://github.com/mongodb/mongo-cxx-driver/releases/tag/legacy-1.0.5

+0

未來,在發佈代碼時,請發佈一些可以用複製粘貼進行編譯的東西:int main(),headers等。 – acm

+1

我運行了你的程序。如果我用mongod啓動它,它連接正常。如果我在沒有啓動mongod的情況下運行它,它會給出上述錯誤。我相信問題在於錯誤消息有誤導性。你應該看看爲什麼它不能連接到你的mongod。您是否真的在配置中偵聽unix域套接字的配置中啓動了mongod?您可以使用lsof -p $(pgrep mongod)|進行檢查grep unix – acm

+0

謝謝!我找到了一個邪惡根源 - 文件/tmp/mongodb-27017.sock沒有權限爲我的Linux用戶讀/寫。我已經使用'chmod'對其進行了更改,並且連接已成功建立。 – Kastaneda

回答

1

這不是正確的方式創建一個連接。使用ConnectionString :: parse方法做出的ConnectionString對象,然後調用連接就可以了:

https://github.com/mongodb/mongo-cxx-driver/blob/legacy/src/mongo/client/examples/insert_demo.cpp#L42-L52

這裏是建築的成績單和使用驅動程序中的UNIX域套接字進行連接

> git checkout legacy 
Already on 'legacy' 
Your branch is up-to-date with 'origin/legacy'. 

> scons --cc=/usr/bin/clang --cxx=/usr/bin/clang++ --cache --dbg=on --sharedclient --ssl --use-sasl-client --extrapath=/usr/local -j10 check-install all install-examples 
... 
scons: `all' is up to date. 
scons: `install-examples' is up to date. 
scons: done building targets. 

> build/install/share/mongo-cxx-driver/examples/insertDemo mongodb:///tmp/mongodb-27017.sock 
dropping collection... 
inserting... 
getlasterror returns: "" 
9 seconds 11111 per second 
+0

謝謝,但現在我在建立連接期間出現了下一個錯誤**無法連接到服務器/tmp/mongodb-27017.sock:27017(/tmp/mongodb-27017.sock),連接嘗試失敗**它看起來像C++驅動程序不支持UNIX域套接字,因爲它不知道默認端口是不必要的。 – Kastaneda

+0

@Kastaneda你正在使用什麼版本的C++驅動程序? – acm

+0

我使用Legacy-1.0.6 – Kastaneda