Rename Mongo Shard

mongodbsharding

Can I, and if I can, how can I rename a shard in Mongo?

Like if I wanted to change the instances of rs0 to rep0 below:

mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
    "_id" : 1,
    "version" : 4,
    "minCompatibleVersion" : 4,
    "currentVersion" : 5,
    "clusterId" : ObjectId("111111111111")
}
  shards:
    {  "_id" : "rs0",  "host" : "rs0/mongo0a:27017,mongo0b:27017" }
...

I have thought about removing and re-adding the shard, but I'm not sure how I'd do that without having to drain the shard and drop dbs.

Currently 0 of the collections have sharding enabled, I just have a few standalones added as shards.

Thanks

Best Answer

No, there is no supported way to rename a shard at present, though as you mention you could remove and re-add. Even then it is not as simple as you might think because although you can specify a name when adding a shard, it does not end there - there is the replica set itself to worry about. The name specification when adding is just the value of the _id, see the following example (my replica set is rs0, like yours):

mongos> db.adminCommand({addShard : "rs0/host.example.com:27017,host.example.com:27018", name : "rep0"});

mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
    "_id" : 1,
    "version" : 4,
    "minCompatibleVersion" : 4,
    "currentVersion" : 5,
    "clusterId" : ObjectId("539838845bc6bf5ee52a56ea")
}
  shards:
    {  "_id" : "rep0",  "host" : "rs0/host.example.com:27017,host.example.com:27018" }
  databases:
    {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }

Note that all that has changed is the _id for the shard, the "host" value remains the same, because rs0 is the name of the replica set - if you try to use rep0 there it will fail to add. Hence, all that a remove and re-add will give you is a mismatch between the two names.

To change that host value, not only do you have to remove/re-add the shard, you also have to alter the replica set config before you re-add the shard. In other words, the replSet parameter must be changed to be rep0 also, and that means re-initializing the set - not an easy task either.

Overall, it is possible to get to where you want, but there will be a large amount of work and it will not be quick (drains, re-init of the set), especially if you have a lot of data. For the sake of changing a couple of strings, I would generally recommend leaving them as-is.

Related Topic