MongoDB replication is the process of synchronizing data across multiple servers.
Replication provides redundant backups of data, stores copies of data on multiple servers, improves data availability, and guarantees data security.
Replication also allows you to recover data from hardware failures and service outages.
What is replication?
- Secure your data
- Data high Availability (24*7)
- Disaster recovery
- No downtime maintenance (e.g. backup, rebuild index, compression)
- Distributed Read data
MongoDB Replication principle
MongoDB requires a minimum of two nodes for replication. One is the master node, which handles client requests, and the rest is the slave node, responsible for replicating the data on the master node.
MongoDB each node common collocation method is: A master one from, a master many from.
The master node records all operations on it, oplog the primary node periodically from the node, and then performs these operations on its own copy of the data, ensuring that the data from the node is consistent with the primary node.
The MONGODB replication structure diagram looks like this:
The above structure diagram total, the client total master node reads the data, the client writes the data to the master node is, the master node and the slave node carries on the data interaction safeguard data consistency.
Replica set Features:
- N-node clusters
- Any node can be used as the master node
- All write operations are on the primary node
- Auto Fail-Over
- Automatic recovery
MongoDB Replica Set settings
In this tutorial we use the same mongodb to do the MongoDB master-slave Experiment, the following steps:
1. Close the running MongoDB server.
Now we start MongoDB by specifying the--replset option. The--replset basic syntax format 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 a port number of 27017.
After startup, open a command prompt and connect to the MongoDB service.
The MONGO client uses command Rs.initiate () to start a new replica set.
We can use rs.conf () to view the configuration of the replica set
View replica set posture using the Rs.status () command
Replica set Add Member
To add members of 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 members of the replica set.
Grammar
The basic syntax format for the Rs.add () command is as follows:
>rs.add (Host_name:port)
Instance
Suppose you have started a MONGO service named Mongod1.net with a port number of 27017. In the Client Command window, use the Rs.add () command 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 MONGO service to the replica set through the master node, judging whether the currently running MONGO service is able to use command Db.ismaster () for the master node.
MongoDB replica set is different from our common master-slave, master and slave after the host outage all services will stop, and replica set after the host outage, the replica will take over the master node becomes the primary node, there will be no downtime.
MongoDB replication (Replica set)