Setup MongoDB Replica Set

Setup MongoDB Replica Set – In this MongoDB Tutorial, we shall learn to set up a replica set on a single machine or across multiple machines connected in a network.


Setup Replica Set on a single machine with multiple instances

In this scenario, there is only a single machine, but we run multiple (for this example, two) mongod instances. One instance acts as PRIMARY and other instances act as SECONDARY.

To set-up Replica Set on a single machine with multiple mongod instances, following is a step by step guide :

  1. Start a mongod instance.

    Standalone mongod instance by default uses 27017 port and /var/lib/mongodb/ (in ubuntu) data path. We shall use the same for this mongod instance.
    Run the following command to start a mongod instance.

    –replSet rs0 : meaning we are setting up a set of nodes to replicate, and the name given to this set is rs0.

  2. Start another mongod instance.

    We should use unique set of port and data path for each instance, otherwise we may get port binding error and data path issues. For this instance we shall use 27018 port and /var/lib/mongodb1/ data path. You may create the directory, /var/lib/mongodb1/ using following command.

    Run the following command to start mongod instance.

    Observe that this instance is also started with same Replica Set.

  3. Start Replication.

    You may create as many number of instances as required and feasible, by following the second step.
    To start replication, open mongo shell and run the following command :

    > rs.initiate()
    Now the mongo shell prompt would be changed to rs0:PRIMARY>

    You may also check the status using rs.status() method.

  4. Add a MongoDB instance to the Replica Set.

    To add the MongoDB instances that we already started in step 2, run the following command in the mongo shell that we already opened in the previous step 2.

    > rs.add(<hostname:port>);

    To check the hostname, Open a new Terminal and run the following command

    $ hostname
    We shall add the second MongoDB instance running at tutorialkart:27018.
    Response { “ok” : 1 } meaning addition of mongod instance to the replica set is successful.
  5. Check the Status.

    You may check the status of the Replica Set by running the following command

    > rs.status();
    Now there are two members in the Replica Set, with tutorialkart:27017 being PRIMARY and tutorialkart:27018 being SECONDARY.
  6. Check Replication.

    Now we shall check if the replication is happening correctly.
    In the PRIMARY instance, insert a document to MongoDB Database.

    It is time to check in the SECONDARY instance, if this has replicated.
    To connect to MongoDB instance running at 27018, run the following command

    Yay!! The replication is happening just as fine.


Set-up Replica Set with multiple machine

In this scenario, there are multiple machines connected over network and there is a MongoDB instance on each of the nodes.

The steps to Setup Replica Set with multiple machines is same as that of Setup Replica Set with multiple machine, with only difference being, instead of starting a second instance one the same machine, start the mongoDB instance on the other machines on the network.

Initially, the one instance from which you start replication, acts as PRIMARY and other instances acts as SECONDARY.


Conclusion :

In this MongoDB Tutorial – Setup MongoDB Replica Set, we have learnt to Setup a Replica Set on a single machine (with multiple mongod instances on a single machine) or across multiple machines connected in a network (with mongod instances running on different machines connected in network).