How to remove shard in mongodb?

Member

by aubrey , in category: PHP Databases , 2 years ago

How to remove shard in mongodb?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by haylee.mertz , 2 years ago

@aubrey 

To remove a shard from a sharded cluster in MongoDB, you will need to follow these steps:

  1. Stop the shard you want to remove:Connect to the mongos instance and use the sh.stopBalancer() command to stop the balancer.Connect to the primary mongod instance of the shard you want to remove and use the db.fsyncLock() command to lock writes to the primary. This will prevent new writes from occurring while you are removing the shard.Connect to the mongos instance and use the sh.stopBalancer() command to stop the balancer.
  2. Remove the shard from the cluster:Connect to the mongos instance and use the sh.removeShard() command to remove the shard from the cluster. This will remove the shard from the shards collection in the config database.
  3. Unlock the primary:Connect to the primary mongod instance of the shard you just removed and use the db.fsyncUnlock() command to unlock writes to the primary. This will allow new writes to occur again.
  4. Restart the balancer:Connect to the mongos instance and use the sh.startBalancer() command to restart the balancer. This will redistribute the chunks that were previously on the shard you just removed to the remaining shards in the cluster.


Note: These steps assume that you are using a sharded cluster with a replica set as the shard. If you are using a standalone mongod instance as a shard, you will need to follow a slightly different process to remove it.

Member

by orpha , a year ago

@aubrey 

To remove a shard in MongoDB, you can follow these steps:

  1. Connect to the primary mongos instance of the cluster using the mongo shell.
  2. Run the command sh.removeShard("
  3. Monitor the removal process by running the command sh.status() in the mongo shell. This will display the current status of the sharded cluster and show the progress of the shard removal.
  4. Wait for the removal process to complete. This may take some time depending on the size of the data on the shard being removed and the network conditions.
  5. Once the removal process is complete, the removed shard will be no longer part of the cluster, and its data will be distributed across the remaining shards.


Note that removing a shard from a sharded cluster is a delicate operation and should be performed with caution. It's important to backup your data and ensure that the cluster is properly configured and balanced before proceeding with shard removal.