On this page本页内容
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 要防止secondary成员在故障切换中成为primary成员,请按此处所述为次要成员分配优先级0。0
, as described here. For a detailed description of secondary-only members and their purposes, see Priority 0 Replica Set Members.有关仅辅助成员及其用途的详细说明,请参阅优先级0副本集成员。
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()
逐步降低当前主节点。
This tutorial uses a sample replica set with 5 members.本教程使用具有5个成员的示例副本集。
rs.reconfig()
shell method can force the current primary to step down, which causes an election. rs.reconfig()
shell方法可以强制当前的初选下台,从而导致选举。mongod
closes all client connections. mongod
关闭所有客户端连接。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
字段,该字段包含一组成员配置文档,副本集的每个成员对应一个文档。
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.在重新配置复制集之前,配置更改不会生效。
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)