Database Manual / Reference / Database Commands / Replication

replSetReconfig (database command数据库命令)

Definition定义

replSetReconfig

The replSetReconfig administrative command modifies the configuration of an existing replica set. You can use this command to add and remove members, and to alter the options set on existing members. replSetReconfig管理命令修改现有副本集的配置。您可以使用此命令添加和删除成员,以及更改对现有成员设置的选项。You must run this command on the admin database of the primary replica set member.您必须在primary副本集成员的admin数据库上运行此命令。

Tip

In mongosh, this command can also be run through the rs.reconfig() helper method.mongosh中,此命令也可以通过rs.reconfig()辅助方法运行。

Helper methods are convenient for mongosh users, but they may not return the same level of information as database commands. 助手方法对mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。 In cases where the convenience is not needed or the additional return fields are required, use the database command.如果不需要便利性或需要额外的返回字段,请使用database命令。

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Important

This command is not supported in MongoDB Atlas clusters. MongoDB Atlas集群不支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

Syntax语法

The command has the following syntax:该命令具有以下语法:

db.adminCommand(
{
replSetReconfig: <new_config_document>,
force: <boolean>,
maxTimeMS: <int>
}
)

Command Fields命令字段

The command takes the following optional field:该命令接受以下可选字段:

Field字段Description描述

force

Defaults to false. Specify true to force the available replica set members to accept the new configuration.默认为false。指定true可强制可用副本集成员接受新配置。

Force reconfiguration can result in unexpected or undesired behavior, including rollback of "majority" committed writes.强制重新配置可能会导致意外或不希望的行为,包括回滚"majority"已提交的写入。

maxTimeMS

Optional. 可选。Specifies a cumulative time limit in milliseconds for processing the replSetReconfig. 指定处理replSetReconfig的累积时间限制(以毫秒为单位)。By default, replSetReconfig waits indefinitely for the replica configuration to propagate to a majority of replica set members. 默认情况下,replSetReconfig会无限期地等待副本配置传播到大多数副本集成员。Setting maxTimeMS may result in the operation failing before it can apply the new configuration. See Reconfiguration Waits Until a Majority of Members Install the Replica Configuration for more information.设置maxTimeMS可能会导致操作失败,然后才能应用新配置。有关详细信息,请参阅重新配置等待大多数成员安装副本配置

You may also run replSetReconfig with the shell's rs.reconfig() method.您还可以使用shell的rs.reconfig()方法运行replSetReconfig

Behavior行为

Global Write Concern全局写入关注

Starting in MongoDB 5.0, you must explicitly set the global default write concern before attempting to reconfigure a replica set with a configuration that would change the implicit default write concern. 从MongoDB 5.0开始,在尝试使用会更改隐式默认写入关注配置重新配置副本集之前,您必须显式设置全局默认写入关注。To set the global default write concern, use the setDefaultRWConcern command.要设置全局默认写入关注,请使用setDefaultRWConcern命令。

term Replica Configuration Field副本配置字段

The term field is set by the primary replica set member. term字段由primary副本集成员设置。The primary ignores the term field if set explicitly in the replSetReconfig operation.如果在replSetReconfig操作中显式设置了term字段,则主字段将忽略该术语字段。

Reconfiguration Can Add or Remove No More than One Voting Member at a Time重新配置一次只能添加或删除一个以上的投票成员

replSetReconfig by default allows adding or removing no more than 1 voting member at a time. 默认情况下,允许一次添加或删除不超过1voting成员。For example, a new configuration can make at most one of the following changes to the cluster membership:例如,新配置最多可以对集群membership进行以下更改之一:

  • Adding a new voting replica set member.添加新的投票副本集成员。
  • Removing an existing voting replica set member.删除现有的投票副本集成员。
  • Modifying the votes for an existing replica set member.修改现有副本集成员的votes

