Adding and removing shards

Adding a new shard to our cluster is as easy as connecting to mongos, connecting to the admin database, and invoking runCommand with the following:

> db.runCommand( {
addShard: "mongo_books_replica_set/rs01.packtdb.com:27017", maxSize: 18000, name: "packt_mongo_shard_UK"
} )

This adds a new shard from the replica set named mongo_books_replica_set from the rs01.packtdb.com host running on port 27017. We also define the maxSize of data for this shard as 18000 MB (or we can set it to 0 to give it no limit) and the name of the new shard as packt_mongo_shard_UK.

This operation will take quite some time to complete as chunks will have to be rebalanced and migrated to the new shard.

Removing a shard, on the other hand, requires more involvement since we have to make sure that we won't lose any data on the way. We do this as follows:

  1. First, we need to make sure that the balancer is enabled using sh.getBalancerState(). Then, after identifying the shard we want to remove using any one of the sh.status(), db.printShardingStatus(), or listShards admin commands, we connect to the admin database and invoke removeShard as follows:
> use admin
> db.runCommand( { removeShard: "packt_mongo_shard_UK" } )

The output should contain the following:

...
"msg" : "draining started successfully",
"state" : "started",
...
  1. Then, if we invoke the same command again, we get the following:
> db.runCommand( { removeShard: "packt_mongo_shard_UK" } )

"msg" : "draining ongoing",
"state" : "ongoing",
"remaining" : {
"chunks" : NumberLong(2),
"dbs" : NumberLong(3)
},

The remaining document in the result contains the number of chunks and dbs that are still being transferred. In our case, it's 2 and 3 respectively.

All the commands need to be executed in the admin database.

An extra complication in removing a shard can arise if the shard we want to remove serves as the primary shard for one or more of the databases that it contains. The primary shard is allocated by MongoDB when we initiate sharding, so when we remove the shard, we need to manually move these databases to a new shard.

  1. We will know whether we need to perform this operation by looking at the following section of the result from removeShard():
...
"note" : "you need to drop or movePrimary these databases",
"dbsToMove" : [
"mongo_books"
],
...

We need to drop or movePrimary our mongo_books database. The way to do this is to first make sure that we are connected to the admin database.

We need to wait for all of the chunks to finish migrating before running this command.
  1. Make sure that the result contains the following before proceeding:
 ..."remaining" : {
"chunks" : NumberLong(0) }...
  1. Only after we have made sure that the chunks to be moved are down to zero can we safely run the following command:
> db.runCommand( { movePrimary: "mongo_books", to: "packt_mongo_shard_EU" })
  1. This command will invoke a blocking operation, and, when it returns, it should have the following result:
{ "primary" : "packt_mongo_shard_EU", "ok" : 1 }
  1. Invoking the same removeShard() command after we are all done should return the following result:
> db.runCommand( { removeShard: "packt_mongo_shard_UK" } )

... "msg" : "removeshard completed successfully",
"state" : "completed",
"shard" : "packt_mongo_shard_UK"
"ok" : 1
...

  1. Once we get to state as completed and ok as 1, it is safe to remove our packt_mongo_shard_UK shard.

Removing a shard is naturally more complicated than adding one. We need to allow some time, hope for the best, and plan for the worst when performing potentially destructive operations on our live cluster.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset