On this page
对副本集成员执行维护
在本页面
Overview
Replica sets允许 MongoDB 部署在大部分维护时段内保持可用。
本文档概述了对副本集的每个成员执行维护的基本过程。此外,此特定序列将尽量减少primary不可用的时间,并控制对整个部署的影响。
将这些步骤用作常见副本集操作(尤其是诸如升级到最新版本的 MongoDB之类的过程)的基础。
Procedure
对于副本集的每个成员,从次要成员开始,执行以下事件序列,以主要事件结束:
停止辅助。
db.shutdownServer()
在另一个端口上独立启动辅助服务器。
在 osShell 程序提示符下,重新启动mongod作为独立实例。
If you are using a configuration file, make the following configuration updates:
Comment out the replication.replSetName option.
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 section.If the mongod is a shard or config server member, you must also:
Comment out the sharding.clusterRole option.
- Set parameter skipShardingConfigurationChecks (available starting in MongoDB 3.6.3+, 3.4.11+, 3.2.19+) to
true
in the setParameter section.
- Set parameter skipShardingConfigurationChecks (available starting in MongoDB 3.6.3+, 3.4.11+, 3.2.19+) to
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,<ip address of the mongod host>
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.
Modify --port to a different port.
Set parameter
disableLogicalSessionCacheRefresh
totrue
in the --setParameter option.If the mongod is a shard or config server member, you must also:
Remove --shardsvr if a shard member and --configsvr if a config server member.
- Set parameter skipShardingConfigurationChecks (available starting in MongoDB 3.6.3+, 3.4.11+, 3.2.19+) to
true
in the setParameter section.
- Set parameter skipShardingConfigurationChecks (available starting in MongoDB 3.6.3+, 3.4.11+, 3.2.19+) to
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
Warning
绑定到非 localhost(例如可公开访问)的 IP 地址之前,请确保已保护群集免受未经授权的访问。有关安全建议的完整列表,请参见Security Checklist。至少考虑enabling authentication和加强网络基础设施。
即使将副本集成员重新启动为独立实例,也始终以同一用户身份启动mongod。
在辅助节点上执行维护操作。
当成员是独立成员时,请使用mongo shell 执行维护:
mongo --port 27218
重新启动 mongod 作为副本集的成员。
执行完所有维护任务后,请按照以下步骤重新启动mongod作为其常规端口上副本集的成员。
完成维护后,从mongo shell 中关闭独立服务器:
use admin
db.shutdownServer()
使用其原始配置将mongod实例重新启动为副本集成员;也就是说,撤消独立启动时所做的配置更改。
Tip
确保删除disableLogicalSessionCacheRefresh
参数。
对于分片或配置服务器成员,请确保删除skipShardingConfigurationChecks参数。
启动后,将mongo shell 连接到重新启动的实例。
中学需要时间到赶上小学。在mongoShell 程序中,使用以下命令来验证该成员已从RECOVERING状态赶到SECONDARY状态。
rs.status()
在最后一个上执行维护。
要在完成所有辅助服务器上的维护任务后对主要服务器执行维护,请在mongo shell 中使用rs.stepDown()降级主要服务器,并允许其中一个辅助服务器被选举为新的主要服务器。指定 300 秒的 await 时间,以防止该成员在五分钟内再次当选主要成员:
rs.stepDown(300)
主要副本降级后,副本集将为选一个新的小学。
重新启动mongod作为独立实例,进行以下配置更新。
If you are using a configuration file, make the following configuration updates:
Comment out the replication.replSetName option.
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 shard or config server member, you must also:
Comment out the sharding.clusterRole option.
- Set parameter skipShardingConfigurationChecks (available starting in MongoDB 3.6.3+, 3.4.11+, 3.2.19+) to
true
in the setParameter section.
- Set parameter skipShardingConfigurationChecks (available starting in MongoDB 3.6.3+, 3.4.11+, 3.2.19+) to
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,<ip address of the mongod host>
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:
Remove --replSetName.
Modify --port to a different port.
Set parameter
disableLogicalSessionCacheRefresh
totrue
in the --setParameter option.If the mongod is a shard or config server member, you must also:
Remove --shardsvr if a shard member and --configsvr if a config server member.
- Set parameter skipShardingConfigurationChecks (available starting in 3.6.3+, 3.4.11+, 3.2.19+) to
true
in the setParameter section.
- Set parameter skipShardingConfigurationChecks (available starting in 3.6.3+, 3.4.11+, 3.2.19+) to
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
Warning
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist. At minimum, consider enabling authentication and hardening network infrastructure.
Warning
绑定到非 localhost(例如可公开访问)的 IP 地址之前,请确保已保护群集免受未经授权的访问。有关安全建议的完整列表,请参见Security Checklist。至少考虑enabling authentication和加强网络基础设施。
完成所有维护任务后,以其原始配置将mongod实例重新启动为副本集成员;也就是说,撤消独立启动时所做的配置更改。
Tip
确保删除disableLogicalSessionCacheRefresh
参数。
对于分片或配置服务器成员,请确保删除skipShardingConfigurationChecks参数。