Prevent Secondary from Becoming Primary防止二级成为一级

On this page本页内容

Overview概述

In a replica set, by default all secondary members are eligible to become primary through the election process. 在副本集中,默认情况下,所有secondary都有资格通过选举过程成为primary。You can use the priority to affect the outcome of these elections by making some members more likely to become primary and other members less likely or unable to become primary.你可以利用priority来影响这些选举的结果,使一些成员更有可能成为初选,而其他成员不太可能或无法成为初选。

Secondaries that cannot become primary are also unable to trigger elections. 无法成为初选的副总统也无法引发选举。In all other respects these secondaries are identical to other secondaries.在所有其他方面,这些辅助设备与其他辅助设备相同。

To prevent a secondary member from ever becoming a primary in a failover, assign the secondary a priority of 0, as described here. 要防止secondary成员在故障切换中成为primary成员,请按此处所述为次要成员分配优先级0。For a detailed description of secondary-only members and their purposes, see Priority 0 Replica Set Members.有关仅辅助成员及其用途的详细说明,请参阅优先级0副本集成员

Considerations注意事项

When updating the replica configuration object, access the replica set members in the members array with the array index. 更新副本配置对象时,使用数组索引访问members数组中的副本集成员。The array index begins with 0. 数组索引以0开头。Do not confuse this index value with the value of the members[n]._id field in each document in the members array.不要将此索引值与members数组中每个文档中members[n]._id字段的值混淆。

Note注意

MongoDB does not permit the current primary to have a priority of 0. MongoDB不允许当前primary的优先级为0。To prevent the current primary from again becoming a primary, you must first step down the current primary using rs.stepDown().要防止当前主节点再次成为主节点,必须首先使用rs.stepDown()逐步降低当前主节点。

Procedure过程

This tutorial uses a sample replica set with 5 members.本教程使用具有5个成员的示例副本集。

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

Retrieve the current replica set configuration.检索当前副本集配置。

The rs.conf() method returns a replica set configuration document that contains the current configuration for a replica set.rs.conf()方法返回副本集配置文档,其中包含副本集的当前配置

In mongosh, when connected to a primary, run the rs.conf() method and assign the result to a variable:mongosh中,当连接到主节点时,运行rs.conf()方法并将结果分配给变量:

cfg = rs.conf()

The returned document contains a members field which contains an array of member configuration documents, one document for each member of the replica set.返回的文档包含一个members字段,该字段包含一组成员配置文档,副本集的每个成员对应一个文档。

2

Assign priority value of 0.分配优先级值0

To prevent a secondary member from becoming a primary, update the secondary member's members[n].priority to 0.要防止次要成员成为主要成员,请将次要成员的members[n].priority更新为0

To assign a priority value to a member of the replica set, access the member configuration document using the array index. 要为副本集的成员分配优先级值,请使用数组索引访问成员配置文档。In this tutorial, the secondary member to change corresponds to the configuration document found at position 2 of the members array.在本教程中,要更改的第二个成员对应于members数组位置2处的配置文档。

cfg.members[2].priority = 0

The configuration change does not take effect until you reconfigure the replica set.在重新配置复制集之前,配置更改不会生效。

3

Reconfigure the replica set.重新配置副本集。

Use rs.reconfig() method to reconfigure the replica set with the updated replica set configuration document.使用rs.reconfig()方法使用更新的副本集配置文档重新配置副本集。

Pass the cfg variable to the rs.reconfig() method:cfg变量传递给rs.reconfig()方法:

rs.reconfig(cfg)

Related Documents相关文件

←  Adjust Priority for Replica Set MemberConfigure a Hidden Replica Set Member →