Docs 菜单

replSetReconfig

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. You must run this command on the admin database of the 主节点 replica set member.

提示

mongosh 中,该命令也可通过 rs.reconfig() 辅助方法运行。

辅助方法对 mongosh 用户来说很方便,但它们返回的信息级别可能与数据库命令不同。如果不追求方便或需要额外的返回字段,请使用数据库命令。

此命令可用于以下环境中托管的部署:

重要

M0、M2、M5、M10+ 和 Flex 集群不支持此命令。有关更多信息,请参阅不支持的命令。

该命令具有以下语法:

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

The command takes the following optional field:

字段
说明

Defaults to false. Specify true to force the available replica set members to accept the new configuration.

Force reconfiguration can result in unexpected or undesired behavior, including rollback of "majority" committed writes.

Optional. Specifies a cumulative time limit in milliseconds for processing the replSetReconfig. By default, replSetReconfig waits indefinitely for the replica configuration to propagate to a majority of replica set members. Setting maxTimeMS may result in the operation failing before it can apply the new configuration. See 重新配置一直等到大多数节点安装副本配置 for more information.

You may also run replSetReconfig with the shell's rs.reconfig() method.

从 MongoDB 5.0 开始,在尝试使用更改隐式默认写关注配置重新配置副本集之前,您必须显式设置全局默认写关注。要设置全局默认写关注,请使用 setDefaultRWConcern 命令。

term 字段是由副本集节点设置的。如果在 replSetReconfig 操作中进行显式设置,主节点将忽略 term 字段。

replSetReconfig 默认情况下,允许一次添加或删除不超过 1voting 个节点。例如,新配置最多可以对集群 membership 进行以下更改之一

  • 添加新的投票副本集节点。

  • 删除现有的投票副本集成员。

  • 修改现有副本集节点的 votes

要添加或删除多个投票成员,请发出一系列 replSetReconfig 操作,每次添加或删除一个成员。

发出 force 重新配置将立即安装新配置,即使它添加或删除多个投票节点。force 重新配置可能会导致意外行为,例如,回滚 "majority" 提交的写入操作。

replSetReconfig 等待大多数投票副本集成员安装新的副本配置,才会返回成功。投票成员是任何 members[n].votes1 的副本集成员,包括仲裁节点。

副本集节点通过心跳传播其副本配置。每当节点发现具有更高 versionterm 的配置时,它就会安装新配置。重新配置过程具有两个不同的“等待”阶段:

1)等待提交当前配置,再安装新配置。

“当前”配置是指在发出 replSetReconfig 时主节点使用的副本配置。

在以下情况下,将提交配置:

  • 大多数投票副本集节点都安装了当前配置,并且

  • 所有在先前配置中以 "majority" 提交的写入也已复制到当前配置中的多数。

通常,当前配置已安装在大多数投票副本集节点上。不过,以前配置中的大多数提交的写入可能不会在当前配置中全部提交。Delayed节点 lagging behind主节点的节点可能会增加在该阶段所花的时间。

如果发出的操作具有 maxTimeMS 限制,并且 该操作在等待时超过该限制,该操作将返回错误并丢弃新配置。该限制是累积性的,在进入下一阶段后不会进行重置。

2)等待新配置中的大多数投票成员安装新配置。

“新”配置是指为 replSetReconfig 指定的副本配置。

主节点安装并开始使用新的副本配置,再将配置传播给其余副本集节点。该操作只需等待多数投票成员安装新配置,而无需等待提交新配置。

如果发出的操作具有 maxTimeMS 限制,并且操作在等待时超过该限制,则操作将返回错误,会继续使用和传播新配置。

无论以前配置的提交状态如何,发出 force 重新配置将立即安装新配置。force 重新配置可能会导致意外行为,例如,回滚 "majority" 提交的写入操作。

要检查当前副本配置的提交状态,请在副本集主节点使用 commitmentStatus 参数发出 replSetGetConfig

从 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. Nodes with the newlyAdded field do not count towards the current number of voting nodes. When initial sync completes and the node reaches SECONDARY state, the newlyAdded field is automatically removed.

注意

  • 即使使用 { force: true } 运行,尝试添加名为 newlyAdded 的字段的配置也会出错。

  • 如果现有节点有一个 newlyAdded 字段,使用 rs.reconfig() 更改配置将不会删除 newlyAdded 字段。newlyAdded 字段将被附加到用户提供的配置中。

  • replSetGetConfig 将删除输出中的所有 newlyAdded 字段。如果您想查看任何 newlyAdded 字段,可以直接查询 local.system.replset 集合。

To run the command on deployments that enforce access control, the user must have replSetConfigure privilege action on the cluster resource. The clusterManager built-in role, available in the admin database, provides the required privileges for this command.

replSetReconfig 获取特殊的独占锁,防止同时发生多个 replSetReconfig 操作。

警告

避免重新配置包含不同 MongoDB 版本节点的副本集,因为验证规则可能因 MongoDB 版本而异。

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 选举 to select a new 主节点:

  • 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.

  • When the primary steps down, it no longer closes all client connections; however, writes that were in progress are killed. For details, see 行为.

假设采用默认 replica configuration settings(副本配置设置),那么集群选举新的主节点之前的平均时间通常不应超过 12 秒。这包括将主节点标记为不可用以及召集和完成选举所需的时间。您可以通过修改 settings.electionTimeoutMillis 复制配置选项来调整该时间段。网络延迟等因素可能会延长副本集选举完成所需的时间,这反过来又会影响集群在没有主节点的情况下运行的时间。这些因素取决于具体集群架构。

在选举进程期间,集群不能接受写操作,直到选举出新的主节点。

您的应用程序连接逻辑应包括对自动故障转移和后续选举的容忍度。MongoDB 驱动程序可检测到主节点丢失,并一次性自动重试某些写入操作,从而为自动故障转移和选举提供额外的内置处理功能:

兼容的驱动程序将默认启用可重试写入

要进一步降低对生产集群的潜在影响,请仅在计划维护期间重新配置。

警告

MongoDB不会在集群中的副本集之间同步强制副本集重新配置。 使用{ force: true } 可能会导致多数提交写入回滚和分片分片集群不一致。使用此选项时请谨慎。

Using replSetReconfig to remove a replica set member does not automatically drop open outgoing connections from other replica set members to the removed member.

默认情况下,副本集节点等待 5 分钟,然后再删除到删除的节点的连接。在分片副本集中,您可以使用 ShardingTaskExecutorPoolHostTimeoutMS 服务器参数修改该超时。

要立即删除从副本集到已删除成员的所有传出连接,请对副本集的每个剩余成员运行 dropConnections 管理命令:

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

<hostname><port> 替换为已删除成员的值。

警告

从 MongoDB 5.0 开始,仅配置了 IP 地址的水平分割 DNS 节点无法通过启动验证,且会报告错误。请参阅 disableSplitHorizonIPCheck

副本集配置字段, 自管理副本集配置, rs.reconfig(), and rs.conf().