MongoDB Replica set: (There are two cases, master-slave replication, replica set)
I'm going to tell you this time. MongoDB replica set, the occurrence of replica set can effectively guarantee MongoDB single point problem, to the robustness of data has certain auxiliary function:
MongoDB replication: Is the process of synchronizing data across multiple servers.
1. Replication provides redundant backup of data and stores copies of data on multiple servers, improving data availability and ensuring data security.
2. 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.
MongoDB Master-slave replication:
Since I have already loaded a MongoDB service locally, I have emulated two mongdb on-premises to initiate a data backup operation:
The 1> configuration path is as follows:
2> Enter the bin directory under each service to start MongoDB and specify the port: Server1 's corresponding master port = 27011
Mongod.exe--dbpath=e:\mongodbrs\db1--master--port=27011
Server2 is the copy of MongoDB port=27012 mongod.exe--dbpath=e:\mongodbrs\db2--port=27012--slave--source=127.0.0.1:27011
Always monitor after startup:
3> inserts a record on 27011:
You can see the change on 27012:
· Solution to the error:
Note: Concou only allow query, insert will error
4> Dynamic Append dependent database I'm impressed.
As described above, when MongoDB starts, I know what the main data is, and here's a way to add it dynamically:
Start MongoDB, do not specify the main library: Mongod.exe--dbpath=e:\mongodbrs\db3--port=27013--slave
The above information indicates that there is no master library configuration: The configuration is as follows:
View MONGODB3 service startup information to see: Slave is in effect.
To this dynamic append from the library is also configured to complete:
5> Read/write separation
This approach is implemented in a larger architecture, which is very simple in MongoDB, and, by default, the subordinate database does not support the reading of the data, but it doesn't matter,
The driver provides us with a read-dependent database called "Slaveokay" that allows us to display to alleviate the performance stress of the primary database
Comparison: one master two from
To the master-slave copy is basically finished, the following is the establishment and introduction of the replica set
Replica set for Mongdb:
Features of the replica set:
1> replica set does not have a specific primary database.
2> if the primary database is down, a subordinate database is chosen as the primary database on top of the cluster, which has the automatic failure recovery function.
Replica set in the construction process, to ensure that the data is empty, because I built the master-slave copy of the data are stored in the DB1 DB2 DB3, so in the building of the replica set, the first to clear the data, to ensure the clean environment of the replica set
Replica set: (Build command)
Mongod.exe--port 27011--dbpath=e:\mongodbrs\db1--replset rs0
Mongod.exe--port 27012--dbpath=e:\mongodbrs\db2--replset rs0
Mongod.exe--port 27013--dbpath=e:\mongodbrs\db3--replset rs0
Replica Set Build process:
Rs.status (): Query the status of the replica set:
6> just said that the replica set is not the primary node of the cluster, the main node after the outage of the other nodes will automatically switch back, the following I simulated this scenario, the 27011 kill:
7> I put 27011 nodes up again, and this time he returned to the cluster as a slave node:
At this point, the master-slave copy of MongoDB and the building of the replica set has been completed, will not be built can be consulted: http://www.runoob.com/mongodb/mongodb-replication.html
Getting Started with MongoDB (6) Replica set