Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 1 down vote accepted

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.

share|improve this answer

This is probably not supported. However, I have found a way to rename the shards. Use at your own risk.

Basically, the shard name appears in the config servers in the following collections: config.databases, config.chunks, and config.shards.

Do a simple update to replace the fields that have the old shard name with the new shard name. You'll have to repeat this on all config servers. Shut down the shard servers and mongos clients first. An example is below:

$ mongo <configserver>:<port>/config
configsvr> db.databases.update({primary:'old_shard_name'},{$set:{primary:'new_shard_name'}},{multi:true})
configsvr> db.chunks.update({shard:'old_shard_name'},{$set:{shard:'new_shard_name'}},{multi:true})
configsvr> var temp = db.shards.findOne({_id:'old_shard_name'})
configsvr> temp._id = 'new_shard_name'
configsvr> db.shards.remove({_id:'old_shard_name'})
configsvr> db.shards.insert(temp)

If you are changing the replica set name at the same time, you can modify the "hosts" field of the record in the config.shards collection at the same time.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.