MongoDB C ++旧版驱动程序 - 如何使用UNIX域套接字进行连接

时间:2015-10-17 10:17:53

标签: c++ mongodb

我尝试使用UNIX域套接字连接

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

我有一个例外:

  

坏数字" /"在解析/// tmp / mongodb-27017.sock

也许有人知道如何使用C ++驱动程序进行操作?

更新

这正是我的代码

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版本,我的输出

  

无法连接:无法连接到服务器   /tmp/mongodb-27017.sock:27017(/tmp/mongodb-27017.sock),连接   尝试失败

我构建了驱动程序,如:

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

我的工作环境:

$ 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

下载了源代码

1 个答案:

答案 0 :(得分:1)

这不是创建连接的正确方法。使用ConnectionString :: parse方法创建一个ConnectionString对象,然后在其上调用connect:

请参阅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