how to configure MongoDB replication

Hi Techrunnr Readers, this document deals with how to configure MongoDB replication. Replication of database are necessary when we run a critical application or we give more importance to data. Replication is the process of syncing data from one location to another. This provides redundancy and high availability

how does it work?

MongoDB uses replication set to achieve the replication of database over servers. Replicaset is a cluster of MongoDB database servers which is configured as master-slave. There is only one primary database server in MongoDB replication. Rest of the servers as a Secondary.
Primary server has read and write access whereas Secondary server has only read access.

It’s always recommended to have 3 MongoDB server for replication.

See this example below.

Consider we have 3 servers ie, server1, server2, server3

Where server 1 is primary and rest of the servers are secondary.

When Primary server 1 goes down dues to some hardware or software crash, New primary server is elected between server2 and sever3.
Electing of a Primary server is based upon MongoDB inbuilt algorithm.

Once the server1 restores the replication it won’t be the primary server, it remains as the Secondary server.

Auto-failover is one of the best features of MongoDB. Only you need to configure the multiple MongoDB servers in the application in order to auto-failover to work.

Auto-failover will work only when replication happening between 3 servers. If you 2 servers for replication, once primary server is down the other server remain read-only.

There are ways to make the secondary server as primary which you can see in the next section.

For better and fast electing the primary server, add one more server called arbiter whose main duty to elect primary server. Arbiters are MongoDB instances that are part of a replica set but do not hold data. Arbiters participate in elections in order to break ties. If a replica set has an even number of members, add an arbiter.


* Make sure that hostname is configured in all servers.
NOTE: In centos, the hostname should be permanent.
check you hostname using the following command.

hostname -f

* Install MongoDB package in all the servers, if it is not installed check this link.


1. Add host entry for all the hosts. mongo1 mongo2

After changing in the hosts’ file you need to reboot the instances using sudo reboot command.

2. On each of your nodes, make the following changes to your /etc/mongod.conf file:

port: 27017

replSetName: myreplica01

myreplica01 is the replica set name which we are using.

3. Once changes are done restart the MongoDB service.

systemctl restart mongod

4. Now login to mongo1 and initialize the replication

> rs.initiate()

5. Add the second node


Test replication

In Mongo1 create a database and insert collection

use exampleDB
myreplica01:PRIMARY> for (var i = 0; i <= 10; i++) db.exampleCollection.insert( { x : i } )

Now, ssh into your seconday or slave nodes and connect to mongo shell.

$ ssh mongo2
$ mongo
myreplica01:SECONDARY> rs.slaveOk()
myreplica01:SECONDARY> use exampleDB
myreplica01:SECONDARY> db.exampleCollection.find()
{ “_id” : ObjectId(“5a1483c65826dc1165ca7d19”), “x” : 0 }
{ “_id” : ObjectId(“5a1483c65826dc1165ca7d1c”), “x” : 3 }
{ “_id” : ObjectId(“5a1483c65826dc1165ca7d1b”), “x” : 2 }
{ “_id” : ObjectId(“5a1483c65826dc1165ca7d1d”), “x” : 4 }
{ “_id” : ObjectId(“5a1483c65826dc1165ca7d1a”), “x” : 1 }
{ “_id” : ObjectId(“5a1483c65826dc1165ca7d1e”), “x” : 5 }
{ “_id” : ObjectId(“5a1483c65826dc1165ca7d20”), “x” : 7 }
{ “_id” : ObjectId(“5a1483c65826dc1165ca7d21”), “x” : 8 }
{ “_id” : ObjectId(“5a1483c65826dc1165ca7d22”), “x” : 9 }
{ “_id” : ObjectId(“5a1483c65826dc1165ca7d1f”), “x” : 6 }
{ “_id” : ObjectId(“5a1483c65826dc1165ca7d23”), “x” : 10 }

You can view the config of connected nodes by using below command in any of the nodes:


That’s It!! Now you have completed MongoDB replication setup.

© 2018, Techrunnr. All rights reserved.

Questions Answered
Articles Written
Overall Points

Prabhin Prabharkaran

He is Technical professional. He is a person who loves to share tricks and tips on the Internet. He Posts what he does!!

Leave a Reply

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.