English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Replication (Replica Set) in MongoDB

MongoDB replication is the process of synchronizing data across multiple servers.

Replication provides redundant data backup and stores data copies on multiple servers, improving data availability, And can ensure data security.

Replication also allows you to recover data from hardware failures and service interruptions.

What is replication?

  • Ensure data security
  • High data availability (24*7)
  • Disaster recovery
  • No downtime maintenance is required (such as backup, index reconstruction, compression)
  • Distributed data reading

MongoDB replication principle

MongoDB replication requires at least two nodes. One is the master node, responsible for handling client requests, and the others are slave nodes, responsible for replicating the data on the master node.

The common node pairing methods for MongoDB are: one master and one slave, one master and multiple slaves.

The master node records all operations on it in the oplog, and the slave nodes periodically poll the master node to obtain these operations, and then execute these operations on their own data copies, thus ensuring that the data of the slave nodes is consistent with the master node.

The MongoDB replication structure diagram is as follows:

In the above structure diagram, the client reads data from the master node, and when the client writes data to the master node, The master node and the slave node interact with each other to ensure data consistency.

Replica set features:

  • N-node cluster
  • Any node can be the master node
  • All write operations are on the master node
  • Automatic failover
  • Automatic recovery

MongoDB replica set settings

In this tutorial, we use the same MongoDB to do the MongoDB master-slave experiment, The operation steps are as follows:

1to start and stop the running MongoDB server.

Now we specify --Use the replSet option to start mongoDB.--The basic syntax format of replSet is as follows:

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

Online Example

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

The above example will start a MongoDB instance named rs0, with the port number of27017

After starting, open the command prompt and connect to the mongoDB service.

Use the rs.initiate() command in the MongoDB client to start a new replica set.

We can use rs.conf() to view the configuration of the replica set

Use the rs.status() command to view the replica set status

Replica set member addition

To add a member to the replica set, we need to start the mongo service on multiple servers. Enter the MongoDB client and use the rs.add() method to add a member to the replica set.

Syntax

The basic syntax format of the rs.add() command is as follows:
>rs.add(HOST_NAME:PORT)

Online Example

Assuming you have started a mongod1.net, port number is27017MongoDB service. Use the rs.add() command in the client command window to add it to the replica set, as shown in the following command:

>rs.add("mongod1.net:27017")
>

In MongoDB, you can only add the MongoDB service to the replica set through the master node. You can use the command db.isMaster() to determine whether the MongoDB service currently running is the master node.

The replica set of MongoDB is different from the master-slave we commonly see. When the master node fails, all services will stop, but when the master node fails in a replica set, the replica will take over the master node and become the master node, and there will be no downtime.