Install standard repository package.
$ sudo aptitude install mongodb
Create data directory. By default mongo uses /data/db
.
$ sudo mkdir -p /data/db/
$ sudo chown `id -u` /data/db
Try to run the server. (If you decided to use another path for data run mongod
with --dbpath
option)
$ mongod
If it runs, the you are ok. But if it throws an error
mongo: error while loading shared libraries: libmozjs.so: cannot open shared object file: No such file or directory
Then we will have to find that missing library.
$ find /usr/lib/* -name libmozjs.so
/usr/lib/firefox-3.6.8/libmozjs.so
/usr/lib/xulrunner-1.9.2.8/libmozjs.so
/usr/lib/xulrunner-devel-1.9.2.8/sdk/lib/libmozjs.so
Now create a symbolic link
$ cd /usr/lib
$ sudo ln -s xulrunner-devel-1.9.2.8/sdk/lib/libmozjs.so libmozjs.so
Now try to start the server again. It should be working. Start the client and run some queries:
$ mongo
> db.foo.save( { a : 1 } )
> db.foo.find()
Installing Libraries For C++
So the database is up and running, now we need some C++ libs to be able to connect to it.
$ sudo aptitude install libboost-all-dev libpcre++-dev
Now we should be able to run this code, to test the connection.
//tutorial.cpp
#include <iostream>
#include <mongo/client/dbclient.h>
using namespace std;
void run() {
mongo::DBClientConnection c;
c.connect("localhost");
}
int main() {
try {
run();
cout << "connected ok" << endl;
} catch( mongo::DBException &e ) {
cout << "caught " << e.what() << endl;
}
return 0;
}
Build and run this code using:
$ g++ tutorial.cpp -lmongoclient -lboost_thread-mt -lboost_filesystem-mt -lboost_program_options-mt tutorial
$ ./tutorial
connected ok
Hi Juri,
ReplyDeleteThat's a good starter tutorial. Waiting for another application example for C++ driver.
Thanks and cheers,
C++ driver user's ;p
my pc doesnt find the folder mongo/client/dbclient.h, am I missing something??
ReplyDeleteTry installing mongodb-dev package.
Delete