定义
replSetReconfigThe
replSetReconfigadministrative 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 theadmindatabase of the primary replica set member.提示
In
mongosh, this command can also be run through thers.reconfig()helper method.Helper methods are convenient for
mongoshusers, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.
兼容性
此命令可用于以下环境中托管的部署:
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
重要
MongoDB Atlas 集群不支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令。
语法
该命令具有以下语法:
db.adminCommand( { replSetReconfig: <new_config_document>, force: <boolean>, maxTimeMS: <int> } )
命令字段
该命令采用以下可选字段:
字段 | 说明 |
|---|---|
默认为 强制重新配置可能导致意外或不希望发生的行为,包括回滚 | |
Optional. Specifies a cumulative time limit in milliseconds for processing the |
You may also run replSetReconfig with the shell's rs.reconfig() method.
行为
全局写关注
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. To set the global default write concern, use the setDefaultRWConcern command.
term 副本配置字段
The term field is set by the primary replica set member. The primary ignores the term field if set explicitly in the replSetReconfig operation.
重新配置可以一次添加或删除不超过一个投票节点
replSetReconfig by default allows adding or removing no more than 1 voting member at a time. For example, a new configuration can make at most one of the following changes to the cluster membership:
添加新的投票副本集节点。
删除现有的投票副本集成员。
修改现有副本集节点的
votes。
To add or remove multiple voting members, issue a series of replSetReconfig operations to add or remove one member at a time.
发出 force 重新配置将立即安装新配置,即使它添加或删除多个投票节点。force 重新配置可能会导致意外行为,例如,回滚 "majority" 提交的写入操作。
重新配置一直等到大多数节点安装副本配置
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.
副本集节点通过心跳传播其副本配置。每当节点发现具有更高 version 和 term 的配置时,它就会安装新配置。重新配置过程具有两个不同的“等待”阶段:
- 1)等待提交当前配置,再安装新配置。
The "current" configuration refers to the replica configuration in use by the primary at the time
replSetReconfigis issued.在以下情况下,将提交配置:
大多数投票副本集节点都安装了当前配置,并且
所有在先前配置中以
"majority"提交的写入也已复制到当前配置中的多数。
通常,当前配置已安装在大多数投票副本集节点上。不过,以前配置中的大多数提交的写入可能不会在当前配置中全部提交。
Delayed节点或lagging behind主节点的节点可能会增加在该阶段所花的时间。如果发出的操作具有 maxTimeMS 限制,并且 该操作在等待时超过该限制,该操作将返回错误并丢弃新配置。该限制是累积性的,在进入下一阶段后不会进行重置。
- 2)等待新配置中的大多数投票成员安装新配置。
The "new" configuration refers to the replica configuration specified to
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集合。
访问控制
要在强制执行访问控制的部署上运行该命令,用户必须对集群资源具有replSetConfigure权限操作特权。admin数据库中提供的clusterManager内置角色提供此命令所需的权限。
锁定行为
replSetReconfig obtains a special mutually exclusive lock to prevent more than one replSetReconfig operation from occurring at the same time.
混合版本的副本集
警告
避免重新配置包含不同 MongoDB 版本节点的副本集,因为验证规则可能因 MongoDB 版本而异。
可用性
该集的大多数成员必须可操作才能使更改正确传播。
replSetReconfig can trigger the current primary to step down in some situations. Primary step-down triggers an election to select a new primary:
当新的主节点启动时,它会增加
term字段,以区分在新的主节点上所做的配置更改与在上一个主节点上所做的配置更改。当主节点降级时,它不再关闭所有客户端连接;正在进行的写入操作也会被终止。有关详情,请参阅行为。
假设采用默认 replica configuration settings(副本配置设置),那么集群选举新的主节点之前的平均时间通常不应超过 12 秒。这包括将主节点标记为不可用以及召集和完成选举所需的时间。您可以通过修改 settings.electionTimeoutMillis 复制配置选项来调整该时间段。网络延迟等因素可能会延长副本集选举完成所需的时间,这反过来又会影响集群在没有主节点的情况下运行的时间。这些因素取决于具体集群架构。
在选举进程期间,集群不能接受写操作,直到选举出新的主节点。
您的应用程序连接逻辑应包括对自动故障转移和后续选举的容忍度。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> 替换为已删除成员的值。
警告
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.