How do I rename a mongodb replica set?
Yes! The process is pretty simple:
- Start all of the nodes in the non-replicated mode
- Stop
mongod
on each server - Start
mongod
back up. If you use/etc/mongod.conf
, remove thereplication
section. If you don't, omit the--replSet
option tomongod
- Stop
Flush the
local
database where the replication set configuration is cached- On each server, open a mongo shell as the admin user and run
use local; db.dropDatabase()
(Make sure that admin user / root user hasdbAdmin
role onlocal
db)
- On each server, open a mongo shell as the admin user and run
Start all of the nodes again in replicated mode
- Stop
mongod
on each server - If you use
/etc/mongod.conf
, add thereplication
section back in with the new name and startmongod
. If not, startmongod
with--replSet <new-name>
- Stop
- Initialize the replica set
- Open a mongo shell as the admin user on one of the nodes. (It will become the new primary)
- Run
rs.initiate()
. DO NOT pass any arguments tors.initiate()
. (It'll fail with an error) Any other config you want to set can be changed usingrs.reconfig()
later. - On the same node where your ran
rs.initiate()
, for each secondary, runrs.add('[secondary.host.name]')
to add it to the replica set. - Wait for the secondaries to come in sync
This doesn't require you to dump and re-import your data, and it can be done with minimal downtime (and a period of degraded performance as the secondaries sync) if you automate it.
Here's an ansible playbook that does the whole thing (assuming you're using /etc/mongod.conf
and managing mongod
via System V/Upstart/things that speak service
.)