Replication is the high availability in MongoDB. Basically, this solution is use to replicate data to multiple servers (nodes) so if one server is down then system still can work by responding reads from clients.



In this example, I installed 3 servers as above. They are MongoDB standalone instance. Now we will set up to combine all of them into Replicat solution of MongoDB.

Click here to see how to install MongoDB.

Modify hosts file to enter IP of other machines

On machine

sudo bash

Step 1: Edit hosts file

nano /etc/hosts

Ctrl+X -> Y -> Ctrl+A+M

Step 2: Edit hostname file. Just alias IP Address such friendly name

nano /etc/hostname

Step 3: Edit mongod file

nano /etc/mongod.conf

Enter name of replication set


Repeate step 1 to step 3 for other machines but please notice that Step 2 is different => should be => should be

Check communication across servers

On master mongodb connect to mongodb1 and mongodb1 machines

Step 1: Connect to mongodb0 (

mongo –port 27017 –host

Step 2 : Connect to mongodb1 (

Mongo –port 27017 –host


Step 2 : Connect to mongodb2 (

Mongo –port 27017 –host

Make sure that all connections are done.

Set up Replication

Restart service mongod

By default, I choose as primary server. Restart mongodb on

sudo service mongod restart

Connect to mongodb

mongo –port 27017 –host

Start the replication


This will initiate the replication set and add the current server connected currently to as the first member of the set.



Add new members to the repl



If connect successfully, then we can check status of replicat set.


Replication solution is now available.



One thought on “How to set up Replication in MongoDB

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