MongoDB - Replication

Replication is the process of synchronizing data between multiple servers. Replication provides redundancy and increases data availability by having multiple copies of data on different database servers. Replication protects the database from the loss of a single server. Replication also allows you to recover from hardware failures and service outages. With additional copies of your data, you can set aside one for disaster recovery, reporting, or backup.
Why Replication?
- To keep your data safe
- High (24 * 7) data availability
- Disaster recovery
- No time for maintenance (e.g. backup, index rebuild, compression)
- Read scaling (additional copies for reading)
- The replica set is transparent to the application
How replication works in MongoDB
MongoDB achieves replication with a replica set. A replica set is a group of mongod instances that contain the same set of data. In a replica, one node is the primary node that receives all writes. All other instances, such as secondary instances, apply operations from the primary so that they have the same set of data. A replica set can have only one primary node.
-
A replica set is a group of two or more nodes (usually a minimum of 3 nodes is required).
-
In a replica set, one node is the primary node and the other nodes are secondary nodes.
-
All data is replicated from the primary to the secondary node.
-
During automatic failover or maintenance, the selection is set to primary and a new primary is selected.
-
After the failed node is restored, it rejoins the replica set and functions as a secondary node.
A replica set is a group of two or more nodes (usually a minimum of 3 nodes is required).
In a replica set, one node is the primary node and the other nodes are secondary nodes.
All data is replicated from the primary to the secondary node.
During automatic failover or maintenance, the selection is set to primary and a new primary is selected.
After the failed node is restored, it rejoins the replica set and functions as a secondary node.
A typical MongoDB replication scheme is shown where the client application always communicates with the primary node, and the primary node then replicates data to the secondary nodes.
Features of a replica set
- Cluster of N nodes
- Any node can be the master
- All write operations go to the main
- Automatic failover
- Automatic recovery
- Consensual primary election
Install a replica set
In this tutorial, we will convert a standalone MongoDB instance to a replica set. To convert to a replica set, follow these steps:
-
Shutting down an already running MongoDB server.
-
Start the MongoDB server with the -replSet parameter. Following is the basic syntax of --replSet −
Shutting down an already running MongoDB server.
Start the MongoDB server with the -replSet parameter. Following is the basic syntax of --replSet −
mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"
example
mongod -- port 27017 -- dbpath "D:\set up\mongodb\data" -- replSet rs0
-
It will start a mongod instance named rs0 on port 27017.
-
Now start a command prompt and connect to this mongod instance.
-
In the Mongo client , issue the rs.initiate () command to initiate a new replica set.
-
To check the replica set configuration, issue the rs.conf () command . To check the status of a replica set, issue the rs.status () command .
It will start a mongod instance named rs0 on port 27017.
Now start a command prompt and connect to this mongod instance.
In the Mongo client , issue the rs.initiate () command to initiate a new replica set.
To check the replica set configuration, issue the rs.conf () command . To check the status of a replica set, issue the rs.status () command .
Add members to a replica set
To add members to a replica set, run mongod instances on multiple machines. Now start the Mongo client and enter the rs.add() command .
Syntax
The basic syntax of the rs.add() command is as follows:
> rs . add ( HOST_NAME : PORT )
example
Let's say your mongod instance name is mongod1.net and it's running on port 27017 . To add this instance to the replica set, issue the rs.add () command in the Mongo client.
> rs . add ( "mongod1.net:27017" ) >
You can only add a mongod instance to a replica set when you are connected to the primary node. To check if you are connected to the master or not, issue the db.isMaster () command in the mongo client.