Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

MongoDB Replication (Replica Set)


May 17, 2021 MongoDB


Table of contents


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 keeping data secure.

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


What is replication?

  • Secure your data
  • High availability of data (24x7)
  • Disaster recovery
  • No downtime maintenance (e.g. backup, index rebuild, compression)
  • Distributed read data

MongoDB replication principle

The replication of mongodb requires at least two nodes. O ne of them is the primary node, which handles client requests, and the rest is from the node, which replicates the data on the primary node.

Mongodb nodes are commonly matched in such a way: one master from one, one master from one from.

The primary node logs all operations on it, periodically polls the primary node from the node to get these operations, and then performs these operations on its own copy of the data to ensure that the data from the node is consistent with the primary node.

The MongoDB replication structure diagram looks like this:

MongoDB Replication (Replica Set)

The above structure diagram total, the total client master node reads the data, in the client write data to the main node is, the main node and the data interaction from the node to ensure the consistency of the data.

Replica set features:

  • A cluster of N nodes
  • Any node can be used as the primary node
  • All writes are on the primary node
  • Automatic failover
  • Automatic recovery

MongoDB replica set settings

In this tutorial we use the same MongoDB to do the MongoDB master experiment, as follows:

1, shut down the running MongoDB server.

Now let's start mongoDB by specifying the --replSet option. The basic syntax format for --replSet is as follows:

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

Instance

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

The above instance launches a MongoDB instance named rs0 with port number 27017.

Open the command prompt box after startup and connect to the mongoDB service.

The Mongo client uses the command rs.ar beginning() to start a new replica set.

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

View the replica set pose using the rs.status() command


The replica set adds members

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

Grammar

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

Instance

Suppose you have started a Mongo mongod1.net port number 27017. The client command window uses the rs.add() command to add it to the replica set, as follows:

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

In MongoDB, you can only add the Mongo service to the replica set through the primary node to determine if the currently running Mongo service can use the command db.isMaster().

The mongoDB replica set is different from our common primary esod, where all services are stopped after the master goes down, and when the master goes down, the replica takes over the primary node as the primary node without downtime.