MongoDB replication (replica set)
MongoDB replication is the process of synchronizing data on multiple servers.
Replication provides redundant data backup and stores data copies on multiple servers. This improves data availability and ensures data security.
Replication also allows you to recover data from hardware faults and service interruptions.
What is replication?
- Ensure data security
- High data availability (24x7)
- Disaster recovery
- No downtime maintenance (such as backup, re-indexing, compression)
- Distributed Data Reading
MongoDB replication principles
Mongodb replication requires at least two nodes. One of them is the master node, which is responsible for processing client requests, and the others are slave nodes, which are responsible for copying data on the master node.
A common combination of mongodb nodes is one master, one slave, and one master, multiple slaves.
The master node records all the operations on the oplog, regularly polls the master node from the node to obtain these operations, and then executes these operations on its own data copies to ensure that the data on the slave node is consistent with that on the master node.
The MongoDB copy structure is as follows:
The preceding structure diagram shows that the master node of the client reads data and writes data from the client to the master node. Data Interaction Between the master node and the slave node ensures data consistency.
Replica set features:
- Clusters with N nodes
- Any node can act as the master node
- All write operations are performed on the master node.
- Automatic failover
- Automatic Recovery
MongoDB replica set settings
In this tutorial, we use the same MongoDB as the master-slave experiment. The procedure is as follows:
1. Shut down the running MongoDB server.
Now we can start mongoDB by specifying the -- replSet option. The basic syntax format of -- 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 starts a MongoDB instance named rs0 with the port number 27017.
After the startup, open the command prompt box and connect to the mongoDB service.
Run the rs. initiate () command on the Mongo client to start a new replica set.
We can use rs. conf () to view the replica set configuration.
View replica set posture using the rs. status () command
Add members to replica set
To add replica set members, 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.
Syntax
The basic syntax format of the rs. add () command is as follows:
>rs.add(HOST_NAME:PORT)
Instance
Suppose you have started a Mongo service named mongod1.net with the port number 27017. In the Client Command window, use the rs. add () command to add it to the replica set. The command is as follows:
>rs.add("mongod1.net:27017")>
In MongoDB, you can only add the Mongo service to the replica set through the master node to determine whether the currently running Mongo service is the master node. You can use the db. isMaster () command ().
MongoDB's replica set is different from our common master-slave mechanism. After the master-slave mechanism is down, all services will stop. When the master-slave mechanism is down, the replica set will take over the master node as the master node, no downtime occurs.
For more MongoDB tutorials, see the following:
CentOS compilation and installation of php extensions for MongoDB and mongoDB
CentOS 6 install MongoDB and server configuration using yum
Install MongoDB2.4.3 in Ubuntu 13.04
MongoDB beginners must read (both concepts and practices)
MongoDB Installation Guide for Ubunu 14.04
MongoDB authoritative Guide (The Definitive Guide) in English [PDF]
Nagios monitoring MongoDB sharded cluster service practice
Build MongoDB Service Based on CentOS 6.5 Operating System
MongoDB details: click here
MongoDB: click here
This article permanently updates the link address: