removeShard
removeShard
- Removes a shard from a sharded cluster. When you run
removeShard
, 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 removeShard
operation resembles the following:
- 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:
- {
- "msg" : "draining started successfully",
- "state" : "started",
- "shard" : "bristol01",
- "note" : "you need to drop or movePrimary these databases",
- "dbsToMove" : [
- "fizz",
- "buzz"
- ],
- "ok" : 1,
- "$clusterTime" : {
- "clusterTime" : Timestamp(1510716515, 1),
- "signature" : {
- "hash" : BinData(0,"B2ViX7XLzFLS5Fl9XEuFXbwKIM4="),
- "keyId" : NumberLong("6488045157173166092")
- }
- },
- "operationTime" : Timestamp(1510716515, 1)
- }
The balancer begins migrating chunks from the shard named bristol01
to 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 dropDatabase
to 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:
- {
- "msg" : "draining ongoing",
- "state" : "ongoing",
- "remaining" : {
- "chunks" : NumberLong(2),
- "dbs" : NumberLong(2)
- },
- "note" : "you need to drop or movePrimary these databases",
- "dbsToMove" : [
- "fizz",
- "buzz"
- ],
- "ok" : 1,
- "$clusterTime" : {
- "clusterTime" : Timestamp(1510716515, 1),
- "signature" : {
- "hash" : BinData(0,"B2ViX7XLzFLS5Fl9XEuFXbwKIM4="),
- "keyId" : NumberLong("6488045157173166092")
- }
- },
- "operationTime" : Timestamp(1510716515, 1)
- }
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:
- {
- "msg" : "removeshard completed successfully",
- "state" : "completed",
- "shard" : "bristol01",
- "ok" : 1,
- "$clusterTime" : {
- "clusterTime" : Timestamp(1510716515, 1),
- "signature" : {
- "hash" : BinData(0,"B2ViX7XLzFLS5Fl9XEuFXbwKIM4="),
- "keyId" : NumberLong("6488045157173166092")
- }
- },
- "operationTime" : Timestamp(1510716515, 1)
- }