First, the principle of the election of MongoDB copy set
Replication is based on the operation Log Oplog, which is equivalent to the binary log in MySQL and records only the changes that have occurred. Replication is the process of applying the Oplog log synchronization of the master node to other slave nodes.
The nodes of the replica set are elected. The node types are divided into standard nodes, passive nodes, and quorum nodes.
(1) Only standard nodes may be elected as active nodes and have the right to vote. The passive node has a complete copy, cannot become an active node, and has the right to vote. The quorum node does not replicate data and cannot become an active node, only the right to vote.
(2) The higher priority value is the standard node, the lower is the passive node.
1. Configure the replication Set priority
Configure a replication set of 4 nodes, set two standard nodes, one passive node, and one quorum node.
> cfg={"_id":"kgcrs","members":[{"_id":0,"host":"192.168.113.177:27017","priority":100},{"_id":1,"host":"192.168.113.177:27018","priority":100},{"_id":2,"host":"192.168.113.177:27019","priority":0},{"_id":3,"host":"192.168.113.177:27020","arbiterOnly":true}]}> rs.initiate(cfg) //启动复制集kgcrs:PRIMARY> rs.isMaster() //查看节点状态
2. Operate the data on the master node and query the Oplog log
kgcrs:PRIMARY> use kgckgcrs:PRIMARY> db.t1.insert({"id":1,"name":"tom"})kgcrs:PRIMARY> db.t1.insert({"id":2,"name":"jerry"})kgcrs:PRIMARY> db.t1.find()kgcrs:PRIMARY> db.t1.update({"id":2},{$set:{"name":"jack"}})kgcrs:PRIMARY> db.t1.remove({"id":1})
You can view the Oplog log in the local database of MongoDB.
kgcrs:PRIMARY> use localkgcrs:PRIMARY> show collections oplog.rs //显示kgcrs:PRIMARY> db.oplog.rs.find()
3. Simulating primary node failure
If the primary node fails, another standard node will be elected as the new master node.
[[email protected] ~]# mongod -f /etc/mongod.conf --shutdownkilling process with pid: 6653[[email protected] ~]# mongo --port 27018kgcrs:PRIMARY> rs.status()
4. Simulate all standard node failures
If all standard nodes fail, the passive node cannot become the primary node.
[[email protected] ~]# mongod -f /etc/mongod2.conf --shutdownkilling process with pid: 6685[[email protected] ~]# mongo --port 27019kgcrs:SECONDARY> rs.status()
II. MongoDB Replication Set Management
Replication set management includes configuration from which nodes can read data, view replica set status information, change the oplog size, and configure a replica set with authentication.
1. Configuration allows data to be read from the node.
The slave node of the default MongoDB replica set cannot read data, and you can use the Rs.slaveok () command to allow data to be read from the node.
Restart two standard nodes, connect to one of the replica sets from the node, and configure it to allow read data.
[[email protected] ~]# mongo --port 27018kgcrs:SECONDARY> show dbs
kgcrs:SECONDARY> rs.slaveOk()kgcrs:SECONDARY> show dbs
2. View replication status information
You can use the Rs.printreplicationinfo () and Rs.printslavereplicationinfo () commands to view the status of a replica set.
kgcrs:SECONDARY> rs.help()kgcrs:SECONDARY> rs.printReplicationInfo()configured oplog size: 990MBlog length start to end: 2974secs (0.83hrs)oplog first event time: Tue Jul 17 2018 14:36:27 GMT+0800 (CST)oplog last event time: Tue Jul 17 2018 15:26:01 GMT+0800 (CST)now: Tue Jul 17 2018 15:26:02 GMT+0800 (CST)kgcrs:SECONDARY> rs.printSlaveReplicationInfo()source: 192.168.113.177:27018 syncedTo: Tue Jul 17 2018 15:26:11 GMT+0800 (CST) -10 secs (0 hrs) behind the primary source: 192.168.113.177:27019 syncedTo: Tue Jul 17 2018 15:26:01 GMT+0800 (CST)
3. Change the Oplog size
Take the example of modifying the Oplog size from node 27018.
(1) Exit the replication set to start with a single instance
kgcrs:SECONDARY> use adminkgcrs:SECONDARY> db.shutdownServer()
In profile logoff replication: Related startup parameters, and modify port port number 27028
vim /etc/mongod2.conf # replication: # replSetName: kgcrs port: 27028:
Start a 27028 node with a single instance
[[email protected] ~]# mongod -f /etc/mongod2.confabout to fork child process, waiting until server is ready for connections.forked process: 9954child process started successfully, parent exiting
(2) Create a new Oplog record, configure its size
Fully prepare all Oplog records for the current node
[[email protected] ~]# mongodump --port 27028 --db local --collection ‘oplog.rs‘2018-07-17T15:54:20.600+0800 writing local.oplog.rs to 2018-07-17T15:54:20.604+0800 done dumping local.oplog.rs (364 documents)
Enter instance 27028 to create a new Oplog collection.
[[email protected] ~]# mongo --port 27028> use local> db.oplog.rs.drop()> db.runCommand( { create: "oplog.rs", capped: true, size: (2 * 1024 * 1024 * 1024) } )
Re-add the single instance 27028 to the replication set, modify the configuration file
> use admin > db.shutdownServer() //关掉服务vim /etc/mongod2.conf port: 27018 //端口号重新改回27018replication: replSetName: kgcrs oplogSizeMB: 2048 //oplog的大小2048M
Restart the 27018 instance and enter the instance
To make instance 27018 the master node, you need to exit the primary node 27017 for the election.
4. Deployment of Certified Replication
(1) Create user authentication on the master node
kgcrs:PRIMARY> use adminkgcrs:PRIMARY> db.createUser({"user":"root","pwd":"123","roles":["root"]})
(2) Edit 4 configuration files to set the key file
vim /etc/mongod.confsecurity: keyFile: /usr/bin/kgcrskey1 clusterAuthMode: keyFile
vim /etc/mongod2.confvim /etc/mongod3.confvim /etc/mongod4.conf
(3) Generate a key file for 4 instances and set permissions
[[email protected] ~]# cd /usr/bin/[[email protected] bin]# echo "kgcrs key"> kgcrskey1[[email protected] bin]# echo "kgcrs key"> kgcrskey2[[email protected] bin]# echo "kgcrs key"> kgcrskey3[[email protected] bin]# echo "kgcrs key"> kgcrskey4[[email protected] bin]# chmod 600 kgcrskey{1..4}
(4) Restart 4 instances in turn
(5) Go to the master node to view the database and replication sets
kgcrs:PRIMARY> show dbs kgcrs:PRIMARY> rs.status()
No identity login verification, direct view, the results are as follows:
kgcrs:PRIMARY> use admin #身份登录验证kgcrs:PRIMARY> db.auth("root","123")
After signing in, review the results as follows:
MongoDB copy election principle and replica set management