Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of copying elections in MongoDB?

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you about the principle of copying election in MongoDB. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Election principle of MongoDB replication set principle of MongoDB replication

The replication of MongoDB is based on the operation log oplog, which is equivalent to the binary log in MySQL, recording only the records that have changed. Replication is the process of synchronizing and applying the oplog logs of the master node to other slave nodes.

The principle of MongoDB election

MongoDB nodes are divided into three types: standard node (host), passive node (passive) and arbitration node (arbiter).

Only the standard node can be elected as the active node (master node) and have the right to vote. The passive node has a complete copy, it is impossible to become an active node and has the right to vote. Arbitration nodes do not copy data, can not become active nodes, only the right to vote. To put it bluntly, only the standard node can be elected as the primary node, even if some standard nodes are down in a replication set, the passive node and arbitration node will not become the primary node. There are subsequent examples to demonstrate verification.

The difference between the standard node and the passive node: the high priority value is the standard node, and the low value is the passive node.

The election rule is that the person with a high number of votes wins, and priority is the value of priority 0000000, which is equivalent to an additional number of 01000 votes. Election result: the person with the highest number of votes wins; if the number of votes is the same, the newcomer wins.

As shown in the inter-node election for MongoDB replication set

Previous link

Yum installation MongoDB and database management

Configure MongoDB replication set

The example verifies the replication set election principle, creates four instances, creates the path file and dbpath file directory for the new instance, and sets the permission mkdir-p / data/mongodb/logs// path directory for the log file, because there will be an instance after the installation is complete. So here we only need to create three instances to mkdir-p / data/mongodb/mongo {2Magol 3diem 4} / / create the dbpath directory touch / data/mongodb/logs/mongod {2je 3 4} .log / / create log file chmod / data/mongodb/logs/*.log / / grant permissions to log files to modify the configuration file (/ etc/mongod.conf) path:dbPath: port: bindIp:replication: replSetName: / / modify the values of these five items in the configuration file

Turn off the firewall and selinux firewall systemctl disable firewalld.servicesystemctl stop firewalld.servicesetenforce 0 start the instance mongod-f / etc/mongod.conf configure replication set to create a replication set consisting of four nodes, and set the priority when creating

This replication set consists of four nodes, including two standard nodes, a passive node and an arbitration node.

Mongo / = enter the database chen= {"_ id": "chenrs", "members": [{"_ id": 0, "host": "172.16.10.29 id 27017", "priority": 100}, {"_ id": 1, "host": "172.16.10.29 id 27018", "priority": 100}, {"_ id": 2, "host": "172.16.10.29Soviet 27019", "priority": 0}, {"_ id": 3) "host": "172.16.10.29 true 27020", "arbiterOnly": true}]}

Initialize replication set rs.initiate (chen) to view the identity of each node rs.isMaster ()

View oplpog Log

At this point, add, delete, modify and query the database. Note that the query statements will not be recorded in the oplog log, only the statements that make changes to the database will be recorded.