To add or remove multiple voting members, issue a series of replSetReconfig operations to add or remove one member at a time.要添加或删除多个投票成员,请执行一系列replSetReconfig操作,一次添加或删除一个成员。

Issuing a force reconfiguration immediately installs the new configuration even if it adds or removes multiple voting members. 发布强制重新配置会立即安装新配置,即使它添加或删除了多个投票成员。Force reconfiguration can cause unexpected behavior, such as the rollback of "majority" committed write operations.强制重新配置可能会导致意外行为,例如回滚"majority"提交的写入操作。

Reconfiguration Waits Until a Majority of Members Install the Replica Configuration重新配置将等待大多数成员安装副本配置

replSetReconfig waits until a majority of voting replica set members install the new replica configuration before returning success. 等待大多数投票副本集成员安装新的副本配置,然后返回成功。A voting member is any replica set member where members[n].votes is 1, including arbiters.投票成员是任何副本集成员,其中members[n].votes1,包括仲裁者。

Replica set members propagate their replica configuration via heartbeats. 副本集成员通过心跳传播其副本配置。Whenever a member learns of a configuration with a higher version and term, it installs the new configuration. The reconfiguration process has two distinct 'waiting' phases:每当成员得知具有更高versionterm的配置时,它都会安装新的配置。重新配置过程有两个不同的“等待”阶段:

1) Wait for the current configuration to be committed before installing the new configuration.在安装新配置之前,请等待当前配置被提交。

The "current" configuration refers to the replica configuration in use by the primary at the time replSetReconfig is issued.“当前”配置是指主服务器在发出replSetReconfig时使用的副本配置。

A configuration is committed when:在以下情况下提交配置:

  • A majority of voting replica set members have installed the current configuration, and大多数投票副本集成员已安装当前配置,并且
  • All writes which were "majority" committed in the previous configuration have also replicated to a majority in the current configuration.在先前配置中提交的所有"majority"写入也复制到当前配置中的多数。

Typically, the current configuration has already been installed on a majority of voting replica set members. However, the majority committed writes in the previous configuration may not all be committed in the current configuration. 通常,当前配置已经安装在大多数投票副本集成员上。但是,在先前配置中提交的大多数写入可能不会在当前配置中全部提交。Delayed members or members that are lagging behind the primary can increase the time spent in this phase.延迟的成员或落后于主要成员的成员可能会增加在此阶段花费的时间。

If the operation was issued with a maxTimeMS limit and the operation exceeds the limit while waiting, the operation returns an error and discard the new configuration. The limit is cumulative and does not reset after proceeding to the next phase.如果操作发出了maxTimeMS限制,并且在等待时操作超过了限制,则操作将返回错误并丢弃新配置。该限制是累积的,在进入下一阶段后不会重置。

2) Wait for a majority of voting members in the new configuration to install the new configuration.等待新配置中的大多数投票成员安装新配置。

The "new" configuration refers to the replica configuration specified to replSetReconfig.“新”配置是指为replSetReconfig指定的副本配置。

The primary installs and begins using the new replica configuration before propagating the configuration to the remaining replica set members. 主服务器安装并开始使用新的副本配置,然后将配置传播给其余的副本集成员。The operation only waits for a majority of voting members to install the new configuration, and does not require waiting for the new configuration to be committed.该操作仅等待大多数投票成员安装新配置,不需要等待新配置提交

If the operation was issued with a maxTimeMS limit and the operation exceeds the limit while waiting, the operation returns an error but continues using and propagating the new configuration.如果操作发出了maxTimeMS限制,并且在等待时操作超过了限制,则操作将返回错误,但会继续使用和传播新配置。

Issuing a force reconfiguration immediately installs the new configuration regardless of the commitment status of the previous configuration. 发布强制重新配置会立即安装新配置,而不管之前配置的承诺状态如何。Force reconfiguration can cause unexpected behavior, such as the rollback of "majority" committed write operations.强制重新配置可能会导致意外行为,例如回滚"majority"提交的写入操作。

