Centos 6/RHEL install MongoDB

MongoDB (from “humongous”) is a high-performance, open source, schema-free document-oriented database. It is used in a variety of applications at institutions across the globe. It has a huge following of contributors, with eminent software engineers from around the world attached to the project in a list which reads like a who’s who of the software world.

It has the MongoDB manual which is equally large at 1234 pages long and will take the majority of people the best part of a lifetime to negotiate. There are sections on the MongoDb site relating to the variety of implementations for Mongo, which are numerous. 

A recent addition is the Little MongoDB book by Karl Seguin, who produced a similar book for the Redis Database. Online courses are now available from 10gen in collaboration with Edx (Mit/Harvard. Probably the best resources to get started with Mongo are the MongoDB Tutorial or the MongoDB quick reference booklet.

MongoDB is written in C++ and offers the following features:

   * Collection oriented storage: easy storage of object/JSON-style data
   * Dynamic queries
   * Full index support, including on inner objects and embedded arrays
   * Query profiling
   * Replication and fail-over support
   * Efficient storage of binary data including large objects (e.g. photos
   and videos)
   * Auto-sharding for cloud-level scalability (currently in early alpha)
   * Commercial Support Available

A key goal of MongoDB is to bridge the gap between key/value stores (which are fast and highly scalable) and traditional RDBMS systems (which are deep in functionality).

Mongo uses a different structure to relational databases, with collections instead of tables, documents instead of rows and fields instead of columns. Additionally it uses lightweight, easily traversable and efficient binary serialized JSON(BSON

Queries and other operations in MongoDB are represented as documents passed to find() and other methods. 

10gen provide the binaries for Centos 6/RHEL

$ sudo vi /etc/yum.repos.d/10gen.repo         enter into it

For 64bit

name=10gen Repository

For 32bit

name=10gen Repository

(If enabled use priorities)

See vi or Vim commands

Install the server

$ sudo yum -y --enablerepo=10gen install mongo-10gen mongo-10gen-server

$ sudo service mongod start            to start the server

$ mongo                                starts an instance

You do not need to seperately 'create' a database with mongo as the first insert command will do this, so let's put in some data

>  db.firstdb.insert({name: 'Gertrude'})

and find it with find()

>  db.firstdb.find()
{ "_id" : ObjectId("51b647bba7e270046e1d364a"), "name" : "Gertrude" }

To drop the database first use it

>  use firstdb
switched to db firstdb

>  db.dropDatabase();
{ "dropped" : "firstdb", "ok" : 1 }

Make sure its gone

> db.firstdb.find()


You should shut down the server properly, or it will lock down. In the mongo shell switch to the admin database and issue the shutdownServer() command.

> use admin 

> db.shutdownServer()
Thu Jun 13 08:06:29.049 DBClientCursor::init call() failed
server should be down...
Thu Jun 13 08:06:29.050 trying reconnect to
Thu Jun 13 08:06:29.051 reconnect failed couldn't connect to server

and then exit the instance

>  exit

Now if you start the server again it will function correctly, if you have not shut it down as above, at least on Centos 6/RHEL then you may get an error on restarting and have to go through
the fixing procedure. But on restart now its fine

$ sudo service mongod start
Starting mongod: Thu Jun 13 11:05:29.231
Thu Jun 13 11:05:29.231 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Thu Jun 13 11:05:29.231
about to fork child process, waiting until server is ready for connections.
forked process: 2561
all output going to: /var/log/mongo/mongod.log
child process started successfully, parent exiting

You can also shut it down from the bash shell with the following

$  sudo mongod --dbpath /var/lib/mongo --shutdown
Thu Jun 13 08:32:53.867
Thu Jun 13 08:32:53.867 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Thu Jun 13 08:32:53.867
killing process with pid: 3670

[anton@Centos ~]

(Change the --dbpath flag to yours if its different)

MongoDB is huge entity and a little different but it is worth the effort once you get comfortable with it. See more commands at


and there are, as mentioned above, a lot of other resources.

MongoDB, a popular database which works well on Centos 6/RHEL. 

More repos

Labels: , ,