MongoDB - Replication

Replication additionally allows you to get over hardware failure and provider interruptions. With extra copies of the records, you could dedicate one to disaster restoration, reporting, or backup.

Replication Benefits
  • To maintain your facts safe
  • High (24*7) availability of facts
  • Disaster recuperation
  • No downtime for safety (like backups, index rebuilds, compaction)
  • Read scaling (more copies to study from)
  • Replica set is apparent to the software

Here Replication Works in MongoDB

First duplicate set is a set of mongod times that host the equal facts set.All different instances, consisting of secondaries, observe operations from the number one so that they've the same facts set.
  • Replica set is a collection of or more nodes (commonly minimum 3 nodes are required).

  • In a duplicate set, one node is number one node and remaining nodes are secondary.

  • All statistics replicates from primary to secondary node.

  • At the time of automated failover or maintenance, election establishes for primary and a today's primary node is elected.

  • After the healing of failed node, it yet again be a part of the replica set and works as a secondary node.

Replica Set Features

  • A cluster of N nodes
  • Any one node can be number one
  • All write operations visit primary
  • Automatic failover
  • Automatic restoration
  • Consensus election of number one

Set Up a Replica Set

we will convert standalone MongoDB example to a reproduction set. To convert to reproduction set, following are the steps −

  • Shutdown already jogging MongoDB server.

  • Start the MongoDB server by using way of specifying -- replSet choice. Following is the number one syntax of --replSet −

mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"


mongod --port 2018 --dbpath "D:set upmongodbdata" --replSet rs0
  • It will start a mongod example with the name rs0, on port 2018.

  • Now start the command prompt and hook up with this mongod instance.

  • In Mongo customer, hassle the command rs.Provoke() to initiate a brand new reproduction set.

  • To test the duplicate set configuration, trouble the command rs.Conf(). To take a look at the fame of replica set issue the command rs.Status().

Add Members to Replica Set

To add people to replica set, begin mongod times on a couple of machines. Now begin a mongo client and trouble a command rs.Upload().


The fundamental syntax of rs.Add() command is as follows −



Suppose your mongod example name is projectmongodb and it is running on port 2018. To add this example to replica set, issue the command rs.Add() in Mongo purchaser.


You can upload mongod instance to replica set only while you are linked to number one node. To take a look at whether you are related to number one or no longer, problem the command db.IsMaster() in mongo purchaser.

welookups is optimized for learning.© welookups 2018 -
All Right Reserved and you agree to have read and accepted our term and condition