Perform Maintenance on Replica Set Members
Overview
Replica sets allow a MongoDB deployment toremain available during the majority of a maintenance window.
This document outlines the basic procedure for performing maintenance oneach of the members of a replica set. Furthermore, this particularsequence strives to minimize the amount of time that theprimary is unavailable and control the impact on theentire deployment.
Use these steps as the basis for common replica set operations,particularly for procedures such as upgrading to the latestversion of 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. - Perform the task on the standalone instance.
- Restart the
mongod
instance as a member of the replicaset.
Stop a secondary.
In the mongo
shell, shut down the mongod
instance:
- db.shutdownServer()
Restart the secondary as a standalone on a different port.
At the operating system shell prompt restart mongod
as a standalone instance.
- Configuration File
- Command-line Options
If you are using a configuration file, make the followingconfiguration 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 thesetParameter
section. - If the
mongod
is a shard orconfig server member, you must also:- Comment out the
sharding.clusterRole
option. - Set parameter
skipShardingConfigurationChecks
(also available for MongoDB 3.6.3+, 3.4.11+, 3.2.19+) totrue
in thesetParameter
section.
- Comment out the
For example, if performing maintenance on a shard/configserver replica set member for maintenance, the updatedconfiguration file will include content like thefollowing 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 followingconfiguration 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 orconfig server member, you must also:- Remove
—shardsvr
if ashard member and—configsvr
if a config server member. - Set parameter
skipShardingConfigurationChecks
(alsoavailable for MongoDB 3.6.3+, 3.4.11+, 3.2.19+) totrue
in thesetParameter
section.
- Remove
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 unauthorizedaccess. For a complete list of security recommendations, seeSecurity Checklist. At minimum, considerenabling authentication andhardening network infrastructure.
Always start mongod
with the same user, even whenrestarting a replica set member as a standalone instance.
Perform maintenance operations on the secondary.
While the member is a standalone, use the mongo
shell toperform maintenance:
- mongo --port 27218
Important
While the member is a standalone, no writes are replicated tothis member nor are writes on this member replicated to the othermembers of the replica set.
Ensure that any writes on this standalone do not conflict withoplog writes that will be applied to the member when it rejoinsthe replica set.
Restart mongod as a member of the replica set.
After performing all maintenance tasks, use the following procedureto restart the mongod
as a member of the replica seton its usual port.
From the mongo
shell, shut down the standaloneserver after completing the maintenance:
- use admin
- db.shutdownServer()
Restart the mongod
instance as a replica setmember with its original configuration; that is, undo theconfiguration changes made when starting as a standalone.
Tip
Be sure to remove the disableLogicalSessionCacheRefresh
parameter.
For shard or config server members, be sure to remove theskipShardingConfigurationChecks
parameter.
When it has started, connect the mongo
shell to therestarted instance.
The secondary takes time to catch up to the primary. From the mongo
shell, use thefollowing command to verify that the member has caught up from theRECOVERING
state to the SECONDARY
state.
- rs.status()
Perform maintenance on the primary last.
- To perform maintenance on the primary after completingmaintenance tasks on all secondaries, connect a
mongo
shell to the primary and users.stepDown()
to step down the primary and allow one ofthe secondaries to be elected the new primary. Specify a 300second waiting period to prevent the member from being electedprimary again for five minutes:
- rs.stepDown(300)
After the primary steps down, the replica set will elect anew primary.
- Restart
mongod
as a standalone instance, makingthe following configuration updates.
- Configuration File
- Command-line Options
If you are using a configuration file, make the followingconfiguration 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 orconfig server member, you must also:- Comment out the
sharding.clusterRole
option. - Set parameter
skipShardingConfigurationChecks
(also available for MongoDB 3.6.3+, 3.4.11+, 3.2.19+) totrue
in thesetParameter
section.
- Comment out the
For example, if performing maintenance on a shard/configserver replica set member for maintenance, the updatedconfiguration file will include content like thefollowing example:
- net:
- bindIp: localhost,<hostname(s)|ip address(es)>
- port: 27218
- # port: 27018
- #replication:
- # replSetName: shardA
- #sharding:
- # clusterRole: shardsvr
- setParameter:
- skipShardingConfigurationChecks: true
- disableLogicalSessionCacheRefresh: true
Warning
Before binding to a non-localhost (e.g. publicly accessible)IP address, ensure you have secured your cluster from unauthorizedaccess. For a complete list of security recommendations, seeSecurity Checklist. At minimum, considerenabling authentication andhardening network infrastructure.
If using command-line options, make the followingconfiguration updates:
- Remove
—replSetName
. - Modify
—port
to a different port. - Set parameter
disableLogicalSessionCacheRefresh
totrue
in the—setParameter
option. - If the
mongod
is a shard orconfig server member, you must also:- Remove
—shardsvr
if ashard member and—configsvr
if a config server member. - Set parameter
skipShardingConfigurationChecks
(alsoavailable for MongoDB 3.6.3+, 3.4.11+, 3.2.19+) totrue
in thesetParameter
section.
- Remove
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 unauthorizedaccess. For a complete list of security recommendations, seeSecurity Checklist. At minimum, considerenabling authentication andhardening network infrastructure.
- Perform maintentance task on the now standalone.
Important
While the member is a standalone, no writes are replicated tothis member nor are writes on this member replicated to the othermembers of the replica set.
Ensure that any writes on this standalone do not conflict withoplog writes that will be applied to the member when it rejoinsthe replica set.
- After performing all maintenance tasks, restart the
mongod
instance as a replica set member with itsoriginal configuration; that is, undo the configuration changes madewhen starting as a standalone.
Tip
Be sure to remove the disableLogicalSessionCacheRefresh
parameter.
For shard or config server members, be sure to remove theskipShardingConfigurationChecks
parameter.