How to change the Replica set server hostnames and replica set name ?
- Vivek Shukla
- Aug 27, 2022
- 1 min read
In day-to-day life as a DBA, we are required to restore latest backup from production environment to lower environments (Development, QA, Staging, Pre-production). To cater for this, we need to take the snapshot from production servers and restore it into lower envionment servers. This requires change the replica set names as well as hostnames.
How to change Replica set name?
To change the replica set name we need to start the mongodb in standalone mode and then execute the below commands.
use local
var cfg= db.system.replset.findOne({ "_id": "repl1" })
cfg._id = 'repl2'
db.system.replset.save(cfg)
db.system.replset.remove({_id:'repl1'})
How to change Hostname?
- One primary and two secondary servers
use local
cfg = db.system.replset.findOne( { "_id": "repl2" } )
cfg.members[0].host = "mongo1.prod.dx.local"
cfg.members[1].host = "mongo2.prod.dx.local"
cfg.members[2].host = "mongo3.prod.dx.local"
db.system.replset.update( { "_id": "repl2" } , cfg )- One primary, One secondary and one arbiter servers
use local
cfg = db.system.replset.findOne( { "_id": "repl2" } )
cfg.members[0].host = "qa-mongo1.qa.dx.local"
cfg.members[1].host = "qa-mongo2.qa.dx.local"
cfg.members[2].host = "qa-mongo3.qa.dx.local"
cfg.members[2].arbiterOnly=true
db.system.replset.update( { "_id": "repl2" } , cfg )
Comments