Table of contents
Preamble
A fashion word for this post: NoSQL ! MongoDB is coming in one of our projects and I wanted to test it to see if it is really as simple as they say. Personally I would have chosen HBase (sub project of Hadoop) but whatever, let’s see if MongoDB is simple…
MongoDB installation
Create mongo user database owner and default data directory (/data/db, can be changed when issuing mongod daemon with –dbpath):
[root@server1 ~]# grep mong /etc/passwd mongodb:x:502:501::/home/mongodb:/bin/bash [root@server1 ~]# mkdir -p /data/db [root@server1 ~]# chown -R mongodb:dba /data |
Installation of binaries is as simple as:
[root@server1 ~]# su - mongodb [mongodb@server1 ~]$ id uid=502(mongodb) gid=501(dba) groups=501(dba) [mongodb@server1 tmp]$ ll /tmp/mongo* -rw-r--r-- 1 mongodb dba 38156469 Mar 8 14:42 mongodb-linux-x86_64-2.0.3.tar.gz [mongodb@server1 tmp]$ gunzip /tmp/mongodb-linux-x86_64-2.0.3.tgz [mongodb@server1 tmp]$ tar xvf /tmp/mongodb-linux-x86_64-2.0.3.tar mongodb-linux-x86_64-2.0.3/ mongodb-linux-x86_64-2.0.3/THIRD-PARTY-NOTICES mongodb-linux-x86_64-2.0.3/GNU-AGPL-3.0 mongodb-linux-x86_64-2.0.3/README mongodb-linux-x86_64-2.0.3/bin/ mongodb-linux-x86_64-2.0.3/bin/mongo mongodb-linux-x86_64-2.0.3/bin/mongotop mongodb-linux-x86_64-2.0.3/bin/mongos mongodb-linux-x86_64-2.0.3/bin/mongodump mongodb-linux-x86_64-2.0.3/bin/mongostat mongodb-linux-x86_64-2.0.3/bin/mongofiles mongodb-linux-x86_64-2.0.3/bin/mongoimport mongodb-linux-x86_64-2.0.3/bin/mongosniff mongodb-linux-x86_64-2.0.3/bin/mongorestore mongodb-linux-x86_64-2.0.3/bin/mongod mongodb-linux-x86_64-2.0.3/bin/bsondump mongodb-linux-x86_64-2.0.3/bin/mongoexport |
First time start and creation of mongoDB tree:
[mongodb@server1 bin]$ /home/mongodb/mongodb-linux-x86_64-2.0.3/bin/mongod /home/mongodb/mongodb-linux-x86_64-2.0.3/bin/mongod --help for help and startup options Thu Mar 8 14:46:02 [initandlisten] MongoDB starting : pid=4340 port=27017 dbpath=/data/db/ 64-bit host=server1.domain.com Thu Mar 8 14:46:02 [initandlisten] db version v2.0.3, pdfile version 4.5 Thu Mar 8 14:46:02 [initandlisten] git version: 05bb8aa793660af8fce7e36b510ad48c27439697 Thu Mar 8 14:46:02 [initandlisten] build info: Linux ip-10-110-9-236 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_41 Thu Mar 8 14:46:02 [initandlisten] options: {} Thu Mar 8 14:46:02 [initandlisten] journal dir=/data/db/journal Thu Mar 8 14:46:02 [initandlisten] recover : no journal files present, no recovery needed Thu Mar 8 14:46:03 [initandlisten] preallocateIsFaster=true 8.54 Thu Mar 8 14:46:04 [initandlisten] preallocateIsFaster=true 8.42 Thu Mar 8 14:46:05 [initandlisten] preallocateIsFaster=true 6.46 Thu Mar 8 14:46:05 [initandlisten] preallocating a journal file /data/db/journal/prealloc.0 115343360/1073741824 10% 188743680/1073741824 17% 356515840/1073741824 33% 471859200/1073741824 43% 660602880/1073741824 61% 838860800/1073741824 78% 996147200/1073741824 92% Thu Mar 8 14:46:28 [initandlisten] preallocating a journal file /data/db/journal/prealloc.1 157286400/1073741824 14% 377487360/1073741824 35% 534773760/1073741824 49% 618659840/1073741824 57% 838860800/1073741824 78% 985661440/1073741824 91% Thu Mar 8 14:46:48 [initandlisten] preallocating a journal file /data/db/journal/prealloc.2 209715200/1073741824 19% 367001600/1073741824 34% 513802240/1073741824 47% 618659840/1073741824 57% 859832320/1073741824 80% 1027604480/1073741824 95% Thu Mar 8 14:47:10 [initandlisten] waiting for connections on port 27017 Thu Mar 8 14:47:10 [websvr] admin web console waiting for connections on port 28017 Thu Mar 8 14:48:10 [clientcursormon] mem (MB) res:15 virt:612 mapped:0 Thu Mar 8 14:48:42 [initandlisten] connection accepted from 127.0.0.1:41011 #1 Thu Mar 8 14:48:48 [FileAllocator] allocating new datafile /data/db/test.ns, filling with zeroes... Thu Mar 8 14:48:48 [FileAllocator] creating directory /data/db/_tmp Thu Mar 8 14:48:48 [FileAllocator] done allocating datafile /data/db/test.ns, size: 16MB, took 0.002 secs Thu Mar 8 14:48:48 [FileAllocator] allocating new datafile /data/db/test.0, filling with zeroes... Thu Mar 8 14:48:48 [FileAllocator] done allocating datafile /data/db/test.0, size: 64MB, took 0.002 secs Thu Mar 8 14:48:48 [FileAllocator] allocating new datafile /data/db/test.1, filling with zeroes... Thu Mar 8 14:48:48 [conn1] build index test.foo { _id: 1 } Thu Mar 8 14:48:48 [FileAllocator] done allocating datafile /data/db/test.1, size: 128MB, took 0.003 secs Thu Mar 8 14:48:48 [conn1] build index done 0 records 0 secs Thu Mar 8 14:49:10 [clientcursormon] mem (MB) res:32 virt:805 mapped:80 Thu Mar 8 14:54:10 [clientcursormon] mem (MB) res:32 virt:805 mapped:80 Thu Mar 8 14:58:01 [conn1] build index test.things { _id: 1 } Thu Mar 8 14:58:01 [conn1] build index done 0 records 0 secs Thu Mar 8 14:59:10 [clientcursormon] mem (MB) res:32 virt:805 mapped:80 Thu Mar 8 15:04:10 [clientcursormon] mem (MB) res:32 virt:805 mapped:80 Thu Mar 8 15:09:10 [clientcursormon] mem (MB) res:32 virt:805 mapped:80 Thu Mar 8 15:14:10 [clientcursormon] mem (MB) res:32 virt:805 mapped:80 . . . |
And that’s it ! You are done ! You can anyway confirm all is fine on default status page (I do not understand why they call it an admin web console), –rest option needed to access details:
Usage
In another session issue mongo client and insert records in two different collections (tables):
[mongodb@server1 ~]$ export PATH=/home/mongodb/mongodb-linux-x86_64-2.0.3/bin:$PATH [mongodb@server1 ~]$ mongo MongoDB shell version: 2.0.3 connecting to: test > db.foo.save( { a : 1 } ) > db.foo.find() { "_id" : ObjectId("4f58b8c0488b0b5ce947464b"), "a" : 1 } > db.things.save({b:2}) > db.things.find() { "_id" : ObjectId("4f58bae9488b0b5ce947464c"), "b" : 2 } > db.foo.find() { "_id" : ObjectId("4f58b8c0488b0b5ce947464b"), "a" : 1 } > exit bye |
If coming from standard RDBMS world you may want to have things more secure, let’s create a global account (admin database):
[mongodb@server1 ~]$ mongo MongoDB shell version: 2.0.3 connecting to: test > use admin switched to db admin > db.addUser("yjaquier","pasword") { "n" : 0, "connectionId" : 3, "err" : null, "ok" : 1 } { "user" : "yjaquier", "readOnly" : false, "pwd" : "cef6152f6115918222e6ee2fa7784d7a", "_id" : ObjectId("4f58bff8c91266fd5827c6fa") } > exit bye |
Then mongoDB daemon must be issued with –auth option (–cpu is for statistics and –bind_ip to listen on server IP, default port is still 27017):
[mongodb@server1 bin]$ /home/mongodb/mongodb-linux-x86_64-2.0.3/bin/mongod --auth --cpu --bind_ip 192.168.56.101 |
Now you must authenticate to access figures:
[mongodb@server1 ~]$ mongo -u yjaquier -p password 192.168.56.101:27017/admin MongoDB shell version: 2.0.3 connecting to: 192.168.56.101:27017/admin > use test switched to db test > db.foo.find() { "_id" : ObjectId("4f58b8c0488b0b5ce947464b"), "a" : 1 } > exit bye |
Not so much complex and for the ones knowing JavaScript and JSON you should not be lost… Don’t know if really an added as wit y RDBMS background I’m a bit disappointed by how figures are stored… But MongoDB added value is more on replication and sharding for scale out and high availability…
MongoVUE
Can be find at this url. Then usage is straightforward:
RockMongo
Can be downloaded at his url.
If not yet done install Apache and PHP:
Then for all those PHP tools you need the mongoDB PHP driver. I discovered that pre-compiled release is for PHP 5.2 while I have 5.3.3 on my test server. So obliged to compile it, to do it you will need to install php-devel package to have access to phpize tool:
[root@server1 tmp]# unzip -q mongodb-mongo-php-driver-1.2.9-119-gd7c19b8.zip [root@server1 tmp]# cd mongodb-mongo-php-driver-d7c19b8 [root@server1 mongodb-mongo-php-driver-d7c19b8]# cat rebuild.sh #!/bin/bash phpize && ./configure && make clean all install [root@server1 mongodb-mongo-php-driver-d7c19b8]# ./rebuild.sh |
Once done (if no error) then a new module has been created for PHP (mongo.so):
[root@server1 ~]# ll /usr/lib64/php/modules/ total 3208 -rwxr-xr-x 1 root root 66160 Mar 30 2011 curl.so -rwxr-xr-x 1 root root 1843536 Mar 30 2011 fileinfo.so -rwxr-xr-x 1 root root 36200 Mar 30 2011 json.so -rwxr-xr-x 1 root root 980811 Mar 8 17:07 mongo.so -rwxr-xr-x 1 root root 261392 Mar 30 2011 phar.so -rwxr-xr-x 1 root root 83056 Mar 30 2011 zip.so |
Create the PHP initialization file:
[root@server1 ~]# cat /etc/php.d/mongo.ini ; Enable redis extension module extension=mongo.so |
Then download RockMongo zip file and unzip it in your Apache document root directory:
[root@server1 html]# pwd /var/www/html [root@server1 html]# unzip -q /tmp/rockmongo-v1.1.0.zip |
Finally to be able to connect I modified the config.php file as below:
/** * mini configuration for another mongo server */ $MONGO["servers"][$i]["mongo_name"] = "192.168.56.101"; $MONGO["servers"][$i]["mongo_host"] = "192.168.56.101"; $MONGO["servers"][$i]["mongo_port"] = "27017"; $MONGO["servers"][$i]["mongo_auth"] = true;//enable control users, works only if mongo_auth=false $i ++; |
Finally you should have something like:
gururaj says:
Hi,
We are looking for a trainer to deliver training on Mongo DB DBA
Location: Chennai ( India)
mail me: gururaj.k@sfjbs.com
Yannick Jaquier says:
Hello,
My knowledge on MongoDB is so poor that would not be a gift to provide training on it…
Thanks, Yannick.