MongoDB Replication (Replica Set)
MongoDB replication is the process of synchronizing data across multiple servers.
Replication provides redundancy of data, stores copies of data on multiple servers, enhances data availability, and ensures data security.
Replication also allows you to recover data from hardware failures and service interruptions.
What is Replication?
- Ensures data security
- High data availability (24*7)
- Disaster recovery
- No downtime for maintenance (such as backups, rebuilding indexes, compression)
- Distributed data reads
MongoDB Replication Principles
MongoDB replication requires at least two nodes. One is the primary node, responsible for handling client requests, and the rest are secondary nodes, responsible for replicating data from the primary node.
Common configurations for MongoDB nodes include one primary and one secondary, or one primary and multiple secondaries.
The primary node records all operations in an oplog. Secondary nodes periodically poll the primary node for these operations and execute them on their data副本 to ensure consistency with the primary node.
The MongoDB replication structure is illustrated as follows:
In the above structure, clients read data from the primary node. When clients write data to the primary node, the primary node interacts with the secondary nodes to ensure data consistency.
Replica Set Features:
- Cluster of N nodes
- Any node can become the primary node
- All write operations are performed on the primary node
- Automatic failover
- Automatic recovery
MongoDB Replica Set Setup
In this tutorial, we use the same MongoDB instance for the primary and secondary nodes. The steps are as follows:
- Shut down the running MongoDB server.
Now, start MongoDB by specifying the --replSet option. The basic syntax for --replSet is as follows:
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
The above example starts a MongoDB instance named rs0 with port 27017.
After starting, open a command prompt and connect to the MongoDB service.
In the Mongo client, use the command rs.initiate() to initiate a new replica set.
You can use rs.conf() to view the replica set configuration.
To view the replica set status, use the rs.status() command.
Adding Members to the Replica Set
To add members to the replica set, you need to start mongo services on multiple servers. Enter the Mongo client and use the rs.add() method to add members to the replica set.
Syntax
>rs.add(HOST_NAME:PORT)
Example
Suppose you have started a MongoDB service named mongod1.net with port 27017. In the client command window, use the rs.add() command to add it to the replica set, as shown below:
>rs.add("mongod1.net:27017")
>
In MongoDB, you can only add a Mongo service to the replica set through the primary node. To determine if the current running service is the primary node, use the command db.isMaster().
MongoDB's replica set differs from the common master-slave setup. In master-slave, all services stop when the master goes down, whereas in a replica set, a secondary node takes over as the primary node when the primary goes down, preventing any downtime.