MongoDB replication is the process of synchronizing data across multiple servers.
Replication provides a redundant backup of data, storing copies of data on multiple servers, increasing the availability of data, and guaranteeing data security.
Replication also allows you to recover data from hardware failures and service outages. What is replication? Secure data security data high availability (24*7) disaster recovery without downtime maintenance (such as backup, rebuild index, compression) distributed read Data MongoDB copy principle
MongoDB replication requires at least two nodes. One is the master node, which handles client requests, and the rest is from the node, responsible for replicating the data on the master node.
MongoDB each node common collocation way is: one main one from, one master more from.
The master node records all operations on it, oplog the primary node from the node periodically, 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 chart looks like this:
The above structure chart total, the client main node reads the data, writes the data to the main node in the client, the main node and from the node carries on the data mutual guarantee data consistency. Replica set Features: N-node clusters any node can be the primary node all writes are automatic failover automatic recovery MongoDB replica set settings on the primary node
In this tutorial we use the same mongodb to do MongoDB master-slave experiments, the following steps:
1, close the running MongoDB server.
Now we start the 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 starts a MongoDB instance named Rs0 with a port number of 27017.
Open the Command Prompt box and connect to the MongoDB service after startup.
The MONGO client uses the 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 Add members using the Rs.status () command replica set
To add a member 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 the members of the replica set. The syntax rs.add () command has the following basic syntax format:
>rs.add (Host_name:port)
Instance
Suppose you have started a MONGO service called 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 to determine whether the currently running MONGO service can use command Db.ismaster () for the primary node.
MongoDB's replica set is different from our common master and subordinate, all services will stop after the host is down, and the replica set will take over the main node as the master node after the host is down, and no downtime will occur.