Refer to official documents:
http://www.mongoing.com/docs/tutorial/remove-shards-from-cluster.html'>http://www.mongoing.com/docs/tutorial/remove-shards-from-cluster.html
1. Check whether balancer is on
mongos> sh.getBalancerState()
true
2. Find fragment id to delete
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5ab2d4f47692cf09f088f952")
}
shards:
{ "_id" : "shard0000", "host" : "192.168.31.59:22001" }
{ "_id" : "shard0001", "host" : "192.168.31.16:22002", "draining" : true }
{ "_id" : "shard0002", "host" : "192.168.31.118:22003" }
active mongoses:
```
//ip server 192.168.31.16 to be deleted, corresponding id: shard0001
3,Perform remove sharding
```
mongos> db.runCommand( { removeShard: "shard0001" } )
{
"msg" : "draining started successfully",#Successful start of pumping
"state" : "started",
"shard" : "shard0001",
"note" : "you need to drop or movePrimary these databases",
"dbsToMove" : [ ],
"ok" : 1
}
4, view
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5ab2d4f47692cf09f088f952")
}
shards:
{ "_id" : "shard0000", "host" : "192.168.31.59:22001" }
{ "_id" : "shard0001", "host" : "192.168.31.16:22002", "draining" : true }
{ "_id" : "shard0002", "host" : "192.168.31.118:22003" }
active mongoses:
"3.2.18-3.9" : 1
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "ntrailcenter", "primary" : "shard0000", "partitioned" : true }
{ "_id" : "testdb", "primary" : "shard0000", "partitioned" : true }
testdb.table1
shard key: { "id" : 1 }
unique: false
balancing: false
chunks:
shard0000 1
shard0001 1
shard0002 1
{ "id" : { "$minKey" : 1 } } -->> { "id" : 2 } on : shard0001 Timestamp(2, 0)
{ "id" : 2 } -->> { "id" : 20 } on : shard0002 Timestamp(3, 0)
{ "id" : 20 } -->> { "id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(3, 1)
5. Check the status of the migration
Check the status of the migration, run the removeShard command in the admin database again,
mongos> db.runCommand( { removeShard: "shard0001" } )
{
"msg" : "draining ongoing",
"state" : "ongoing",
"remaining" : {
"chunks" : NumberLong(1),
"dbs" : NumberLong(0)
},
"note" : "you need to drop or movePrimary these databases",
"dbsToMove" : [ ],
"ok" : 1
}
6. Remove non sharded data
Migrate data without sharding
If the shard is the primary shard of one or more databases, the data without shards will be stored on it. If not, the migration task will be skipped
In a cluster, a database without shards can only store data on a shard, which is the main shard of the database
Warning:
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()
db.printShardingStatus()
In the returned document, the databases field lists all databases and their main partitions. For example, the following databases field shows that the products database uses mongodb0 as its main partition
{ "_id" : "products", "partitioned" : true, "primary" : "mongodb0" }
To migrate the database to another partition, you need to use the movePrimary command. Use the following command to migrate all the remaining non fragmented data from mongodb0 to mongodb1
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 }
warning
This command will not return until all data migration is completed, which may take a long time. The final result is similar to this
6. Migration complete
mongos> use admin
switched to db admin
mongos> db.runCommand( { removeShard: "shard0001" } )
{
"msg" : "removeshard completed successfully",
"state" : "completed",
"shard" : "shard0001",
"ok" : 1
}
If the state is completed, the migration is complete.