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

How to configure replica set in MongoDB

2025-03-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to configure replica set in MongoDB, for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Environmental preparation

Three servers with the following addresses:

192.168.248.128192.168.248.135192.168.248.136

Follow the steps described above to build a replica set environment, which I will not repeat here.

Add and delete members of replica set

After the replica set environment is set up, we can delete a replica set member with the following command:

Rs.remove ('192.168.248.128 rs.remove 27017')

After the above command is executed, we can use the rs.status () command to see if the deletion is successful, or we can add a member to the replica set with the following command:

Rs.add ('192.168.248.128 rs.add 27017')

Of course, the replica set can also be updated by using the reconfig command, as follows:

First, define config as follows:

Config= {_ id: "rs", members: [{_ id:3,host: "192.168.248.128"}, {_ id:1,host: "192.168.248.135"}]}

Then perform the update operation:

Rs.reconfig (config)

We can also use config=rs.config () to get the original config file, modify it, and then execute rs.reconfig (config), as follows:

Config=rs.config () config.members [0] .host = "192.168.248.136" rs.reconfig (config) elect arbitrator

In the above, we demonstrated to the partners what happens after the master node dies. Unlike other (such as Redis) database master-slave replication, the MongoDB master node will automatically select a new master node from the backup node after it hangs up. This is a process of election and voting, but if the number of backup nodes is even, there may be equal votes between the two servers, in order to avoid this problem. We generally have two solutions:

1. The number of data nodes is odd, which avoids the problems described above.

two。 Use the election arbitrator, which is a special member. The arbitrator does not save data or provide services to the client, but comes out to vote in the event of a stalemate. There can be at most one arbitrator in a copy set.

Election arbitrators consume very little system resources, so there are few requirements for the performance of the deployed server, and the way to add arbitrators to the replica set is as follows:

Rs.addArb ('192.168.248.128 rs.addArb 27017')

You can also use the reconfig we mentioned earlier to operate:

Config=rs.config () config.members [2] = {_ id:2,host:'192.168.248.128',arbiterOnly:true} rs.reconfig (config)

After the addition is completed, we can use the rs.status () command to check whether the addition is successful. If we see the following, it means that the addition is successful:

{"_ id": 2, "name": "192.168.248.128 ISODate 27017", "health": 1, "state": 7, "stateStr": "ARBITER", "uptime": 2, "lastHeartbeat": ISODate ("2017-11-03T08:56:12.406Z"), "lastHeartbeatRecv": ISODate ("2017-11-03T08:56:08.417Z"), "pingMs": NumberLong (1), "configVersion": 8}

The removal of arbitrators is the same as the removal of ordinary nodes, and I will not repeat it here.

Priority problem

Priority is used to describe the priority of a backup node to become the primary node. The value range of priority is [0-100], and the default is 1. The higher the number, the higher the priority, the more likely it is to become the primary node. 0 means that the node can never become the primary node.

We can specify the priority when adding nodes, as follows:

Rs.add ({_ id:0,host:'192.168.248.128:27017',priority:2})

You can also set priorities for existing nodes:

Config=rs.config () config.members [0] .priority = 99rs.reconfig (config) this is the answer to the question about how to configure replica sets in MongoDB. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel for more related knowledge.

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

Internet Technology

Wechat

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

12
Report