In our three part series, we created a standalone Mongo server and got it running.  It’s time to continue by setting up a second Mongo server with replication.  For this  demonstration, we have two servers:

  • litdmongo1 –  primary member 0
  • litdmongo2 –  secondary member 1

More on the members in a minute, but for right now, the first server should already be up and running with some data.  The second server should be operational without the Mongo service started (make sure you tested the standalone and backed it up before starting the replica setup!).

On litdmongo1, we need to stop the Mongo service:

systemctl stop mongod

DNS should be used if at all possible for machine identification.  However, if this is not available or you fear your DNS may not be available, add the following to your /etc/hosts file on BOTH servers: litdmongo2 litdmongo2.litd.prv litdmongo1 litdmongo1.litd.prv

*Replace the IP addresses with what you are using.

Next, we need to create a name for our replication set.  For most systems, systemctl is used.  Instead of using a startup line, Mongo has a convenient option in the /etc/mongod.conf file.  Make sure you are the first server (the primary server) for the this part.  The line you need to change is:

# Arg is <setname>[/<optionalseedhostlist>]
#replSet = arg

Change it to:

# Arg is <setname>[/<optionalseedhostlist>]
replSet = rs0

The “rs0” is the replica set name.  You can use anything you choose, but this will work for now.

Start the Mongo service on the first/primary server:

systemctl start mongod

A systemctl status mongod should show:


To prepare and start the replica set the first time, open a mongo shell or use Mongo IDE and type:


In Robo3T it looks like this:


The results above should be very similar to yours.  If you type:

rs.status() in a shell window, you will hopefully see an “ok” at the bottom of your status:


Now that the first member is created, we can start adding members.  Remember, on the second server, we did not start it up on purpose.  The data directory (in this case /mongodata) should be empty.  If you already have it started and there is data, overwrite it by copying the data directory from the primary server.  However, if you have been following along our journey here, it’s safe to start the second server:

systemctl start mongod

Check to make sure the primary server is set to “Master” by issuing the following at a Mongo shell prompt:


You should see a result exactly like this:


To add a new member, you will need the following line:

rs.add( { host: “litdmongo2.litd.prv:27017”, priority: 0, votes: 0 } )

The above line will only return “ok.”  However, a simple rs.status() should show multiple systems under the “members” key.  One of the keys to note is the voting and priority variables above.  For the most part, these can be left alone, but if you are creating multiple secondary members, you can adjust how many votes a particular member gets versus the others.  You can also set the priority for which server becomes the primary if an election is forced and two or more have the same number of votes.  If you need to make this change use the following command:


This is only a two node test of replication, so no change needs to be made.  The documentation can be checked out here.  There are also a few notes about changing the configuarion.  First, any reconfiguration kicks users off the database server and lasts between 10 and 20 seconds.  It also doesn’t work well if you have replication servers that are on different versions of Mongo.