Docs HomeMongoDB Manual

Adjust Priority for Replica Set Member调整副本集成员的优先级

Overview概述

The priority settings of replica set members affect both the timing and the outcome of elections for primary. 副本集成员的priority设置会影响主选举的时间和结果。Higher-priority members are more likely to call elections, and are more likely to win. 优先级较高的成员更有可能举行选举,也更有可能获胜。Use this setting to ensure that some members are more likely to become primary and that others can never become primary.使用此设置可确保某些成员更有可能成为主要成员,而其他成员则永远不会成为主要成员。

The value of the member's priority setting determines the member's priority in elections. 成员的priority设置值决定成员在选举中的priorityThe higher the number, the higher the priority.数字越高,优先级就越高。

Considerations注意事项

To modify priorities, you update the members array in the replica configuration object. The array index begins with 0. 要修改优先级,请更新复制副本配置对象中的members数组。数组索引以0开头。Do not confuse this index value with the value of the replica set member's members[n]._id field in the array.不要将此索引值与数组中的副本集成员的members[n]._id字段的值混淆。

The value of priority can be any floating point (i.e. decimal) number between 0 and 1000. priority的值可以是01000之间的任何浮点(即十进制)数字。The default value for the priority field is 1.priority字段的默认值为1

To block a member from seeking election as primary, assign it a priority of 0. 若要阻止成员作为主要成员寻求选举,请为其分配优先级0Hidden members and delayed members have priority set to 0.隐藏成员延迟成员的优先级设置为0。

Arbiters have priority 0.仲裁员的优先级为0

Adjust priority settings during a scheduled maintenance window. 在计划维护窗口期间调整优先级设置。Reconfiguring priority can force the current primary to step down, leading to an election. 重新配置优先级可能会迫使当前的初选下台,从而导致选举。Before an election, the primary closes all open client connections.在选举之前,主服务器会关闭所有打开的客户端连接。

Priority and Votes优先级和投票

members[n].priority and members[n].votes have the following relationship:具有以下关系:

As such, increasing a non-voting member's priority requires setting votes to 1 and increases the number of voting replica set members. 因此,增加无投票权成员的priority需要将votes设置为1,并增加有投票权的副本集成员的数量。Before increasing the priority of a non-voting member, consider the following:在增加无表决权成员的优先权之前,请考虑以下事项:

  • MongoDB replica sets can have no more than 7 voting members. MongoDB副本集的投票成员不能超过7个If the replica set already has 7 voting members, you cannot modify the priority of any remaining members in the replica set to be greater than 0.如果副本集中已有7个投票成员,则不能将副本集中任何剩余成员的优先级修改为大于0
  • Starting in MongoDB 4.4, replica reconfiguration can add or remove no more than one voting member at a time. To change multiple non-voting members to have a priority greater than 0, issue a series of replSetReconfig or rs.reconfig() operations to modify one member at a time. 从MongoDB 4.4开始,副本重新配置一次可以添加或删除不超过一个投票成员。要将多个无投票权成员更改为优先级大于0,请执行一系列replSetReconfigrs.reconfig()操作,一次修改一个成员。See Reconfiguration Can Add or Remove No More than One Voting Member at a Time for more information.有关详细信息,请参阅重新配置一次最多可添加或删除一个投票成员

Procedure过程

Warning
  • The rs.reconfig() shell method can force the current primary to step down, which causes an election. rs.reconfig()shell方法可以强制当前的primary退出,从而导致选举。When the primary steps down, the mongod closes all client connections. While this typically takes 10-20 seconds, try to make these changes during scheduled maintenance periods.当primary关闭时,mongod会关闭所有客户端连接。虽然这通常需要10-20秒,但请尝试在计划维护期间进行这些更改。
  • Avoid reconfiguring replica sets that contain members of different MongoDB versions as validation rules may differ across MongoDB versions.避免重新配置包含不同MongoDB版本成员的副本集,因为验证规则可能因MongoDB版本而异。
1

Copy the replica set configuration to a variable.将复制副本集配置复制到变量中。

In mongosh, use rs.conf() to retrieve the replica set configuration and assign it to a variable. mongosh中,使用rs.conf()检索副本集配置并将其分配给一个变量。For example:例如:

cfg = rs.conf()
2

Change each member's priority value.更改每个成员的优先级值。

Change each member's members[n].priority value, as configured in the members array.根据members数组中的配置,更改每个成员的members[n].priority值。

cfg.members[0].priority = 0.5
cfg.members[1].priority = 2
cfg.members[2].priority = 2

This sequence of operations modifies the value of cfg to set the priority for the first three members defined in the members array.此操作序列修改cfg的值,以设置members数组中定义的前三个成员的优先级。

3

Assign the replica set the new configuration.为复制副本集分配新配置。

Use rs.reconfig() to apply the new configuration.使用rs.reconfig()应用新配置。

rs.reconfig(cfg)

This operation updates the configuration of the replica set using the configuration defined by the value of cfg.此操作使用cfg值定义的配置更新复制副本集的配置。