To check the commitment status of the current replica configuration, issue replSetGetConfig with the commitmentStatus parameter on the replica set primary.要检查当前副本配置的承诺状态,请在副本集主上发出带有commitmentStatus参数的replSetGetConfig

Automatic Reconfiguration for New Voting Replica Set Members新投票副本集成员的自动重新配置

Starting in MongoDB 5.0, a newly added secondary does not count as a voting member and cannot be elected until it has reached the SECONDARY state.从MongoDB 5.0开始,新添加的次要成员不算作投票成员,在达到SECONDARY状态之前无法当选。

When a new voting node is added to a replica set, replSetReconfig will internally add a newlyAdded field to the node's configuration. 当一个新的投票节点被添加到副本集中时,replSetReconfig将在节点的配置中内部添加一个新添加的字段。Nodes with the newlyAdded field do not count towards the current number of voting nodes. 带有newlyAdded字段的节点不计入当前投票节点的数量。When initial sync completes and the node reaches SECONDARY state, the newlyAdded field is automatically removed.当初始同步完成并且节点达到SECONDARY状态时,newlyAdded字段将自动删除。

Note

  • Configurations that attempt to add a field named newlyAdded will error even if run with { force: true }.即使使用{ force: true }运行,尝试添加名为newyAdd的字段的配置也会出错。
  • If an existing node has a newlyAdded field, using rs.reconfig() to change the configuration will not remove the newlyAdded field. The newlyAdded field will be appended to the user provided configuration.如果现有节点有一个newlyAdded的字段,使用rs.reconfig()更改配置不会删除newlyAdded字段。newlyAdded字段将附加到用户提供的配置中。
  • replSetGetConfig will remove any newlyAdded fields from its output. 将从其输出中删除任何newlyAdded字段。If you would like to see any newlyAdded fields, you can query the local.system.replset collection directly.如果你想看到任何newlyAdded字段,你可以直接查询local.system.replset集合。

Access Control访问控制

To run the command on deployments that enforce access control, the user must have replSetConfigure privilege action on the cluster resource. 要在强制访问控制的部署上运行该命令,用户必须对群集资源执行replSetConfigure权限操作。The clusterManager built-in role, available in the admin database, provides the required privileges for this command.admin数据库中提供的clusterManager内置角色为此命令提供了所需的权限。

Locking Behavior锁定行为

replSetReconfig obtains a special mutually exclusive lock to prevent more than one replSetReconfig operation from occurring at the same time.获取一个特殊的互斥锁,以防止同时发生多个replSetReconfig操作。

Mixed Version Replica Set混合版本副本集

Warning

Avoid reconfiguring replica sets that contain members of different MongoDB versions as validation rules may differ across MongoDB versions.避免重新配置包含不同MongoDB版本成员的副本集,因为不同MongoDB版本的验证规则可能不同。

Availability可用性

A majority of the set's members must be operational for the changes to propagate properly.集合的大多数成员必须可操作,才能正确传播更改。

replSetReconfig can trigger the current primary to step down in some situations. 在某些情况下,可以触发当前的初选下台。Primary step-down triggers an election to select a new primary:Primary降级触发选举以选择新的primary

  • When the new primary steps up, it increments the term field to distinguish configuration changes made on the new primary from changes made on the previous primary.当新主服务器升级时,它会递增term字段,以区分在新主服务器上进行的配置更改和在上一个主服务器上所做的更改。
  • When the primary steps down, it no longer closes all client connections; however, writes that were in progress are killed. For details, see Behavior.当主服务器关闭时,它不再关闭所有客户端连接;然而,正在进行的写作被扼杀了。有关详细信息,请参阅行为

