翻译或纠错本页面

修改复制集节点的优先级

On this page

概述

复制集节点的 priority 参数的值决定了选举中该节点的优先级。值越高,优先级越高。

The value of the member’s priority setting determines the member’s priority in elections. The higher the number, the higher the priority.

Considerations

我们可以通过修改复制集配置参数中 members 数组位置的优先级来修改对应机器的优先级。数组索引从 0 开始。不要将数组下标与数组 _id 混淆。

The value of priority can be any floating point (i.e. decimal) number between 0 and 1000. The default value for the priority field is 1.

To block a member from seeking election as primary, assign it a priority of 0. Hidden members and delayed members have priority set to 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.

复制集节点的优先级影响了 elections 的结果。我们通过设置不同的优先级来提高部分节点成为主节点的可能性,也可以让某些节点不能成为主节点。

1

Copy the replica set configuration to a variable.

In the mongo shell, use rs.conf() to retrieve the replica set configuration and assign it to a variable. 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.

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.

3

Assign the replica set the new configuration.

Use rs.reconfig() to apply the new configuration.

rs.reconfig(cfg)

This operation updates the configuration of the replica set using the configuration defined by the value of cfg.