Mongod instances need to be setup on the servers you plan to use in the replica set.
Ideally, a replica set is to have an odd number of mongod instances; this is to ensure that during an election for a master mongod instance, quorom can be reached. If you can only set up even mongod instances, then you have to add an arbiter. The minimum number of mongod instances is three, if you can only setup two, then you have to add an arbiter.
Arbiters are mongod instances that are part of a replica set but do not hold data. Arbiters participate in elections in order to break ties.
I have seen a few cases where all mongod instances of a replica set are setup on a single server, I think it beats the purpose of setting up a replica set. If that server goes down, then your MongoDB server becomes unavailable.
The connection between the Mongod instances on the different servers needs to be tested and confirmed. This is important if you use a firewall on your servers. I have had cases where I setup a replica set and it wasn’t working only to find out the connection port between the Mongod instances was blocked.
The hostname of the servers where other Mongod instances are located needs to be added to each server’s
/etc/hosts. For a server with hostname
idarlington and ip
192.168.2.218, it should be added as :
After confirming the connection between the Mongod instances, you can now begin the replica set setup proper. You need to edit the MongoDB conf file that starts the Mongod service.
For most linux systems the conf file is located in the
/etc/init. For MongoDB 2.4 and lower, the file is
mongodb.conf while for MongoDB 2.6 and above, the file is
For MongoDB version 2.6 and above
For MongoDB 2.4 and below
You can now exit the conf file and start a mongo shell on the server you want to be the primary node.
Then to initiate the replica set :
To show the configuration of the replica set :
To show the recent status of the replica set :
After initiating the replica set, its only member is the current server where you are running the mongo shell. To add other members to the replica set, use the IP address or hostname of the server with the mongod instance you want to add like
Arbiters are useful when you have an even number of nodes, if you already have an odd number of nodes, it is not needful. To add an arbiter, you can either use its hostname or ip address.