The median time before a cluster elects a new primary should not typically exceed 12 seconds, assuming default replica configuration settings. 假设默认副本配置设置,集群选择新主服务器之前的中位时间通常不应超过12秒。This includes time required to mark the primary as unavailable and call and complete an election. 这包括将初选标记为不可用以及呼叫并完成选举所需的时间。You can tune this time period by modifying the settings.electionTimeoutMillis replication configuration option. 您可以通过修改设置settings.electionTimeoutMillis复制配置选项来调整此时间段。Factors such as network latency may extend the time required for replica set elections to complete, which in turn affects the amount of time your cluster may operate without a primary. These factors are dependent on your particular cluster architecture.网络延迟等因素可能会延长完成副本集选举所需的时间,这反过来又会影响集群在没有主服务器的情况下运行的时间。这些因素取决于特定集群架构。

During the election process, the cluster cannot accept write operations until it elects the new primary.在选举过程中,集群在选择新的主节点之前无法接受写入操作。

Your application connection logic should include tolerance for automatic failovers and the subsequent elections. 应用程序连接逻辑应包括对自动故障转移和后续选择的容忍度。MongoDB drivers can detect the loss of the primary and automatically retry certain write operations a single time, providing additional built-in handling of automatic failovers and elections:MongoDB驱动程序可以检测到主写入操作的丢失,并自动重试某些写入操作一次,提供额外的内置自动故障转移和选择处理:

Compatible drivers enable retryable writes by default默认情况下,兼容的驱动程序允许可重试的写入

To further reduce potential impact to a production cluster, reconfigure only during scheduled maintenance periods.为了进一步减少对生产集群的潜在影响,请仅在计划维护期间进行重新配置。

{ force: true }

Warning

MongoDB does not synchronize a forced replica set reconfiguration between the replica sets in a cluster. MongoDB不会在集群中的副本集之间同步强制的副本集重新配置。Using { force: true } can lead to a rollback of majority-committed writes and an inconsistent sharded cluster. Exercise caution when you use this option.使用{ force: true }可能会导致多数提交的写入回滚和不一致的分片集群。使用此选项时请小心。

Drop Outgoing Connections After Removing a Member删除成员后删除传出连接

Using replSetReconfig to remove a replica set member does not automatically drop open outgoing connections from other replica set members to the removed member.使用replSetReconfig删除副本集成员不会自动删除从其他副本集成员到被删除成员的打开的传出连接。

By default, replica set members wait for 5 minutes before dropping connections to the removed member. In sharded replica sets, you can modify this timeout using the ShardingTaskExecutorPoolHostTimeoutMS server parameter.默认情况下,副本集成员在断开与已删除成员的连接之前会等待5分钟。在分片副本集中,您可以使用ShardingTaskExecutorPoolHostTimeoutMS服务器参数修改此超时。

To immediately drop all outgoing connections from the replica set to the removed member, run the dropConnections administrative command on each remaining member on the replica set:要立即删除从副本集中到已删除成员的所有传出连接,请在副本集中的每个剩余成员上运行dropConnections管理命令:

db.adminCommand(
{
"dropConnections" : 1,
"hostAndPort" : [
"<hostname>:<port>"
]
}
)

Replace <hostname> and <port> with those of the removed member.<hostname><port>替换为已删除成员的名称。

Warning

Starting in MongDB 5.0, split horizon DNS nodes that are only configured with an IP address fail startup validation and report an error. See disableSplitHorizonIPCheck.从MongDB 5.0开始,仅配置了IP地址的拆分水平DNS节点无法通过启动验证并报告错误。请参阅disableSplitHorizonIPCheck

Member Priority and Votes会员优先级和投票

  • Members with priority greater than 0 cannot have 0 votes.priority大于0的成员不能有0票。
  • Non-voting (i.e. votes is 0) members must have priority of 0.无投票权(即votes数为0)成员的优先级必须为0。

Additional Information附加信息

Replica Set Configuration Fields, Self-Managed Replica Set Configuration, rs.reconfig(), and rs.conf().副本集配置字段自我管理副本集配置rs.reconfig()rs.conf()