Here is a comprehensive guide on how to remove a node from a MongoDB replica set. You can choose from two primary methods: using the rs.remove() helper function or directly reconfiguring the replica set with rs.reconfig(). Both methods require you to connect to the current primary node of the replica set.
Before you begin
Before you remove a replica set member, it's essential to follow these preparatory steps to ensure data integrity and a smooth transition:
-
Identify the current primary: Connect to any replica set member and run the
rs.status()ordb.hello()command to determine the current primary. This is the node you will connect to for the removal operation. -
Back up your data: Always create a backup of your replica set data before performing any critical maintenance operation.
-
Ensure replica set health: Verify that the replica set is healthy and all members are communicating correctly by checking the output of
rs.status(). -
Shut down the target node: Gracefully shut down the
mongodinstance of the member you plan to remove. This prevents any inconsistencies and ensures no data is written to the node after its removal from the replica set configuration.javascriptuse admin db.shutdownServer()Use code with caution.
Method 1: Use rs.remove()
The rs.remove() command is the simplest way to remove a single member from a replica set. It automatically updates the replica set configuration.
Step-by-step procedure
-
Connect to the primary: Using
mongosh, connect to the current primary node of your replica set. -
Execute the
rs.remove()command: Runrs.remove()with the host and port of the member you want to remove.javascriptrs.remove("hostname_or_ip:port")Use code with caution.
- Example:
rs.remove("mongod3.example.net:27017") - Behavior: MongoDB will automatically perform a reconfiguration of the replica set. The primary may step down, and a new election will occur to choose a new primary. If this happens, your shell may briefly disconnect before automatically reconnecting.
- Example:
-
Verify the new configuration: After the command completes, run
rs.status()orrs.conf()to confirm that the member has been successfully removed from the replica set.javascriptrs.status()Use code with caution.
-
Clean up the removed node: The removed
mongodinstance still has its old data and configuration. You should perform the following cleanup tasks:- Delete the data directory of the removed member to prevent it from rejoining the set with old data.
- If you plan to repurpose the node as a standalone instance, update its configuration file to remove the
replication.replSetNamesetting.
Method 2: Use rs.reconfig()
The rs.reconfig() method offers greater control over the reconfiguration process by allowing you to manually edit the replica set configuration document. This is especially useful for removing multiple members or performing more complex configuration changes.
Step-by-step procedure
-
Connect to the primary: Connect to the current primary node using
mongosh. -
Get the current configuration: Retrieve the current replica set configuration document by running
rs.conf()and assign it to a variable.javascriptcfg = rs.conf()Use code with caution.
-
Find the member to remove: Identify the member you want to remove within the
membersarray of the configuration document. The_idfield is the index of the member in the array.json{ "_id" : "rs", "version" : 7, "members" : [ { "_id" : 0, "host" : "mongod_A.example.net:27017" }, { "_id" : 1, "host" : "mongod_B.example.net:27017" }, { "_id" : 2, "host" : "mongod_C.example.net:27017" } ] }Use code with caution.
-
Modify the configuration: Use the
Array.prototype.splice()method in JavaScript to remove the member from themembersarray. For example, to remove the member with_id: 2, you would use:javascriptcfg.members.splice(2, 1)Use code with caution.
-
Apply the new configuration: Pass the modified configuration document to the
rs.reconfig()method.javascriptrs.reconfig(cfg)Use code with caution.
- Note: If you are removing multiple voting members, you must perform this operation one member at a time to maintain a stable majority.
-
Verify and clean up: As with the
rs.remove()method, verify the new configuration withrs.status()and then clean up the removedmongodinstance's data directory.
Considerations for removing the primary
Removing a primary node is a more complex operation because it necessitates a failover and election process.
- Planned failover: Before removing the current primary, you can force a failover to a different member. This gives you greater control over which node becomes the new primary. You can do this by using the
rs.stepDown()command on the current primary. - Election: When you remove the current primary, an election will immediately be triggered among the remaining members to choose a new primary. This might cause a brief outage until the election completes.
What happens after a node is removed?
After a node is removed from the replica set, the following occurs:
- State change: The removed member, if still running, will enter a "REMOVED" state. The remaining replica set members will no longer attempt to communicate with it.
- Configuration update: The replica set's configuration document is updated across all members to reflect the new state.
- Cleanup: The data directory on the removed machine remains intact. It is crucial to manually delete this data or reconfigure the node for standalone use to prevent a desynchronized member from accidentally rejoining with a "divergent write history," which can cause data inconsistencies.