Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change the name of my replica set while mongod processes are running?

Tags:

mongodb

If so, how?

When I first created the replica set, I chose a confusing name; I'd like to change it now.

The replica set name is mentioned in /etc/mongod.conf, and I'm not sure when it reads/rereads that. Since the replica set name can also be passed in as a command-line parameter, I'm assuming (and currently testing) the following:

  • it just uses replica set name once on startup while syncing up with other nodes
  • I have to reconfigure, stop, change replSet CLI argument or replSet value in /etc/mongod.conf, then restart

In other words, I'm assuming the answer to my original question is "no, you must restart" or "no, replica set name is immutable". I'll probably figure this out soon enough since I'm trying it locally.

like image 759
Adam Monsen Avatar asked Jun 29 '12 17:06

Adam Monsen


People also ask

What is MongoDB replica set name?

On MongoDB Atlas project page, select the Deployment link on the left hand side menu. Under Processes tab, with Topology view you should see the replica set name (this should be the default view presented when you click on Deployment ). Generally, the replica set name is the cluster name plus -shard-0 .

How does replica set work in MongoDB?

MongoDB achieves replication by the use of replica set. A replica set is a group of mongod instances that host the same data set. In a replica, one node is primary node that receives all write operations. All other instances, such as secondaries, apply operations from the primary so that they have the same data set.

How do I reinitialize a replica set in MongoDB?

To reset the configuration, make sure every node in your replica set is stopped. Then delete the "local" database for every node. Once you are confident all nodes are not running and the local database are gone, start the mongod process again (of course using the --replSet flag).


2 Answers

Here's how to do it with downtime:

  1. Stop all the servers
    • If authentication is enabled, make sure it is disabled or changes to the local.system.replset collection may not be authorized.
  2. Start up each server without the --replSet option, pointing at the correct data directory.
  3. Update the local.system.replset doc on each server with the new replica set name. You have to change every server here.
    Here is how to update local.system.replset in mongo shell:

    use local  
    var doc = db.system.replset.findOne()  
    doc._id = 'NewReplicaSetName'
    db.system.replset.save(doc)  
    db.system.replset.remove({_id:'OldReplicaSetName'})
    
  4. Shut them down again.
  5. Change the /etc/mongod.conf replSet option to the new name on all the servers.
  6. Start them all up with the original options.
like image 172
kristina Avatar answered Nov 04 '22 23:11

kristina


kristina's answer is good, but step 3 could use a little more explanation. In your mongo shell, edit db.system.replset like this:

use local
var doc = db.system.replset.findOne()
doc._id = 'NewReplicaSetName'
db.system.replset.save(doc)
db.system.replset.remove({_id:'OldReplicaSetName'})
like image 32
Thomas Lane Avatar answered Nov 04 '22 22:11

Thomas Lane