翻译或纠错本页面

removeShard

On this page

removeShard

Removes a shard from a sharded cluster. When you run removeShard, MongoDB drains the shard by using the balancer to move the shard’s chunks to other shards in the cluster. Once the shard is drained, 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 existing removeShard operation is in progress.

Access Requirements

You must run removeShard while connected to a mongos. Issue the command against the admin database or use the sh._adminCommand() helper.

If you have authorization enabled, you must have the clusterManager role or any role that includes the removeShard action.

Database Migration Requirements

Each database in a sharded cluster has a primary shard. If the shard you want to remove is also the primary of one of the cluster’s databases, then you must manually move the databases to a new shard after migrating all data from the shard. See the movePrimary command and the 从集群中删除分片 for more information.

Chunk Balancing

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

Example

From the mongo shell, the removeShard operation resembles the following:

use admin
db.runCommand( { removeShard : "bristol01" } )

Replace bristol01 with the name of the shard to remove. When you run removeShard, the command returns immediately, with the following message:

{
    "msg" : "draining started successfully",
    "state" : "started",
    "shard" : "bristol01",
    "ok" : 1
}

The balancer begins migrating chunks from the shard named bristol01 to other shards in the cluster. These migrations happens slowly to avoid placing undue load on the overall cluster.

If you run the command again, removeShard returns the following progress output:

{
    "msg" : "draining ongoing",
    "state" : "ongoing",
    "remaining" : {
        "chunks" : 23,
        "dbs" : 1
    },
    "ok" : 1
}

The remaining document specifies how many chunks and databases remain on the shard. Use db.printShardingStatus() to list the databases that you must move from the shard. Use the movePrimary to move databases.

After removing all chunks and databases from the shard, you can issue removeShard again see the following:

{
    "msg" : "removeshard completed successfully",
    "state" : "completed",
    "shard" : "bristol01",
    "ok" : 1
}
←   listShards getShardMap  →