对自管理副本集成员进行维护
Overview
Replica sets allow a MongoDB deployment to remain available during the majority of a maintenance window.
This document outlines the basic procedure for performing maintenance on each of the members of a replica set. Furthermore, this particular sequence strives to minimize the amount of time that the 主节点 is unavailable and control the impact on the entire deployment.
Use these steps as the basis for common replica set operations, particularly for procedures such as upgrading to the latest version of MongoDB.
步骤
For each member of a replica set, starting with a secondary member, perform the following sequence of events, ending with the primary:
Restart the
mongod
instance as a standalone.Perform the task on the standalone instance.
Restart the
mongod
instance as a member of the replica set.
Restart the secondary as a standalone on a different port.
At the operating system shell prompt restart mongod
as a standalone instance.
如果使用配置文件,请进行以下配置更新:
注释掉
replication.replSetName
选项。Change the
net.port
to a different port. Make a note of the original port setting as a comment.在
setParameter
部分中将参数
disableLogicalSessionCacheRefresh
设置为true
。
If the
mongod
is a 分片 or config server member, you must also:注释掉
sharding.clusterRole
选项。在
setParameter
部分中将参数skipShardingConfigurationChecks
设置为
true
。
For example, if performing maintenance on a shard/config server replica set member for maintenance, the updated configuration file will include content like the following example:
net: bindIp: localhost,<hostname(s)|ip address(es)> port: 27218 # port: 27018 #replication: # replSetName: shardA #sharding: # clusterRole: shardsvr setParameter: skipShardingConfigurationChecks: true disableLogicalSessionCacheRefresh: true
If using command-line options, make the following configuration updates to restart:
Modify
--port
to a different port.Set parameter
disableLogicalSessionCacheRefresh
totrue
in the--setParameter
option.If the
mongod
is a 分片 or config server member, you must also:如果是分片成员,则删除
--shardsvr
;如果是配置服务器分片,则删除 {--configsvr
。Set parameter
skipShardingConfigurationChecks
totrue
in thesetParameter
section.
For example, to restart a replica set member that is not part of a sharded cluster:
mongod --port 27218 --dbpath /srv/mongodb --bind_ip localhost,<hostname(s)|ip address(es)> --setParameter disableLogicalSessionCacheRefresh=true
For example, to restart a shard/config server replica set member for maintenance:
mongod --port 27218 --dbpath /srv/mongodb --bind_ip localhost,<hostname(s)|ip address(es)> --setParameter skipShardingConfigurationChecks=true --setParameter disableLogicalSessionCacheRefresh=true
警告
将实例绑定到可公开访问的 IP 地址之前,必须保护集群免遭未经授权的访问。有关安全建议的完整列表,请参阅自管理部署的安全清单。至少应考虑启用身份验证和强化网络基础设施。
Always start mongod
with the same user, even when
restarting a replica set member as a standalone instance.
Perform maintenance operations on the secondary.
While the member is a standalone, use mongosh
to
perform maintenance:
mongo --port 27218
重要
While the member is a standalone, no writes are replicated to this member nor are writes on this member replicated to the other members of the replica set.
Ensure that any writes on this standalone do not conflict with oplog writes that will be applied to the member when it rejoins the replica set.
重新启动 mongod
as a member of the replica set.
After performing all maintenance tasks, use the following procedure
to restart the mongod
as a member of the replica set
on its usual port.
From mongosh
, shut down the standalone
server after completing the maintenance:
use admin db.shutdownServer()
Restart the mongod
instance as a replica set
member with its original configuration; that is, undo the
configuration changes made when starting as a standalone.
提示
请务必删除 disableLogicalSessionCacheRefresh
参数。
For shard or config server members, be sure to remove the
skipShardingConfigurationChecks
parameter.
When it has started, connect mongosh
to the
restarted instance.
The secondary takes time to catch up to the primary. From mongosh
, use the
following command to verify that the member has caught up from the
RECOVERING
state to the SECONDARY
state.
rs.status()
Perform maintenance on the primary last.
To perform maintenance on the primary after completing maintenance tasks on all secondaries, connect
mongosh
to the primary and users.stepDown()
to step down the primary and allow one of the secondaries to be elected the new primary. Specify a 300 second waiting period to prevent the member from being elected primary again for five minutes:rs.stepDown(300) After the primary steps down, the replica set will elect a new primary.
重新启动
mongod
as a standalone instance, making the following configuration updates.
如果使用配置文件,请进行以下配置更新:
注释掉
replication.replSetName
选项。Change the
net.port
to a different port. Make a note of the original port setting as a comment.Set parameter
disableLogicalSessionCacheRefresh
totrue
in the--setParameter
option.If the
mongod
is a 分片 or config server member, you must also:注释掉
sharding.clusterRole
选项。在
setParameter
部分中将参数skipShardingConfigurationChecks
设置为
true
。
For example, if performing maintenance on a shard/config server replica set member for maintenance, the updated configuration file will include content like the following example:
net: bindIp: localhost,<hostname(s)|ip address(es)> port: 27218 # port: 27018 #replication: # replSetName: shardA #sharding: # clusterRole: shardsvr setParameter: skipShardingConfigurationChecks: true disableLogicalSessionCacheRefresh: true
警告
将实例绑定到可公开访问的 IP 地址之前,必须保护集群免遭未经授权的访问。有关安全建议的完整列表,请参阅自管理部署的安全清单。至少应考虑启用身份验证和强化网络基础设施。
如果使用命令行选项,请进行以下配置更新:
Modify
--port
to a different port.Set parameter
disableLogicalSessionCacheRefresh
totrue
in the--setParameter
option.If the
mongod
is a 分片 or config server member, you must also:如果是分片成员,则删除
--shardsvr
;如果是配置服务器分片,则删除 {--configsvr
。Set parameter
skipShardingConfigurationChecks
totrue
in thesetParameter
section.
For example, to restart a replica set member that is not part of a sharded cluster:
mongod --port 27218 --dbpath /srv/mongodb --bind_ip localhost,<hostname(s)|ip address(es)> --setParameter disableLogicalSessionCacheRefresh=true
For example, to restart a shard/config server replica set member for maintenance:
mongod --port 27218 --dbpath /srv/mongodb --bind_ip localhost,<hostname(s)|ip address(es)> --setParameter skipShardingConfigurationChecks=true --setParameter disableLogicalSessionCacheRefresh=true
警告
将实例绑定到可公开访问的 IP 地址之前,必须保护集群免遭未经授权的访问。有关安全建议的完整列表,请参阅自管理部署的安全清单。至少应考虑启用身份验证和强化网络基础设施。
Perform maintentance task on the now standalone.
重要
While the member is a standalone, no writes are replicated to this member nor are writes on this member replicated to the other members of the replica set.
Ensure that any writes on this standalone do not conflict with oplog writes that will be applied to the member when it rejoins the replica set.
After performing all maintenance tasks, restart the
mongod
instance as a replica set member with its original configuration; that is, undo the configuration changes made when starting as a standalone.提示
请务必删除
disableLogicalSessionCacheRefresh
参数。For shard or config server members, be sure to remove the
skipShardingConfigurationChecks
parameter.