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

On this page本页内容

Overview概述

The priority settings of replica set members affect both the timing and the outcome of elections for primary. 复制集成员的priority设置影响primary选举的时间和结果。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. 要修改优先级,请更新副本配置对象中的members数组。The array index begins with 0. 数组索引以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.隐藏成员延迟成员priority设置为0

Changed in version 3.6.在版本3.6中更改

Starting in MongoDB 3.6, arbiters have priority 0. 从MongoDB 3.6开始,仲裁器的优先级为0When you upgrade a replica set to MongoDB 3.6, if the existing configuration has an arbiter with priority 1, MongoDB 3.6 reconfigures the arbiter to have priority 0.当您将副本集升级到MongoDB 3.6时,如果现有配置具有优先级为1的仲裁器,MongoDB 3.6会将仲裁器重新配置为优先级为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 priorityrequires 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:在增加无表决权成员的优先权之前,请考虑以下事项:

Procedure过程

Warning警告
  • The rs.reconfig() shell method can force the current primary to step down, which causes an election. rs.reconfig()shell方法可以强制当前的初选下台,从而导致选举When the primary steps down, the mongod closes all client connections. 当主服务器关闭时,mongod关闭所有客户端连接。While this typically takes 10-20 seconds, try to make these changes during scheduled maintenance periods.虽然这通常需要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值定义的配置更新副本集的配置。

←  Member Configuration TutorialsPrevent Secondary from Becoming Primary →