MongoDB : Replication configuration

Previous : mongoDB replica sets

Replication set creation & configuration

Create a Replication Set

First of create 3 directories for each MongoDB Instance, like so c:\Mongo\DB1, DB2, DB3 etc. Then you would start a Mongo instance using each one of those directories…. (CD to the Mongo directory and type away on your console the following)

@Rem Primary
start "a"   mongod  --dbpath ./db1 --port 30000 --replSet  "myDemo"
@Rem Secondary
start "a"   mongod  --dbpath ./db2 --port 40000 --replSet  "myDemo"
@Rem Arbitrator
start "a"   mongod  --dbpath ./db3 --port 50000 --replSet  "myDemo"

Notice that each Mongo instance is on a different directory and a different TCP port (because we are setting this up on the same machine and we cant have everyone listening on the standard port of 27017) The “–replset” option will tell mongo the name of the replicaset (myDemo)  that all three instances will participate in. Running the above command will start all the 3 instances of the mongo server.

At this point the servers dont know much about each other (i.e. who is the primary, secondary or the arbitrator). All they know is that they belong to the same replica set. We will need to configure the roles of the servers using a configuration  script by connecting into one of the servers.

Lets just connect to the primary (even though its really not a primary at this point). so here goes nothing

mongo –port 30000

.This will connect you to the localhost ( on port 30000. to verify this you could type “db.getmongo()

Next up, we will define and configure the replica set such that it has 3 members. As mentioned earlier the Mongo Shell is a JavaScript  interpreter. So we will go ahead and create  a JavaScript Object, which will be the configuration or the replica set.

It goes something like this

var  configdemo={_id: “myDemo”  members: [{ _id: 0; host: ‘localhost:30000’, priority: 10}, { _id: 1; host: ‘localhost:40000’}, { _id: 2; host: ‘localhost:50000’, arbiteronly:true}]};

If you want to see a more verbose and human friendly display of the setting simply type configdemo.  IN the configuration above, _id:demo is the name of the replication set and the members are described in an array. This is a very minimalistic and simple setup. for detailed configuration information like geographic redundancy etc see this link.

Notice that the primary has a priority of 10. In a replica set, the higher the priority, the more number of votes ( therefore likely to be promoted to primary) it will get in case of a failure of the primary. Having no priority simply means it will be  a secondary. Having an extra attribute of “arbiteronly” tells mongo that the server will not store data and will receive neither read or write request.

To initialize the replication we do

“info” : “config  now saved locally. Should come online in a minute.”
“OK” : 1

Notice that the prompt changed from “>” to “demo:startup2”.  I.E. the replica set name (demo) followed by the status of the current server we are connected to. If you were to hit <enter> again, the prompt changes to


Since the status has changed from STARTUP to PRIMARY it means that the replication set has started successfully and that we are connected to the “primary”. Since its primary, it can accept new write commands and accept new data. So lets just try that

Verifying that Replication works

myDemo:primary>{_id:1, value: ‘hello there’})

this will enter data into the collection FOO that has an ID of 1 and value of “hello there”.

{“_id”:1, “value”: ‘hello there’}

AS you can see the record had been saved. Now lets connect to the secondary and see if the data has been replicated. so we connect to the secondary via the command  Mongo –port 40000

Mongo –port 40000
MongoDB Shell version: x.x.x
connecting to
error: {“$err” : “not master and slaveok=false”, “code”: 12345}
{“_id”:1, “value”: ‘hello there’}

The error just simply means that you cant read from a secondary unless you say its OK. So we do just that. SetSlaveOK() is a connection level setting… and presto it magically finds the values saved on the PRIMARY. This shows that the replication was successful.

Replication Fail-over

To demonstrate a replication fail-over simply kill the primary by closing or CTRL-C the primary server console.
doing a
demo:secondary>  yields  {“_id”:1, “value”: ‘hello there’}. So it’s still readable.

After a few seconds the secondary gets promoted to primary.

myDemo:Primary> db.getMongo()
Connection to 40000

Notice that the primary was previously on port 30000. Now lets resurrect the primary just like we did before

cmd> start "a"   mongod  --dbpath ./db1 --port 30000 --replSet  "myDemo"

We will now connect back into the secondary

cmd>mongo –port 40000
MongoDB Shell version: x.x.x
connecting to

AS you can see its connected back into the secondary (the secondary has been demoted from primary when the Primary came back online)

BTW to get help on the commands available for the repo set (RS) use

to get the status of the reposet  issue rs.status() and it will give you a very descriptive text with information ranging from the health of the reposet to the roles of each server, their ports , polling intervals etc.

As you can see fail-over is a fairly fast and simple process both in terms of configuration  as well as DB performance.

Next up: MongoDB Storage Internals

For all your application development needs, visit for a fiscally conscious proposal that meets your needs ( So I can keep this blog going as well!!!!)

Alternatively click through the link   if you found this article interesting. (This will help the companies Search engine rankings)



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s