What is Replication in MongoDB?

Replication is the process of synchronizing data across multiple servers. Replication provides redundancy and increases data availability with multiple copies of data on different database servers. Replication can provide increased read capacity as clients can send read operations on different servers. You can also maintain additional copies for a dedicated purpose, such as disaster recovery, reporting, or backup.

A replica set is a group of mongod instances that maintain the same set of data set. A replica set contains various data bearing nodes and optionally one arbiter node. Of the data bearing nodes, one and only one member is primary node, while the other nodes are secondary nodes. The primary nodes receive all write operations. The secondary replicates the primary’s oplog and apply the operations to the datasets such that the secondaries data sets reflects the primary’s data set. If the primary is unavailable, The eligible secondary will hold an elect to elect itself the new primary.

To add Members to the replica set:

  • Start the new mongod instance.
  • Specify the data directory and the replica set name

Mongod –dbpath/srv/mongodb/db0 –repSet rs0 Data Directory -/srv/mongodb/db() Replica set – rs0

  • Add the new member

rs.add(host,arbiterOnly) rs.add(“mongodb5.example.net:27017”)

  • Verify that the member is now part of the replica set.

rs.conf()