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成员都有资格通过选举过程成为主要成员。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
来影响这些选举的结果,使一些成员更有可能成为primary,而其他成员不太可能或无法成为primary。
Secondaries that cannot become primary are also unable to trigger elections. In all other respects these secondaries are identical to other secondaries.不能成为primary的secondary也不能引发选举。在所有其他方面,这些secondary与其他secondary相同。
To prevent a secondary member from ever becoming a primary in a failover, assign the secondary a priority of 若要防止secondary成员在故障转移中成为primary,请为该secondary分配优先级0,如下所述。0
, as described here. 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
字段的值混淆。
MongoDB does not permit the current primary to have a priority of MongoDB不允许当前primary的优先级为0
. 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个成员的示例复制副本集。
Thers.reconfig()
shell method can force the current primary to step down, which causes an election. When the primary steps down, themongod
closes all client connections.rs.reconfig()
shell方法可以强制当前的primary退出,从而导致选举。当primary关闭时,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版本而异。
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
字段,该字段包含一组成员配置文档,副本集的每个成员一个文档。
Assign priority value of 0
.分配优先级值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.在重新配置复制副本集之前,配置更改不会生效。
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)