Docs HomeMongoDB Manual

Prevent Secondary from Becoming Primary防止中学成为小学

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 0, as described here. 若要防止secondary成员在故障转移中成为primary,请为该secondary分配优先级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的优先级为0To 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. When the primary steps down, the mongod 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版本而异。
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相关文件