2

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

3
  • In the future, when posting code, please post something that can be compiled with just a copy paste: int main(), headers, etc. Commented Oct 20, 2015 at 16:41
  • 1
    I ran your program. If I run it with mongod started, it connects fine. If I run it without mongod started, it gives the error above. I believe the issue is that the error message misleading. You should look into why it can't connect to your mongod. Did you actually start mongod in a configuration where it listens on the unix domain socket? You can check with lsof -p $(pgrep mongod) | grep unix Commented Oct 20, 2015 at 16:46
  • Thank you! I have found a root of evil - the file /tmp/mongodb-27017.sock has no permissions to read/write for my Linux user. I have changed it using 'chmod' and connection is successfully established. Commented Oct 20, 2015 at 17:23

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, but now I have next error during establish a connection couldn't connect to server /tmp/mongodb-27017.sock:27017 (/tmp/mongodb-27017.sock), connection attempt failed It looks like C++ driver does not support UNIX domain sockets, because it does not know that the default port it unnecessary.
@Kastaneda What version of the C++ driver are you using?
@Kastaneda That version hasn't yet been released, though we are planning too sometime this week. Can you post a new code example showing exactly what you are doing? I just tested locally and connected just fine to a UNIX domain socket with a mongodb::// URI.
Possible I was wrong about version. I have looked into /include/mongo/version.h and found this line const char kVersionString[] = "1.0.6-rc0-pre"; I will post my code soon.
@Kastaneda - I see. You should really check out a released tag (or download one of the release tarballs from the github project releases page) when building the driver. The HEAD of legacy may be unstable as we work on fixes for the next release.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.