Enterprise Java

Setup MongoDB Replica Set in 4 steps


Before going into details of configuring MongoDB replica sets, let me give brief details about same:

  • Replica Set is the feature provided by the Mongodb database to achieve high availability and automatic failover.
  • It is kinda traditional master-slave configuration but have capability of automatic failover.
  • It is basically group/cluster of the mongod instances which communicates, replicates to each other to provide high availability and to do automatic failover
  • Basically, in replica sets there are minimum 2 and maximum of 12 mongod instances can exist
  • In replica set following types of server exist. out of all, one server is always primary.
    • Primary: It stores the data, all write/read operation goes to this server from any client.
    • Secondary: First it stores the data, second these servers stays in sync with primary, if   primary server goes down then these servers are for failover to the primary.
    • BackUp: It stores backup data and as name says, it meant for backup only although it never becomes primary
    • Arbiter: It stores the no data, it meant only for participate in election during failover.
  • It provides automatic failover meaning that it doesn’t require human intervention to make another server as primary and it has internal mechanism to elect new primary server.
  • In replica set, if primary server goes down due to network error or other servers in replica sets not able to reach to primary server or primary become unresponsive then internally by all servers election mechanism is triggered to elect new primary server and all this is very automatic.

In this article, i am going to show you configuration of the 3 member/node cluster replica set in 4 easy steps. In this replica set configuration, we will make two data servers in which one will be primary,  another will be secondary and other one non-data server which is arbiter. So, lets go ahead and configure the replica in following 4 steps:

Step-1 : Setup the each instance of replica set

Create and setup 3 new instances of mongodb server instance the as i discussed in last article with following information but don’t start these server instances:

  • server-1
port = 27017

directory path =<dir path>\ rs1

E.g.directory structure :  I:\workspace\mongo\rs1\bin
  • server-2
port = 26017

directory path =<dir path>\ rs2

E.g.directory structure  I:\workspace\mongo\rs2\bin
  • server-3
port = 25017

directory path =<dir path>\ rs2

E.g.directory structure  I:\workspace\mongo\rs3\bin

Step-2 : modify the mongodb.conf to include replica set information

Edit the mongod.conf  of each server to Include the following properties:

  • replSet = unique name for replica set which is given to all members
  • rest = to enable rest interface for admin web page

You can give any name to replica sets but make sure that it is same for all members. Go ahead give any name you want. Here, i am using  name ‘damon’

E.g. append following into mongod.conf file


Step-3 : start the all the server up

After necessary configuration, go ahead and start the server in any order

rs1 >> mongod -f \rs1\mongod.conf
rs2 >> mongod -f \rs2\mongod.conf
rs3 >> mongod -f \rs3\mongod.conf

Step-4 : configure the servers to include in replica set

Now, all the servers are up but they aren’t connected to each other. In other words, they are not part of replica set. So, for that we need to do following. Again, we going to create the two data server i,e one primary, another secondary and one arbiter server. Now, connect to any one of the server using mongo shell provided in MongoDB binaries.

Here. lets connect to server rs1 using mongodb shell

rs1 > /rs1/bin/mongo.exe --port 27017

After this you will get the prompt for mongo shell as shown in following figure:


For replica configuration, mongo shell provides the predefined object rs which contains the following functions for configuration:

  • initiate() => creates the replica set configuration in current server
  • status() => gives the current status of replica set to which server is part of
  • add(serverInfo) => adds the new server (provided as param) in replica set.
  • addArb(serverInfo) => specialized function for adding the arbiter in the replica set

First we need to create the replica set configuration in one of server. Go and hit the following command in mongo shell:

> rs.initiate()

After the you see following in prompt:


This will create the necessary replica sets information locally for this server and you will notice prompt changing from initial ‘>’ to ‘damon:STARTUP2 >’ which is indicating <replicaSetName:State of server >

You can enter the rs.status() in the shell prompt to check how many servers are in replica set:


As you can see we only have the only one server added in replica set which is current server, whose state is primary.

Other way of checking for the members in replica is use following URL in your browser address bar http://:/_replSet . In our case,   http://localhost:28017/_replSet
Note that, this requires rest=true to enabled in config file.(and we did it already! ). This one can be preferred way of checking for states of servers in replica sets.


Now we have the one server added in replica, lets add the rest of servers.

For add() and addArb() method we are going to provide the string param that contains <machine/host-name> : <port-no-of-server>

(Please note, that if you want machine name then use hostname command in dos or bash shell )





Lets check whether node/member added using rs.status() command:


or using the browser based:


And here we configured the 3 node replica set MongoDB database cluster. Although this piece of information can be applied to configuring any no. of node in replica set.

Reference: Setup MongoDB Replica Set in 4 steps from our JCG partner Abhijeet Sutar at the Another Java Duke blog.

Abhijeet Sutar

Abhijeet (ajduke) is self-taught, self-organized software developer. His current go to language is Java and also he is exploring other languages such as Scala, Ruby.
Notify of

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

Inline Feedbacks
View all comments
Back to top button