Dragonfly

Question: How do you remove a node from a MongoDB cluster?

Answer

Removing a node from a MongoDB cluster, specifically from a replica set, is an operation that should be performed with care to ensure the cluster continues to operate smoothly. Here's how you can safely remove a node from a MongoDB replica set:

Step 1: Ensure Replica Set Stability

Before removing any node, ensure that:

Step 2: Remove the Node from the Replica Set Configuration

Connect to the mongo shell of one of the remaining nodes in the replica set and issue the following commands:

cfg = rs.conf()
cfg.members = cfg.members.filter(member => member.host !== 'nodeToRemove:port')
rs.reconfig(cfg)

Replace 'nodeToRemove:port' with the actual hostname and port of the node you want to remove.

Step 3: Shutdown the Removed Node

After successfully removing the node from the replica set configuration, you can shut down the removed node:

  1. Connect to the mongo shell on the node to be removed.
  2. Use the following command to shut down the MongoDB server:
db.shutdownServer()

Step 4: Post-Removal Steps

Important Consideration

If the removed node was holding a significant amount of data, consider balancing the data across the remaining nodes in the replica set. This may happen automatically depending on your cluster's settings, but it's good practice to monitor disk space and load distribution post-removal.

This process is for replica sets. For sharded clusters, removing a shard involves additional steps and considerations, as data must be migrated away from the shard before removal.

Was this content helpful?

Other Common MongoDB Performance Questions (and Answers)

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost