Setup MongoDB master/slave replication

June 1, 2010    database mongodb nosql redhat

I am recently getting into MongoDB. Thanks to my company (OpenSky) sending me to MongoNYC event. I am also tasked with setting up infrastructure to run MongoDB. So we are starting off with a simple master/slave MongoDB setup.

I always thought MySQL was easy to setup replication on but MongoDB proves it just gets easier. This will expect you are using the official MongoDB provided RPMs from their repository.

So the first thing we want to do is install the -stable branch on both nodes

yum install mongo-stable-server

Now that you have it installed lets edit the init.d script. As of writing this, they do not make use of a sysconfig script.


Now find the following line

OPTIONS=" -f /etc/mongod.conf"

For the master you want to make it look like

OPTIONS=" -f /etc/mongod.conf --master"

For the slave you want to do this

OPTIONS=" -f /etc/mongod.conf --slave --source" is the IP address of the master server.

Now start mongod on both servers and run chkconfig to make sure it loads on restart

chkconfig mongod on<br /> service mongod start

Now on the master run the following command to get into the Mongo Shell


Now in the mongo shell run


You should get something like this if everything is setup correctly

> db.runCommand("ismaster");<br /> { "ismaster" : 1, "msg" : "not paired", "ok" : 1 }

Now lets insert something into the master server. By default you connect to the test database and this is not replicated so you must switch to a new database. In Mongo, you don’t have to create a database, just use it and it will create it.

> use zcentric<br /> switched to db zcentric<br /> > db.users.insert({'username': 'mzupan', 'full_name': 'Mike Zupan'});<br /> > db.users.find({})<br /> { "_id" : ObjectId("4c05431becb442845873ec05"), "username" : "mzupan", "full_name" : "Mike Zupan" }

Now on the slave run

> use zcentric<br /> switched to db zcentric<br /> > db.users.find({})<br /> { "_id" : ObjectId("4c05431becb442845873ec05"), "username" : "mzupan", "full_name" : "Mike Zupan" }

So You see it works nicely! Have fun with Mongo.. I am

comments powered by Disqus