Perform Maintenance on Replica Set Members对副本集成员执行维护
On this page本页内容
Overview概述
Replica sets allow a MongoDB deployment to remain available during the majority of a maintenance window.副本集允许MongoDB部署在维护窗口的大部分时间内保持可用。
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 primary is unavailable and control the impact on the entire deployment.此外,这个特定的序列努力将primary不可用的时间降到最低,并控制对整个部署的影响。
Use these steps as the basis for common replica set operations, particularly for procedures such as upgrading to the latest version of MongoDB.使用这些步骤作为常见副本集操作的基础,特别是在升级到最新版本的MongoDB等过程中。
Procedure过程
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.mongod
实例作为独立实例重新启动。Perform the task on the standalone instance.在独立实例上执行任务。Restart the将mongod
instance as a member of the replica set.mongod
实例作为副本集的成员重新启动。
Restart the secondary as a standalone on a different port.在其他端口上将辅助设备作为独立设备重新启动。
At the operating system shell prompt restart 在操作系统shell提示符下,将mongod
as a standalone instance.mongod
作为独立实例重新启动。
If you are using a configuration file, make the following configuration updates:如果您正在使用配置文件,请进行以下配置更新:
-
Comment out the注释掉replication.replSetName
option.replication.replSetName
选项。 -
Change the将net.port
to a different port.net.port
更改为其他端口。Make a note of the original port setting as a comment.记下原始端口设置作为注释。 -
Set parameter在disableLogicalSessionCacheRefresh
totrue
in thesetParameter
section.setParameter
部分中将参数disableLogicalSessionCacheRefresh
设置为true
。 -
If the如果mongod
is a shard or config server member, you must also:mongod
是分片或config
服务器成员,则还必须:-
Comment out the注释掉sharding.clusterRole
option.sharding.clusterRole
选项。 -
Set parameter在skipShardingConfigurationChecks
(also available for MongoDB 3.6.3+, 3.4.11+, 3.2.19+) totrue
in thesetParameter
section.setParameter
部分将参数skipShardingConfigurationChecks
(也可用于MongoDB 3.6.3+、3.4.11+、3.2.19+)设置为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:例如,如果对分片/config服务器副本集成员执行维护以进行维护,则更新后的配置文件将包括如下内容:
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:如果使用命令行选项,请进行以下配置更新以重新启动:
-
Remove删除--replSetName
.--replSetName
。 -
Set parameter在disableLogicalSessionCacheRefresh
totrue
in the--setParameter
option.--setParameter
选项中将参数disableLogicalSessionCacheRefresh
设置为true
。 -
If the如果mongod
is a shard or config server member, you must also:mongod
是分片或config
服务器成员,则还必须:-
Remove如果是分片成员,则移除--shardsvr
if a shard member and--configsvr
if a config server member.--shardsvr
;如果是配置服务器成员,则删除--configsvr
。 -
Set parameter在skipShardingConfigurationChecks
(also available for MongoDB 3.6.3+, 3.4.11+, 3.2.19+) totrue
in thesetParameter
section.setParameter
部分将参数skipShardingConfigurationChecks
(也可用于MongoDB 3.6.3+、3.4.11+、3.2.19+)设置为true
。
-
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:例如,要重新启动分片/config服务器副本集成员进行维护:
mongod --port 27218 --dbpath /srv/mongodb --bind_ip localhost,<hostname(s)|ip address(es)> --setParameter skipShardingConfigurationChecks=true --setParameter disableLogicalSessionCacheRefresh=true
Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. 在将实例绑定到可公开访问的IP地址之前,必须保护群集不受未经授权的访问。For a complete list of security recommendations, see Security Checklist. 有关安全建议的完整列表,请参阅安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证和强化网络基础设施。
Always start 始终使用同一用户启动mongod
with the same user, even when restarting a replica set member as a standalone instance.mongod
,即使将副本集成员作为独立实例重新启动也是如此。
Perform maintenance operations on the secondary.对辅助设备执行维护操作。
While the member is a standalone, use 当成员是独立的时,请使用mongosh
to perform maintenance:mongosh
执行维护:
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.请确保此独立操作上的任何写入操作都不会与成员重新加入副本集时将应用于该成员的oplog写入操作冲突。
Restart mongod
as a member of the replica set.将mongod
作为副本集的成员重新启动。
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.mongod
作为复制集的成员重新启动到其常用端口。
From 在mongosh
, shut down the standalone server after completing the maintenance:mongosh
中,完成维护后关闭独立服务器:
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.mongod
实例重新启动为具有其原始配置的副本集成员;也就是说,撤消作为独立程序启动时所做的配置更改。
Be sure to remove the 请确保删除disableLogicalSessionCacheRefresh
parameter.disableLogicalSessionCacheRefresh
参数。
For shard or config server members, be sure to remove the 对于分片或config服务器成员,请确保删除skipShardingConfigurationChecks
parameter.skipShardingConfigurationChecks
参数。
When it has started, connect 启动后,将mongosh
to the restarted instance.mongosh
连接到重新启动的实例。
The secondary takes time to catch up to the primary. 次要的需要时间才能赶上primary。From 在mongosh
, use the following command to verify that the member has caught up from the RECOVERING
state to the SECONDARY
state.mongosh
中,使用以下命令验证成员是否已从RECOVERING
状态恢复到SECONDARY
状态。
rs.status()
Perform maintenance on the primary last.对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.mongosh
连接到主设备,然后使用mongosh
逐步关闭主设备,并允许其中一个辅助设备被选为新的主设备。Specify a 300 second waiting period to prevent the member from being elected primary again for five minutes:指定300秒的等待时间,以防止成员在五分钟内再次当选为主要成员:rs.stepDown(300)
After the primary steps down, the replica set will elect a new primary.主复制副本关闭后,复制副本集将选择一个新的主复制副本。Restart将mongod
as a standalone instance, making the following configuration updates.mongod
作为独立实例重新启动,进行以下配置更新。
If you are using a configuration file, make the following configuration updates:如果您正在使用配置文件,请进行以下配置更新:
Comment out the注释掉replication.replSetName
option.replication.replSetName
选项。Change the将net.port
to a different port. Make a note of the original port setting as a comment.net.port
更改为其他端口。记下原始端口设置作为注释。Set parameter在disableLogicalSessionCacheRefresh
totrue
in the--setParameter
option.--setParameter
选项中将参数disableLogicalSessionCacheRefresh
设置为true
。If the如果mongod
is a shard or config server member, you must also:mongod
是分片或config
服务器成员,则还必须:-
Comment out the注释掉sharding.clusterRole
option.sharding.clusterRole
选项。 -
Set parameter在skipShardingConfigurationChecks
(also available for MongoDB 3.6.3+, 3.4.11+, 3.2.19+) totrue
in thesetParameter
section.setParameter
部分将参数skipShardingConfigurationChecks
(也可用于MongoDB 3.6.3+、3.4.11+、3.2.19+)设置为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:例如,如果对分片/config服务器副本集成员执行维护以进行维护,则更新后的配置文件将包括如下内容:
net:
bindIp: localhost,<hostname(s)|ip address(es)>
port: 27218
# port: 27018
#replication:
# replSetName: shardA
#sharding:
# clusterRole: shardsvr
setParameter:
skipShardingConfigurationChecks: true
disableLogicalSessionCacheRefresh: true
Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. 在将实例绑定到可公开访问的IP地址之前,必须保护群集不受未经授权的访问。For a complete list of security recommendations, see Security Checklist. 有关安全建议的完整列表,请参阅安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证和强化网络基础设施。
If using command-line options, make the following configuration updates:如果使用命令行选项,请进行以下配置更新:
-
Remove删除--replSetName
.--replSetName
。 -
Set parameter在disableLogicalSessionCacheRefresh
totrue
in the--setParameter
option.--setParameter
选项中将参数disableLogicalSessionCacheRefresh
设置为true
。 -
If the如果mongod
is a shard or config server member, you must also:mongod
是分片或config
服务器成员,则还必须:-
Remove如果是分片成员,则移除--shardsvr
if a shard member and--configsvr
if a config server member.--shardsvr
;如果是配置服务器成员,则删除--configsvr
。 -
Set parameter在skipShardingConfigurationChecks
(also available for MongoDB 3.6.3+, 3.4.11+, 3.2.19+) totrue
in thesetParameter
section.setParameter
部分将参数skipShardingConfigurationChecks
(也可用于MongoDB 3.6.3+、3.4.11+、3.2.19+)设置为true
。
-
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:例如,要重新启动分片/config服务器副本集成员进行维护:
mongod --port 27218 --dbpath /srv/mongodb --bind_ip localhost,<hostname(s)|ip address(es)> --setParameter skipShardingConfigurationChecks=true --setParameter disableLogicalSessionCacheRefresh=true
Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. 在将实例绑定到可公开访问的IP地址之前,必须保护群集不受未经授权的访问。For a complete list of security recommendations, see Security Checklist. 有关安全建议的完整列表,请参阅安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证和强化网络基础设施。
Perform maintentance task on the now standalone.在现在的单机版上执行维护任务。ImportantWhile 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.请确保此独立操作上的任何写入操作都不会与成员重新加入副本集时将应用于该成员的oplog写入操作冲突。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.mongod
实例重新启动为具有原始配置的副本集成员;也就是说,撤消作为独立程序启动时所做的配置更改。TipBe sure to remove the请确保删除disableLogicalSessionCacheRefresh
parameter.disableLogicalSessionCacheRefresh
参数。For shard or config server members, be sure to remove the对于分片或config服务器成员,请确保删除skipShardingConfigurationChecks
parameter.skipShardingConfigurationChecks
参数。