简体   繁体   中英

How do I rename a mongodb replica set?

I started up a few MongoDB replica sets with the default configuration and added them to Cloud Manager , but since the default replica set name is "rs0", they're hard to tell apart in the UI. Is there a way to change the name of a replica set, preferably without having to drop and re-import all of the data in the cluster?

I've tried using rs.reconfig ( link ) but it doesn't allow you to change the replica set name.

Yes! The process is pretty simple:

  1. 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 the replication section. If you don't, omit the --replSet option to mongod
  2. 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() use local; db.dropDatabase() (Make sure that admin user / root user has dbAdmin role on local db)
  3. Start all of the nodes again in replicated mode

    • Stop mongod on each server
    • If you use /etc/mongod.conf , add the replication section back in with the new name and start mongod . If not, start mongod with --replSet <new-name>
  4. 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 to rs.initiate() . (It'll fail with an error) Any other config you want to set can be changed using rs.reconfig() later.
    • On the same node where your ran rs.initiate() , for each secondary, run rs.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 .)

You could remove the existing replica set by following below steps and then can start the mongods with new replica set

  1. start all mongods without --replSet

    mongod --dbpath data/rs3 --port 27019 --oplogSize 64
    Note: - Above command is for windows machine

  2. start mongo for all the respective mongods remove the existing replica set use local
    db.system.replset.remove({_id:'OldReplicaSetName'})

  3. start each mongod with the new replica set with --replSet ex - mongod --dbpath data/rs3 --port 27019 --oplogSize 64 --replSet mySet Note: - Above command is for windows machine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM