Replication In MonogDB

Posted By : Praddumn Kumar | 26-Sep-2017

In MongoDB replication is used to syncronize data across multiple servers.It provides data redundancy and increase data highly available with multiple copies of data at different database servers.Due to replication of data over multiple server,it protects loss of data from the loss of single server.Replication of data over multiple server also provides facility of recovery of data from hardware failure and service interruptions . Replication also reduce downtime in maintenance like backups, index rebuild, compaction etc.


MongoDB uses replication through replica sets which is called group of mongoDB instances.


In replica set, one node(instance) is considered as primary node and rest node(instances) are considered as secondary node. Primary node is used perform/receive all write operations and secondory nodes apply operations from primary node to keep redundant data up-to-date.
In mongoDB replica set can have only one primary node and rest as secondary nodes.All data in secondary nodes replicate from primary node.A replica set can have 50 members and up to 7 voting members.At the time of failure or maintenance , election establishes in primary and new primary instance is elected . After completion of recovery of filed node it again works as a secondary node.

 

Set Up a Replica Set :

Here we will convert a standalone mongoDB to a replica set.
1- First shutdown already running mongoDB server.
2- Starting mongoDB server by specifying replica set option.

syntax of --replicaSet :

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

For example :

mongod --port 27017 --dbpath "D:\set up\mongodb\data" --replSet rs0

--replSet rs0 : shows that replica set name with rs0 , on port 27017. Add Members to Replica Set : If you want to add more members in replica set, first start mongod instances on multiple servers and use rs.add() command to add instances. Syntax :

rs.add(HOST_NAME:PORT)

Example :

rs.add("mongo.net:27017")

here mongo.net is the new mongod instance running on 27017 port.

Monogd instance can be added to replica set only  if  connected to primary instance.

About Author

Author Image
Praddumn Kumar

Praddumn is a bright Web App Developer, and has good knowledge of Core java, Spring and hibernate.

Request for Proposal

Name is required

Comment is required

Sending message..