On this page本页内容
replSetReconfig
The replSetReconfig
administrative command modifies the configuration of an existing replica set. replSetReconfig
管理命令修改现有副本集的配置。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 primary replica set member.admin
数据库上运行此命令。
The command has the following syntax:该命令具有以下语法:
db.adminCommand({
replSetReconfig: <new_config_document>,
force: <boolean>,
maxTimeMS: <int>
})
The command takes the following optional field:该命令接受以下可选字段:
| |
|
You may also run 您也可以使用shell的replSetReconfig
with the shell's rs.reconfig()
method.rs.reconfig()
方法运行replSetReconfig
。
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
MongoDB 4.4 adds the MongoDB 4.4将term
field to the replica set configuration document. term
字段添加到副本集配置文档中。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
操作中显式设置,则primary将忽略term
字段。
Starting in MongoDB 4.4, 从MongoDB 4.4开始,replSetReconfig
by default allows adding or removing no more than 1
voting
member at a time. replSetReconfig
默认允许一次添加或删除不超过1
个voting
成员。For example, a new configuration can make at most one of the following changes to the cluster 例如,新配置最多可以对群集membership
:membership
进行以下更改之一:
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"
提交的写入操作。
Starting in MongoDB 4.4, 从MongoDB 4.4开始,replSetReconfig
waits until a majority of voting replica set members install the new replica configuration before returning success. replSetReconfig
等待大多数投票副本集成员安装新的副本配置,然后返回成功。A voting member is any replica set member where 投票成员是members[n].votes
is 1
, including arbiters.members[n].votes
为1
的任何复制集成员,包括仲裁员。
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. version
和term
的配置时,就会安装新配置。The reconfiguration process has two distinct 'waiting' phases:重新配置过程有两个不同的“等待”阶段:
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:在以下情况下提交配置:
"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. 但是,在以前的配置中提交的大多数写入可能不会全部在当前配置中提交。延迟的成员或落后于primary的成员可能会增加此阶段所花费的时间。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 andthe operation exceeds the limit while waiting, the operation returns an error and discard the new configuration. 如果发出的操作具有maxTimeMS
限制,并且在等待时操作超过了限制,则操作将返回错误并放弃新配置。The limit is cumulative and does not reset after proceeding to the next phase.该限制是累积的,在进入下一阶段后不会重置。
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 要检查当前副本配置的承诺状态,请在副本集primary上发出带有replSetGetConfig
with the commitmentStatus parameter on the replica set primary.commitmentStatus
参数的replSetGetConfig
。
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 从MongoDB 5.0开始,新添加的次要成员不算作投票成员,在达到SECONDARY
state.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
将在内部向节点的配置中添加一个newAdded
字段。Nodes with the 具有newlyAdded
field do not count towards the current number of voting nodes. newAdded
字段的节点不计入当前投票节点数。When initial sync completes and the node reaches 当初始同步完成并且节点达到SECONDARY
state, the newlyAdded
field is automatically removed.SECONDARY
状态时,newlyAdded
字段将自动删除。
newlyAdded
will error even if run with { force: true }
.newlyAdded
的字段的配置即使使用{ force: true }
运行也会出错。newlyAdded
field, using rs.reconfig()
to change the configuration will not remove the newlyAdded
field. newlyAdded
字段,则使用rs.reconfig()
更改配置不会删除newlyAdded
字段。newlyAdded
field will be appended to the user provided configuration.newlyAdded
的字段将附加到用户提供的配置中。replSetGetConfig
newlyAdded
fields from its output. newlyAdded
字段。newlyAdded
fields, you can query the local.system.replset
collection directly.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. replSetConfigure
权限操作。The clusterManager
built-in role, available in the admin
database, provides the required privileges for this command.admin
数据库中提供的clusterManager
内置角色为该命令提供了所需的权限。
replSetReconfig
obtains a special mutually exclusive lock to prevent more than one 获取一个特殊的互斥锁,以防止同时发生多个replSetReconfig
operation from occurring at the same time.replSetReconfig
操作。
Avoid reconfiguring replica sets that contain members of different MongoDB versions as validation rules may differ across MongoDB versions.避免重新配置包含不同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 election to select a new primary:初选递降会触发选择新primary的选举:
term
field to distinguish configuration changes made on the new primary from changes made on the previous primary.term
字段,以区分在新主服务器上所做的配置更改与在上一个主服务器上进行的更改。The median time before a cluster elects a new primary should not typically exceed 12 seconds, assuming default 假设默认副本配置设置,群集选择新主服务器之前的中值时间通常不应超过12秒。replica configuration settings
. 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. 应用程序连接逻辑应包括对自动故障切换和后续选择的容忍度。Starting in MongoDB 3.6, 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 3.6开始,MongoDB驱动程序可以检测主设备的丢失,并在一次自动重试某些写入操作,从而提供自动故障切换和选择的额外内置处理:
retryWrites=true
in the connection string.retryWrites=true
来显式启用可重试写入。To further reduce potential impact to a production cluster, reconfigure only during scheduled maintenance periods.为了进一步减少对生产集群的潜在影响,请仅在计划的维护期间重新配置。
{ force: true }
Forcing the 强制执行replSetReconfig
command can lead to a rollback situation. replSetReconfig
命令可能会导致回滚情况。Use with caution.小心使用。
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. 默认情况下,副本集成员在断开与已删除成员的连接之前等待5分钟。In sharded replica sets, you can modify this timeout using the 在分片副本集中,可以使用ShardingTaskExecutorPoolHostTimeoutMS
server parameter.ShardingTaskExecutorPoolHostTimeoutMS
服务器参数修改此超时。
New in version 4.2.在版本4.2中新增。 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>
替换为已删除成员的名称。
Starting in MongDB 5.0, split horizon DNS nodes that are only configured with an IP address fail startup validation and report an error. 从MongDB 5.0开始,仅使用IP地址配置的拆分地平线DNS节点无法通过启动验证并报告错误。See 请参见disableSplitHorizonIPCheck
.disableSplitHorizonIPCheck
。
Replica Set Configuration Fields, Replica Set Configuration, 副本集配置字段、副本集配置、rs.reconfig()
, and rs.conf()
.rs.reconfig()
和rs.conf()
。