In the use of MongoDB, in order to ensure the security of data, it is necessary to consider data backup and failure recovery, and for performance reasons, the ability to read and write separation will certainly improve database performance.
So, here's an introduction to the replica set in this article. Replica set (Replica set) is a master-slave cluster with automatic fault recovery capability.
A replica set has one master node and one or more backup nodes. When the primary node fails, the backup node chooses a new backup node by voting.
Replica set initialization
Here are some concrete practices to feel the replica set.
Start the three MongoDB servers with the following three commands first. Where the "replset" option specifies the name of the replica set.
Mongod.exe--dbpath="c:\mongodb\db\node1" --port=11111 --replset myreplset-- oplogsize=--dbpath="c:\mongodb\db\node2" --port=22222 -- Replset myreplset--oplogsize=--dbpath="c:\mongodb\db\node3" --port=33333 --replset myreplset--oplogsize=
As you can see from the server's output, we now need "rs.initiate ()" To initialize the replica set.
Here we do the initialization of the replica set, mainly the following steps:
- First execute "mongo–port=11111" on a server connected to "port=11111" via a mongodb shell
- Then define the configuration information "Myreplset_config"
- Finally initialize the replica set with "Rs.initiate (Myreplset)"
After the initialization command is complete, you can view the status of the replica set through the Rs.status () command.
In the above output, there are several fields to note:
- "Health": This indicates whether the state of the node is normal
- "State": This field indicates what type of node the node is.
- "Statestr": Indicates the state of the current synchronization, recovering indicates that the data is being synchronized, secondary indicates that it has been successfully synchronized and can be used normally
Now, stop the "port=11111" node with the "CTRL + C" command, and then open a new CMD window to connect to the "port=22222" server.
Use the Rs.status () command again to view the status of the replica set. You can see that the "health" status of "port=11111" becomes 0, while the "port=22222" server becomes the primary node.
If we start "port=11111" again, it will become a backup node, and this is not a demonstration.
Adding delete nodes dynamically
There are times when there is a need to add a new node, then you can use the following command for dynamically adding a delete node
1 mongod.exe--dbpath="c:\mongodb\db\node4" --port=44444 --replset Myreplset--oplogsize=2 rs.add ("127.0.0.1:44444") 3rs.status ()4 rs.remove ("127.0.0.1:44444") 5 rs.status ()
Read/write separation
Replica sets not only guarantee the security of the data, but also can reduce the reading data pressure of the primary node through read and write separation.
Then the above example inserts some data through the master node.
When we want to read the data through the backup node, we get the following error, prompting us that the "Slaveok" property is false.
However, with the Rs.slaveok () command, you can set the read operation for the backup node.
The reading and writing of the master node can be greatly constructed by the read-write separation, but there are some problems in this way, the data of the backup node is relatively lag, so the data read may not be up to date.
Summarize
With this article, I've probably learned about the use of replica sets. By using a replica set, the data can be further protected, and read-write separation can also be used to optimize the performance of reading and writing.
Ps: All the commands in this article can refer to the following links
Http://files.cnblogs.com/wilber2013/replicaSet.js
MongoDB Replica Set