Renaming mongo shards

From lxadm | Linux administration tips, tutorials, HOWTOs and articles
Jump to: navigation, search

Mongo has mostly excellent documentation, yet sometimes it may not be obvious how to do some less-used configuration changes. One of these is a way to rename mongo shards.

Renaming mongo shards can be done in the following way:

  • assume you have “non-portable” naming like this – if your server addresses change for some reason, you’re in trouble:
mongos> use admin
switched to db admin

mongos> db.runCommand({listshards:1})
{
"shards" : [
{
"_id" : "shard0001",
"host" : "ec2-aa-bb-cc-dd.compute-1.amazonaws.com:27018"
},
{
"_id" : "shard0002",
"host" : "ec2-bb-cc-dd-ee.compute-1.amazonaws.com:27018"
},
{
"_id" : "shard0003",
"host" : "10.11.12.13:27018"
                }
        ],
        "ok" : 1
}


  • let’s say you want to rename them to shard0001.example.com, shard0002.example.com, shard0003.example.com:
mongos> db.getSiblingDB("config").shards.save( {_id: "shard0001", host: "shard0001.example.com:27018" } )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

mongos> db.getSiblingDB("config").shards.save( {_id: "shard0002", host: "shard0002.example.com:27018" } )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

mongos> db.getSiblingDB("config").shards.save( {_id: "shard0003", host: "shard0003.example.com:27018" } )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
mongos> db.runCommand({listshards:1})
{
“shards” : [
{
“_id” : “shard0001”,

                        "host" : "shard0001.example.com:27018"
                },
                {
                        "_id" : "shard0002",
                        "host" : "shard0002.example.com:27018"
                },
                {
                        "_id" : "shard0003",
                        "host" : "shard0003.example.com:27018"
                }
        ],
        "ok" : 1


  • The last step is restarting all mongos instances; if possible, also restart mongod instances.