Skip to content

Instantly share code, notes, and snippets.

@liujingyu
Created May 9, 2015 03:44
Show Gist options
  • Select an option

  • Save liujingyu/c6eee6e27702e82b3f87 to your computer and use it in GitHub Desktop.

Select an option

Save liujingyu/c6eee6e27702e82b3f87 to your computer and use it in GitHub Desktop.
mongodb 从现有的片式集群中删除分片
Ensure the Balancer Process is Active
To successfully migrate data from a shard, the balancer process must be active. Check the balancer state using the sh.getBalancerState() helper in the mongo shell. For more information, see the section on balancer operations.
Determine the Name of the Shard to Remove
To determine the name of the shard, do one of the following:
From the admin database, run the listShards command.
Run either the sh.status() method or the sh.printShardingStatus() method.
The shards._id field lists the name of each shard.
Remove Chunks from the Shard
Run the removeShard command. This begins “draining” chunks from the shard you are removing to other shards in the cluster. For example, for a shard named mongodb0, run:
db.runCommand( { removeShard: "mongodb0" } )
This operation returns immediately, with the following response:
{ msg : "draining started successfully" , state: "started" , shard :"mongodb0" , ok : 1 }
Depending on your network capacity and the amount of data, this operation can take from a few minutes to several days to complete.
Check the Status of the Migration
To check the progress of the migration at any stage in the process, run removeShard. For example, for a shard named mongodb0, run:
db.runCommand( { removeShard: "mongodb0" } )
The command returns output similar to the following:
{ msg: "draining ongoing" , state: "ongoing" , remaining: { chunks: NumberLong(42), dbs : NumberLong(1) }, ok: 1 }
In the output, the remaining document displays the remaining number of chunks that MongoDB must migrate to other shards and the number of MongoDB databases that have “primary” status on this shard.
Continue checking the status of the removeShard command until the number of chunks remaining is 0. Then proceed to the next step.
Move Unsharded Data
If the shard is the primary shard for one or more databases in the cluster, then the shard will have unsharded data. If the shard is not the primary shard for any databases, skip to the next task, Finalize the Migration.
In a cluster, a database with unsharded collections stores those collections only on a single shard. That shard becomes the primary shard for that database. (Different databases in a cluster can have different primary shards.)
警告 Do not perform this procedure until you have finished draining the shard.
To determine if the shard you are removing is the primary shard for any of the cluster’s databases, issue one of the following methods:
sh.status()
sh.printShardingStatus()
In the resulting document, the databases field lists each database and its primary shard. For example, the following database field shows that the products database uses mongodb0 as the primary shard:
{ "_id" : "products", "partitioned" : true, "primary" : "mongodb0" }
To move a database to another shard, use the movePrimary command. For example, to migrate all remaining unsharded data from mongodb0 to mongodb1, issue the following command:
db.runCommand( { movePrimary: "products", to: "mongodb1" })
This command does not return until MongoDB completes moving all data, which may take a long time. The response from this command will resemble the following:
{ "primary" : "mongodb1", "ok" : 1 }
Finalize the Migration
To clean up all metadata information and finalize the removal, run removeShard again. For example, for a shard named mongodb0, run:
db.runCommand( { removeShard: "mongodb0" } )
A success message appears at completion:
{ msg: "remove shard completed successfully" , stage: "completed", host: "mongodb0", ok : 1 }
Once the value of the stage field is “completed”, you may safely stop the processes comprising the mongodb0 shard.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment