Getting started with MongoDB is relatively straight forward, following the instructions from the Quickstart guide has you operational in a few minutes.
I like projects that provide a latest version link for software. There is no need to update any documentation or blog posts over time. The current instructions require some additional steps when creating the initial data directory, due to normal permissions of the root directory. This is the only pre-requisite to using the software out of the box. There is no additional configuration required for a default installation.
$ sudo mkdir /data/db $ sudo chown `id -u` /data/db
I ran a few boundary tests to verify the error handling of the initial startup process.
The following occurs when the data directory does not exist.
$ ./mongodb-osx-i386-1.4.3/bin/mongod ./mongodb-osx-i386-1.4.3/bin/mongod --help for help and startup options Tue Jun 8 15:59:52 Mongo DB : starting : pid = 78161 port = 27017 dbpath = /data/db/ master = 0 slave = 0 32-bit ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data ** see http://blog.mongodb.org/post/137788967/32-bit-limitations for more Tue Jun 8 15:59:52 Assertion: 10296:dbpath (/data/db/) does not exist 0x68572 0x247814 0x24821a 0x24a855 0x1e06 0 mongod 0x00068572 _ZN5mongo11msgassertedEiPKc + 514 1 mongod 0x00247814 _ZN5mongo14_initAndListenEiPKc + 548 2 mongod 0x0024821a _ZN5mongo13initAndListenEiPKc + 42 3 mongod 0x0024a855 main + 4917 4 mongod 0x00001e06 start + 54 Tue Jun 8 15:59:52 exception in initAndListen std::exception: dbpath (/data/db/) does not exist, terminating Tue Jun 8 15:59:52 dbexit: Tue Jun 8 15:59:52 shutdown: going to close listening sockets... Tue Jun 8 15:59:52 shutdown: going to flush oplog... Tue Jun 8 15:59:52 shutdown: going to close sockets... Tue Jun 8 15:59:52 shutdown: waiting for fs preallocator... Tue Jun 8 15:59:52 shutdown: closing all files... Tue Jun 8 15:59:52 closeAllFiles() finished Tue Jun 8 15:59:52 dbexit: really exiting now
The following error occurs when the user has insufficient permissions for the directory.
$ sudo mkdir /data/db $ ./mongodb-osx-i386-1.4.3/bin/mongod ./mongodb-osx-i386-1.4.3/bin/mongod --help for help and startup options Tue Jun 8 16:01:52 Mongo DB : starting : pid = 78178 port = 27017 dbpath = /data/db/ master = 0 slave = 0 32-bit ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data ** see http://blog.mongodb.org/post/137788967/32-bit-limitations for more Tue Jun 8 16:01:52 User Exception 10309:Unable to create / open lock file for dbpath: /data/db/mongod.lock Tue Jun 8 16:01:52 exception in initAndListen std::exception: Unable to create / open lock file for dbpath: /data/db/mongod.lock, terminating Tue Jun 8 16:01:52 dbexit: Tue Jun 8 16:01:52 shutdown: going to close listening sockets... Tue Jun 8 16:01:52 shutdown: going to flush oplog... Tue Jun 8 16:01:52 shutdown: going to close sockets... Tue Jun 8 16:01:52 shutdown: waiting for fs preallocator... Tue Jun 8 16:01:52 shutdown: closing all files... Tue Jun 8 16:01:52 closeAllFiles() finished Tue Jun 8 16:01:52 shutdown: removing fs lock... Tue Jun 8 16:01:52 couldn't remove fs lock errno:9 Bad file descriptor Tue Jun 8 16:01:52 dbexit: really exiting now
A missing step from the existing documentation is to set appropriate permissions to the data directory so that mongod run as your normal user can write to the directory.
$ sudo chown `id -u` /data/db $ ./mongodb-osx-i386-1.4.3/bin/mongod Tue Jun 8 16:06:37 Mongo DB : starting : pid = 78284 port = 27017 dbpath = /data/db/ master = 0 slave = 0 32-bit ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data ** see http://blog.mongodb.org/post/137788967/32-bit-limitations for more Tue Jun 8 16:06:37 db version v1.4.3, pdfile version 4.5 Tue Jun 8 16:06:37 git version: 47ffbdfd53f46edeb6ff54bbb734783db7abc8ca Tue Jun 8 16:06:37 sys info: Darwin broadway.local 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:55:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_40 Tue Jun 8 16:06:37 waiting for connections on port 27017 Tue Jun 8 16:06:37 web admin interface listening on port 28017
You are then ready to rock and roll. Use the exit command to exit the mongo interactive shell.
$ ./mongodb-osx-i386-1.4.3/bin/mongo > db.foo.save ({a:2}); > db.foo.find(); { "_id" : ObjectId("4c0ea308f5ea2f7148a33b9f"), "a" : 2 } > exit bye
The mongod output of the first save of data into the “foo” collection shows a little of how mongodb operations. There is a lazy instantiation of “test” database when first required. There is no need to first create this, or use this as with MySQL.
Tue Jun 8 16:07:36 allocating new datafile /data/db/test.ns, filling with zeroes... Tue Jun 8 16:07:36 done allocating datafile /data/db/test.ns, size: 16MB, took 0.05 secs Tue Jun 8 16:07:37 allocating new datafile /data/db/test.0, filling with zeroes... Tue Jun 8 16:07:38 done allocating datafile /data/db/test.0, size: 64MB, took 1.282 secs Tue Jun 8 16:07:40 building new index on { _id: 1 } for test.foo Tue Jun 8 16:07:40 Buildindex test.foo idxNo:0 { name: "_id_", ns: "test.foo", key: { _id: 1 } } Tue Jun 8 16:07:40 done for 0 records 0.018secs Tue Jun 8 16:07:40 insert test.foo 3181ms
$ ls -l /data/db total 163848 drwxr-xr-x 2 rbradfor admin 68 Jun 8 16:07 _tmp -rwxr-xr-x 1 rbradfor admin 6 Jun 8 16:06 mongod.lock -rw------- 1 rbradfor admin 67108864 Jun 8 16:07 test.0 -rw------- 1 rbradfor admin 16777216 Jun 8 16:07 test.ns
One observation is the output of the mongod is more a trace output. I have yet to see any information about a more appropriately formatted error log.
Calvin Froedge says
This post helped me figure out my problem. I hadn’t chowned the /data/db directory. Thank you!
Thomas says
This was very help full, however the doc uses mongod run –config /usr/local/etc/mongod.conf instead, for what reason I know not.