Use stady / / use the stady library db.abc.insert ({"id": 1, "name": "zhangsan"}) / / to write a data db.abc.insert ({"id": 2) to the table abc "name": "lisi"}) / / insert a data db.abc.find () / / View the contents of the table db.abc.update ({"id": 2}) {$set: {"name": "jack"}) / / modify table data db.abc.remove ({"id": 1} / / Delete table data View oplog content use localshow collections / / you can see that there is an oplog.rs file db.oplog.rs.find () / / due to too much content It is not shown here. After looking at it, you will find that there is no record of the query. Only the operation of recording changes to the database simulates the failure of the primary node kill-9 + process number / / shuts down the primary node mongod-f / etc/mongod4.conf-- shutdown / / closes the primary node mongo-- port 27018chenrs:PRIMARY > rs.status () / / at this time the node has become the primary node {"set": "chenrs" "date": ISODate ("2018-07-16T05:30:04.152Z"), "myState": 1, omit some contents "members": ["_ id": 0, "name": "172.16.10.29 16T05:30:04.152Z 27017", "health": 0, / / health value is 0 In inactive state "state": 8, "stateStr": "(not reachable/healthy)", omitting some contents "_ id": 1, "name": "172.16.10.29 id 27018", "health": 1, "state": 1 "stateStr": "PRIMARY", / / at this time 1 is the master node omitting part of the content "_ id": 2, "name": "172.16.10.29 name", "health": 1, "state": 2, "stateStr": "SECONDARY" Omit part of the content "_ id": 3, "name": "172.16.10.29 health", "health": 1, "state": 7, "stateStr": "ARBITER" Simulate another primary node failure mongod-f / etc/mongod2.conf-- shutdownmongo-- port 27019chenrs:SECONDARY > rs.status () / / still in secondary status {"set": "chenrs", "date": ISODate ("2018-07-16T05:40:09.740Z"), "members": [{"_ id": 0 "name": "172.16.10.29 stateStr 27017", "health": 0, "state": 8, "stateStr": "(not reachable/healthy)", omitting part of the content "_ id": 1, "name": "172.16.10.29Switzerland 27018" "health": 0, "state": 8, "stateStr": "(not reachable/healthy)", omitting some contents "_ id": 2, "name": "172.16.10.29 id 27019", "health": 1, "state": 2 "stateStr": "SECONDARY", / / even if both standard nodes are down The passive node and the arbitration node did not become the primary node to omit part of the content "_ id": 3, "name": "172.16.10.29 health 27020", "health": 1, "state": 7, "stateStr": "ARBITER" Omit part of the content MongoDB replication set management configuration allows data to be read from the node

The slave node of the default MongoDB replication set cannot read data, but secret commands can be used to allow data to be read from the node.

Rs.slaveOk () / / allows the slave node to read data and view replication set status information rs.help rs.printReplicationInfo () / / View the size and time range of the oplog log file rs.printSlaveReplicationInfo () / / query the node and the time of node replication

Resize oplog log files

If the node is a member of the replica set, it is not allowed to change the size of the oplog at this time, so move the node out of the replication set and want to change the default size of the log file. What you need to do at this time is to first shut down the service of the node, and then exit the replication set

Turn off the node service (offline upgrade) use admin / / do db.shutdownServer () / / disable the service on the slave node of the replication set. If you want to log in to the node again, the node will fail and exit the replication set.

Log out the value of replication and modify the port value to start it as a single instance

Vim / etc/mongod2.conf#replication:# replSetName: chenrsport: 27028mongod-f / etc/mongod2.conf / / start the instance At this point, the instance does not belong to the replication set full backup oplog log mongodump-- port 27028-- db local-- collection 'oplog.rs' / / in the linux interface to delete the oplog file > use local > db.oplog.rs.drop () rebuild the oplog log db.runCommand ({create: "oplog.rs", capped: true, size: (2 * 1024 * 1024 * 1024)}) once again close the node service use admindb.shutdownServer () to add the node to the replication set

Callback parameter, and add a parameter to specify the log file size

Vim / etc/mongod2.confreplication: replSetName: chenrs oplogSizeMB: 2048 / / in megabytes-f / etc/mongod2.conf / / start instance mongo-port 27018 / / enter the instance

View oplog size rs.printReplicationInfo ()

For the change in log file size, it only takes effect on this node, and the other nodes are still the default

Deploy certified replication to create administrative user use admindb.createUser ({"user": "root", "pwd": "123123", "roles": ["root"]})

Configure key authentication

To enable other nodes to synchronize with the primary node, create a key file to enable other nodes to synchronize

Create a validation file

# cd / usr/bin/# echo "chenrskey" > chenrskey1# echo "chenrskey" > chenrskey2# echo "chenrskey" > chenrskey3# echo "chenrskey" > chenrskey4 / / key content customization, but to ensure the consistency of the content # chmod 600 chenrskey {1.. 4} / set file permissions, if not set, an error will be reported in the next startup

Modify the configuration file and enable the security verification function of mongodb (all four configuration files need to be modified, pay attention to the differences in content)

Vim / etc/mongod.confsecurity:keyFile: / usr/bin/chenrskey1 / / the verification file of each node is different. ClusterAuthMode: keyFile / / authentication type and key file authentication should be modified according to different node.

Restart the service

Mongod-f / etc/mongod.conf-- shutdownmongod-f / etc/mongod.conf / other servers are all restarted in the same way, and can be authenticated and logged in after repeated operations (verify the master first, then verify the slave)

When you directly use the login command to log in to the system, you cannot view the data using show dbs, so you need to use authentication.

Mongo-port 27018use admindb.auth ("root", "123123")

These are the principles of copying elections in the MongoDB shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report