Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MongoDB Replication (Replica Set)

MongoDB Replication (Replica Set)

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

Replication provides redundant backups of data and stores copies of data on multiple servers, improving data availability and ensuring data security.

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


What is replication?


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 of MongoDB nodes are: one primary with one secondary, one primary with multiple secondaries.

The primary node records all operations in its oplog; secondary nodes periodically poll the primary node to obtain these operations, and then execute these operations on their own data copies, thereby ensuring that the secondary nodes' data is consistent with the primary node.

The MongoDB replication structure diagram is shown below:

MongoDB replication structure diagram

In the above structure diagram, the client reads data from the primary node. When the client writes data to the primary node, the primary node interacts with the secondary nodes to ensure data consistency.

Replica set features:


MongoDB replica set setup

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

1. Shut down the running MongoDB server.

Now we start mongoDB by specifying the --replSet option. The basic syntax of --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 number 27017.

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

In the Mongo client, use the command rs.initiate() to start a new replica set.

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

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


Add members to the replica set

To add members to the replica set, we need to use multiple servers to start the mongo service. Enter the Mongo client and use the rs.add() method to add replica set members.

Syntax

The basic syntax of the rs.add() command is as follows:



>rs.add(HOST_NAME:PORT)

Example

Suppose you have started a Mongo service named mongod1.net with port number 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 whether the currently running Mongo service is the primary node, you can use the command db.isMaster().

MongoDB replica sets differ from the common master-slave setup. In master-slave, all services stop after the host goes down, whereas in a replica set, after the host goes down, a replica takes over the primary node and becomes the primary node, so there is no downtime.