Configure MongoDB Replica Set

By | October 29, 2016


A replica set provides MongoDB with redundancy and increased data availability by replicating the database to multiple servers. The primary member of the replica set can be used for read and write operations whereas the secondary members are available for read-only operations.

A replica set must consist of an odd number of MongoDB servers. If a single server fails, the replica set members must be able elect a new primary server. An election majority can also be achieved with two database servers by including a third MongoDB arbiter. The arbiter does not have to maintain a copy of the database, but it does need to be a separate server instance.


  • Minimum of three server instances
  • MongoDB 2.6 installed and running
  • Each server instance must allow TCP traffic from each replica set member over port 27017
  • Each server instance hostname must be resolvable from each replica set member

As mentioned in the above requirements, each replica set member must be able to communicate with the other replica set members by their hostname. The hostname will default to the fully qualified domain name of each server. The server hostnames can be added to DNS, but the recommended method is to add all replica set member hostnames to their /etc/hosts file. For example: mongodb01 mongodb02 mongodb03

Create Replica Set

MongoDB must successfully be running on the primary server instance. The standalone instance of MongoDB can become a replica set member by assigning a replica set name in the MongoDB configuration file. To create a replica set called “rs0”, add the following line to /etc/mongod.conf.


MongoDB will need to be restarted for the change to take affect.

sudo /etc/init.d/mongod restart

Use the mongo shell to connect once the service is running again.


Initiate the replica set from within the mongo shell.


The initial replica set configuration can be verified using rs.conf().

    "_id" : "rs0",
    "version" : 1,
    "members" : [
            "_id" : 0,
            "host" : ""

At this stage, the mongo shell prompt should also indicate the replica name and replica member role. For example:


The replica set is now created but more members must be added to achieve redundancy and increased data availability.

Add Secondary Members

The secondary MongoDB server instances must be successfully running MongoDB and accessible by the other members. The configuration file on the secondary members must reflect the same replica set name as the primary member. Add the following line to the /etc/mongod.conf on all secondary members.


Restart the MongoDB service.

sudo /etc/init.d/mongod restart

The secondary members can now be added from the mongo shell on the primary server using rs.add().

rs0:PRIMARY> rs.add("")
rs0:PRIMARY> rs.add("")

The rs.status() command can be used to verify the status of the replica set:


Add an Arbiter

An arbiter is an optional member of a replica set which stores no data. Instead, it is available to break election ties in a situation where there is an even number of MongoDB members.

An arbiter member does not require the same performance capabilities of a full MongoDB server, but it must reside on a separate server instance. Never configure an arbiter on an existing replica set member.

To add an arbiter, first install MongoDB on a server instance accessible by the other replica set members and add the replica set name to /etc/mongod.conf.


Unnecessary data can be reduced on the arbiter server by adding the following files to /etc/mongod.conf.


Now log into the primary replica set member and launch the mongo shell.


The arbiter can be added with the following command:


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.