On this page本页内容
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最新版本等过程中。
For each member of a replica set, starting with a secondary member, perform the following sequence of events, ending with the primary:对于副本集的每个成员,从次要成员开始,执行以下事件序列,以主要成员结束:
mongod
instance as a standalone.mongod
实例作为独立实例重新启动。mongod
instance as a member of the replica set.mongod
实例。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
to true
in the setParameter
section.setParameter
部分将参数disableLogicalSessionCacheRefresh
设置为true
。
If the 如果mongod
is a shard or config server member, you must also:mongod
是分片或配置服务器成员,还必须:
Comment out the 注释掉sharding.clusterRole
option.sharding.clusterRole
选项。
Set parameter 在skipShardingConfigurationChecks
(also available for MongoDB 3.6.3+, 3.4.11+, 3.2.19+) to true
in the setParameter
section.setParameter
部分将参数skipShardingConfigurationChecks
设置为true
(也可用于MongoDB 3.6.3+、3.4.11+、3.2.19+)。
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:如果使用命令行选项,请进行以下配置更新以重新启动:
Remove 删除--replSetName
.--replSetName
。
Set parameter 在disableLogicalSessionCacheRefresh
to true
in the --setParameter
option.--setParameter
选项中将参数disableLogicalSessionCacheRefresh
设置为true
。
If the 如果mongod
is a shard or config server member, you must also:mongod
是分片或配置服务器成员,还必须:
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+) to true
in the setParameter
section.setParameter
部分将参数skipShardingConfigurationChecks
设置为true
(也可用于MongoDB 3.6.3+、3.4.11+、3.2.19+)。
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:例如,要重新启动shard/config服务器副本集成员进行维护,请执行以下操作:
mongod --port 27218 --dbpath /srv/mongodb --bind_ip localhost,<hostname(s)|ip address(es)> --setParameter skipShardingConfigurationChecks=true --setParameter disableLogicalSessionCacheRefresh=true
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured 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
,即使在将副本集成员作为独立实例重新启动时也是如此。
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写入冲突,这些写入操作将在成员重新加入副本集时应用于该成员。
mongod
as a member of the replica set.mongod
。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 对于分片服务器或配置服务器成员,请确保删除skipShardingConfigurationChecks
parameter.skipShardingConfigurationChecks
参数。
When it has started, connect 启动后,将mongosh
to the restarted instance.mongosh
连接到重新启动的实例。
The secondary takes time to catch up to the primary. secondary
需要时间才能赶上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()
To perform maintenance on the primary after completing maintenance tasks on all secondaries, connect 要在完成所有辅助设备的维护任务后对主设备执行维护,请将mongosh
to the primary and use rs.stepDown()
to step down the primary and allow one of the secondaries to be elected the new primary. mongosh
连接到主设备,并使用rs.stepDown()
逐步减少主设备,允许其中一个辅助设备被选为新的主设备。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.主节点关闭后,副本集将选择一个新的主节点。
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. net.port
更改为其他端口。Make a note of the original port setting as a comment.记下原始端口设置作为注释。
Set parameter 在disableLogicalSessionCacheRefresh
to true
in the --setParameter
option.--setParameter
选项中将参数disableLogicalSessionCacheRefresh
设置为true
。
If the 如果mongod
is a shard or config server member, you must also:mongod
是分片服务器或配置服务器成员,还必须:
Comment out the 注释掉sharding.clusterRole
option.sharding.clusterRole
选项。
Set parameter 在skipShardingConfigurationChecks
(also available for MongoDB 3.6.3+, 3.4.11+, 3.2.19+) to true
in the setParameter
section.setParameter
部分将参数skipShardingConfigurationChecks
设置为true
(也可用于MongoDB 3.6.3+、3.4.11+、3.2.19+)。
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
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured 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
to true
in the --setParameter
option.--setParameter
选项中将参数disableLogicalSessionCacheRefresh
设置为true
。
If the 如果mongod
is a shard or config server member, you must also:mongod
是分片服务器或配置服务器成员,还必须:
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+) to true
in the setParameter
section.setParameter
部分将参数skipShardingConfigurationChecks
设置为true
(也可用于MongoDB 3.6.3+、3.4.11+、3.2.19+)。
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:例如,要重新启动shard/config服务器副本集成员进行维护,请执行以下操作:
mongod --port 27218 --dbpath /srv/mongodb --bind_ip localhost,<hostname(s)|ip address(es)> --setParameter skipShardingConfigurationChecks=true --setParameter disableLogicalSessionCacheRefresh=true
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured 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.在现在的单机版上执行维护任务。
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写入冲突,这些写入操作将在成员重新加入副本集时应用于该成员。
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
实例,并使用其原始配置;也就是说,撤消作为独立程序启动时所做的配置更改。
Be sure to remove the 请确保删除disableLogicalSessionCacheRefresh
parameter.disableLogicalSessionCacheRefresh
参数。
For shard or config server members, be sure to remove the 对于shard或config服务器成员,请确保删除skipShardingConfigurationChecks
parameter.skipShardingConfigurationChecks
参数。