Replication is the process of synchronizing statistics across multiple servers. Replication offers redundancy and increases statistics availability with more than one copies of statistics on distinctive database servers. Replication protects a database from the lack of a unmarried server. Replication also lets in you to get over hardware failure and carrier interruptions. With additional copies of the records, you could dedicate one to disaster restoration, reporting, or backup.
- To preserve your data safe
- High (24*7) availability of facts
- Disaster recovery
- No downtime for maintenance (like backups, index rebuilds, compaction)
- Read scaling (greater copies to read from)
- Replica set is obvious to the utility
How Replication Works in MongoDB
MongoDB achieves replication with the aid of using reproduction set. A reproduction set is a collection of mongod times that host the equal data set. In a duplicate, one node is primary node that receives all write operations. All other times, along with secondaries, practice operations from the primary so that they've the same statistics set. Replica set may have simplest one number one node.
- Replica set is a collection of or greater nodes (usually minimum three nodes are required).
- In a replica set, one node is number one node and closing nodes are secondary.
- All data replicates from number one to secondary node.
- At the time of automatic failover or renovation, election establishes for primary and a brand new number one node is elected.
- After the recovery of failed node, it once more be part of the reproduction set and works as a secondary node.
- A traditional diagram of MongoDB replication is proven wherein purchaser application always interact with the number one node and the primary node then replicates the information to the secondary nodes.
Replica Set Features
- A cluster of N nodes
- Any one node may be number one
- All write operations go to number one
- Automatic failover
- Automatic recovery
- Consensus election of number one
Set Up a Replica Set
In this tutorial, we will convert standalone MongoDB instance to a duplicate set. To convert to reproduction set, following are the steps −
- Shutdown already jogging MongoDB server.
- Start the MongoDB server by specifying -- replSet choice. Following is the fundamental syntax of --replSet −
mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"
mongod --port 27017 --dbpath "D:\set up\mongodb\data" --replSet rs0
- It will begin a mongod example with the call rs0, on port 27017.
- Now begin the command spark off and connect to this mongod example.
- In Mongo consumer, problem the command rs.Initiate() to initiate a brand new replica set.
- To take a look at the duplicate set configuration, problem the command rs.Conf(). To check the fame of duplicate set trouble the command rs.Repute().
Add Members to Replica Set
To upload members to reproduction set, begin mongod times on multiple machines. Now begin a mongo client and difficulty a command rs.Upload().
The fundamental syntax of rs.Upload() command is as follows −
Suppose your mongod example name is mongod1.Internet and it's miles running on port 27017. To upload this instance to replica set, problem the command rs.Add() in Mongo client.
You can upload mongod example to reproduction set simplest while you are linked to primary node. To take a look at whether you're related to primary or not, trouble the command db.IsMaster() in mongo client.