removeShard

  • removeShard
  • Removes a shard from a sharded cluster. When you runremoveShard, MongoDB drains the shard by using the balancer tomove the shard’s chunks to other shards in the cluster. Once the shard isdrained, MongoDB removes the shard from the cluster.

To run removeShard, use the db.runCommand( { <command> } ) method.

Behavior

You can only remove one shard at a time.removeShard returns an error if an existingremoveShard operation is in progress.

Access Requirements

You must run removeShard while connected to amongos. Issue the command against the admin database oruse the sh._adminCommand() helper.

If you have authorization enabled, you must have theclusterManager role or any role thatincludes the removeShard action.

Database Migration Requirements

Each database in a sharded cluster has a primary shard. If the shard youwant to remove is also the primary of one of the cluster’s databases, thenyou must manually move the databases to a new shard after migratingall data from the shard. See the movePrimary command andthe Remove Shards from an Existing Sharded Cluster for more information.

Chunk Balancing

When you remove a shard in a cluster with an uneven chunkdistribution, the balancer first removes the chunks from the drainingshard and then balances the remaining uneven chunk distribution.

Write Concern

mongos converts thewrite concern of theremoveShard command to "majority".

Change Streams

A shard removal may cause an open change stream cursor to close, and the closed change stream cursor maynot be fully resumable.

Example

From the mongo shell, the removeShardoperation resembles the following:

  1. db.adminCommand( { removeShard : "bristol01" } )

Replace bristol01 with the name of the shard to remove. When yourun removeShard, the command returns with a message thatresembles the following:

  1. {
  2. "msg" : "draining started successfully",
  3. "state" : "started",
  4. "shard" : "bristol01",
  5. "note" : "you need to drop or movePrimary these databases",
  6. "dbsToMove" : [
  7. "fizz",
  8. "buzz"
  9. ],
  10. "ok" : 1,
  11. "$clusterTime" : {
  12. "clusterTime" : Timestamp(1510716515, 1),
  13. "signature" : {
  14. "hash" : BinData(0,"B2ViX7XLzFLS5Fl9XEuFXbwKIM4="),
  15. "keyId" : NumberLong("6488045157173166092")
  16. }
  17. },
  18. "operationTime" : Timestamp(1510716515, 1)
  19. }

The balancer begins migrating chunks from the shard named bristol01to other shards in the cluster. These migrations happen slowly in order toavoid placing undue load on the cluster. Since bristol01 isthe primary shard for the fizz and buzz databases,removeShard notes that you must use themovePrimary command to move thosedatabases to another shard. Alternatively, you can use dropDatabaseto drop the database and delete its associated data files.

Note

If the shard you are removing is not the primary shard for anydatabases, the dbsToMove array will be empty andremoveShard can complete the migration withoutintervention.

If you run the command again, removeShard returns outputthat resembles the following:

  1. {
  2. "msg" : "draining ongoing",
  3. "state" : "ongoing",
  4. "remaining" : {
  5. "chunks" : NumberLong(2),
  6. "dbs" : NumberLong(2)
  7. },
  8. "note" : "you need to drop or movePrimary these databases",
  9. "dbsToMove" : [
  10. "fizz",
  11. "buzz"
  12. ],
  13. "ok" : 1,
  14. "$clusterTime" : {
  15. "clusterTime" : Timestamp(1510716515, 1),
  16. "signature" : {
  17. "hash" : BinData(0,"B2ViX7XLzFLS5Fl9XEuFXbwKIM4="),
  18. "keyId" : NumberLong("6488045157173166092")
  19. }
  20. },
  21. "operationTime" : Timestamp(1510716515, 1)
  22. }

The remaining document specifies how many chunks and databasesremain on the shard. Use the movePrimary command to moveeach database listed in dbsToMove to another shard.Alternatively, use dropDatabase to drop the database.

When the balancer completes moving all chunks off of the shard andyou have either moved or dropped any database listed in dbsToMove,running removeShard again returns output thatresembles the following:

  1. {
  2. "msg" : "removeshard completed successfully",
  3. "state" : "completed",
  4. "shard" : "bristol01",
  5. "ok" : 1,
  6. "$clusterTime" : {
  7. "clusterTime" : Timestamp(1510716515, 1),
  8. "signature" : {
  9. "hash" : BinData(0,"B2ViX7XLzFLS5Fl9XEuFXbwKIM4="),
  10. "keyId" : NumberLong("6488045157173166092")
  11. }
  12. },
  13. "operationTime" : Timestamp(1510716515, 1)
  14. }