Remove Shards from the existing Sharded cluster
Important points to be considered before removing the shard from the existing shard cluster
- Make sure that the shard data is migrated to the other shards successfully.
- Make sure the balancer is enabled before you run the remove shard.
Step-1: find the shard name to be removed
To find the name of the shard, connect to mongos instance.
db.adminCommand( { listShards: 1 } )
Output:
mongos> db.adminCommand( { listShards: 1 } )
{
"shards" : [
{
"_id" : "replicaset1",
"host" : "replicaset1/mysql8.localdomain:27011,mysql8.localdomain:27012,mysql8.localdomain:27013",
"state" : 1
},
{
"_id" : "replicaset2",
"host" : "replicaset2/mysql8.localdomain:27021,mysql8.localdomain:27022,mysql8.localdomain:27023",
"state" : 1
},
{
"_id" : "replicaset3",
"host" : "replicaset3/mysql8.localdomain:27031,mysql8.localdomain:27032,mysql8.localdomain:27033",
"state" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1603537651, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1603537651, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
Step-2: Remove Chunks from the Shard
db.adminCommand( { removeShard: "replicaset1" } )
Note
login the admin database and run the removeShard command. This will start the "draining" chunks to the others chunk from the shard which you will remove in the cluster. The balancer begins migrating chunks from the shard to the other shards in the cluster. The migrations takes place slowly to avoid any load on all other clusters.
Depending on the network capacity and the amount of data, this operation can take from minutes to days as well to complete.
The operation will return the prompt immediately.
Output:
mongos> db.adminCommand( { removeShard: "replicaset1" } )
{
"msg" : "draining started successfully",
"state" : "started",
"shard" : "replicaset1",
"note" : "you need to drop or movePrimary these databases",
"dbsToMove" : [
"school"
"college"
],
"ok" : 1,
"operationTime" : Timestamp(1603687180, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1603687180, 2),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
Step-3: Check the migration status
db.adminCommand( { removeShard: "replicaset1" } )
Output:
mongos> db.adminCommand( { removeShard: "replicaset1" } )
{
"msg" : "removeshard completed successfully", <------------------ complete successfully
"state" : "completed",
"shard" : "replicaset1",
"ok" : 1,
"operationTime" : Timestamp(1603687688, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1603687688, 2),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
Step-4: validate the list of shards post remove successfully
db.adminCommand( { listShards: 1 } )
Output:
You could observe the shard name "replicaset01" is not listed in the below result. So, we have successfully removed the Shard.
mongos> db.adminCommand( { listShards: 1 } )
{
"shards" : [
{
"_id" : "replicaset2",
"host" : "replicaset2/mysql8.localdomain:27021,mysql8.localdomain:27022,mysql8.localdomain:27023",
"state" : 1
},
{
"_id" : "replicaset3",
"host" : "replicaset3/mysql8.localdomain:27031,mysql8.localdomain:27032,mysql8.localdomain:27033",
"state" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1603687785, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1603687785, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
Step-5: Move Databases to Another Primary Shard
If the removed shard was the primary shard of the databases in the cluster, then you should make that database use a different shard as its primary shard.
If the shard is not the primary shard for any databases, skip the step-5 and Finalize the Migration.
db.adminCommand( { movePrimary: "school", to: "replicaset2" })
Note: This command does not return the prompt until it completes moving all data successfully.
Step-6: Finalize the Migration
db.adminCommand( { removeShard: "replicaset1" } )
Note: look at the state filed and that should show as "completed".
Comments
Post a Comment