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

Mongodb replica set model article

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

Share

Shulou(Shulou.com)06/01 Report--

Copy the set framework composition:

Three-node replicaset configuration installation (no quorum node):

Replica set [10.10.20.161-10.10.20.163]

10.10.20.161-10.10.20.163 these three are installed as follows:

Mkdir-p/u01/mongodb/data/replset/

Mkdir-p/u01/mongodb/log/

/ usr/local/mongodb/bin/mongod--replSet myrepl-fork-port 40001-dbpath / u01/mongodb/data/replset/--logpath / u01/mongodb/log/replset.log-logappend-oplogSize=4096

Then set up a cluster, log in to the mongo of whichever server you want to be the master, and then add the following configuration

/ usr/local/mongodb/bin/mongo-port 40001

Config = {_ id: 'myrepl', members: [{_ id: 0, host:' 10.10.20.161 host:' 40001'}, {_ id: 1, host:'10.10.20.162:40001'}, {_ id: 3, host:' 10.10.20.163Freight 40001'}]}

Rs.initiate (config)

After configuration, exit and login will display the word primary.

/ usr/local/mongodb/bin/mongo-port 40001

MongoDBshell version: 2.2.2

Connectingto: 127.0.0.1:40001/test

Myrepl:PRIMARY >

The other two show myrepl:SECONDARY.

Three-node replicaset configuration installation (with arbitration node):

The advantage of three nodes without arbitration is that one master is written and two slave are read. The disadvantage is that I only have two machines, and I also want to do replica set, so there is a problem. If there are only two machines, if master goes down, there is only one slave, and the voting algorithm cannot be carried out, so slave cannot be upgraded to master only, but if there is an arbitration node, the switch can be completed, and the arbitration node is only responsible for voting and does not need to synchronize data.

Adding an arbitration node is also very simple.

After configuring the 1 master and 1 slave node, add rs.add ('10.10.20.163virtual 40001slave true) or use rs.addArb (' 10.10.20.163virtual 40001')

Can also be added when configuring for the first time

Config = {_ id: 'myrepl', members: [{_ id: 0, host:' 10.10.20.161 host:' 40001'}, {_ id: 1, host:'10.10.20.162:40001'}, {_ id: 3, host:' 10.10.20.163Vera 40001mm, arbiterOnly: true}]}

Rs.initiate (config)

Read-write separation:

When Replica set is done by default, slave is not open and can only be used for failover, which is wasteful. In order to share the pressure on the host, slave should be made readable by executing rs.slaveOk () on the slave server.

Manually switch master:

If there is a problem with the current master and we want to downgrade it to slave, it's very simple, we just log in to master and execute rs.stepDown (10), which means that we will not participate in the election of master for 10 seconds, and after execution, the copy set will select one of the other slave to promote to master. In fact, another way to manually switch master in addition to this command is to be more complicated, that is, modify the config, set the priority, and then reload. There is also a command that sometimes uses rs.freeze (10) on slave, which means that we will not participate in the election of master for 10 seconds. If we are switching master manually, we do not want some slave to do master.

Replica set related commands:

Rs.status (): view replication set status information

Rs.conf (): view replication set configuration information

Deletion and addition of replica set nodes:

Suppose we now need to maintain one of the nodes 10.10.20.163, then we need to execute the following command

Log in to master

Execute rs.remove ("10.10.20.163VR 40001")

Wait until the maintenance is finished and then add the node with rs.add ("10.10.20.163virtual 40001"). However, if the oplog has been overwritten for too long, the synchronization of 10.10.20.163 will stop after adding the node with rs.add. And there will be an error Thu Feb 28 09:58:00 [rsBackgroundSync] replSet not trying to sync from 10.10.20.162 replSet not trying to sync from 40001, it isvetoed for 133 more seconds.

Replica set node synchronization failure handling method:

Following the above topic, if there is a problem with synchronization, there are two ways to solve it.

Remove a node with good synchronization, and then scp the data directory and start it again.

Delete the data directory of the out-of-sync node, then start it, and mongodb will automatically synchronize it for you.

When will the handover of replicaset fail:

This is a very important knowledge point, that is, when the number of replica set member nodes exceeds half, PRIMARY will automatically be downgraded to SECONDARY, and the cluster can only be read-only. In order to prevent the cluster from being divided into multiple blocks and selecting multiple masters due to network reasons. Therefore, in order to ensure that the remaining machines can still be switched to the main, multiple arbitration nodes can be added.

For example: 1 master + 2 slave + 1 arbitration

There are a total of 4 nodes, half of which is 2, so if the two slave libraries are down, that master will be automatically downgraded to slave. At this time, the replica set is not writable, so it is not available.

Then to solve this problem, we can use: 1 master + 2 slave + 2 arbitration, so that the two slave libraries can not reach half of the total number of nodes, so it will not affect the cluster.

Modify the synchronization source of slave (what's new in mongo 2.2):

After the cluster is set up, generally speaking, slave is synchronized from master. If there is a lot of slave, master is under a lot of pressure. We can ask some slave to synchronize data from other slave, and use the rs.syncFrom ("10.10.20.163slave 40001") statement to synchronize the slave. Note that this setting will become invalid if the slave is rebooted.

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