当前位置: 动力学知识库 > 问答 > 编程问答 >

MongoDB C++ Legacy driver - how to connect using UNIX domain socket

问题描述:

I tried to connect using UNIX domain socket

mongo::DBClientConnection connection(true);

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

And I had an exception:

Bad digit "/" while parsing ///tmp/mongodb-27017.sock

Maybe someone knows how to do it using C++ driver?

UPDATE

This is exactly my code

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;

}

I have tried version 1.0.6 and 1.0.5, my output

couldn't connect : couldn't connect to server

/tmp/mongodb-27017.sock:27017 (/tmp/mongodb-27017.sock), connection

attempt failed

I built the driver like:

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

My working 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

I downloaded source code from this page https://github.com/mongodb/mongo-cxx-driver/releases/tag/legacy-1.0.5

网友答案:

That isn't the right way to create a connection. Use the ConnectionString::parse method to make a ConnectionString object, then call connect on it:

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

Here is a transcript of building and using the driver to connect over a UNIX domain socket:

> 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
分享给朋友:
您可能感兴趣的文章:
随机阅读: