Adam Monsen
Adam Monsen

Reputation: 9420

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

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:

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.

Upvotes: 10

Views: 15356

Answers (4)

B. Naga Lakshmi
B. Naga Lakshmi

Reputation: 627

My answer will be useful for those running master-slave replication using MongoDB Docker containers.

To rename your MongoDB replica set across two Docker Compose files (one for the primary and one for the secondary) from `rs1` to `rs2`, follow these steps:

### 1. **Shut Down the Replica Set Members**

1. **Stop Containers**:
   - On both servers, stop the MongoDB containers.
   ```bash
   docker-compose down
   ```

### 2. **Rename the Replica Set on both master and slave servers**

**Note:** Follow the below steps on both the primary and secondary servers.

1. **Start Containers Without Replica Set Configuration**:
   - Start the containers without the `--replSet` option:
     ```yaml
     command: mongod --bind_ip_all
     ```

2. **Connect to the MongoDB Instance**:
   - Use `mongo` shell to connect to the MongoDB instance.
   ```bash
   docker exec -it mongo-server mongo
   ```

3. **Update the Replica Set Name**:
   - Execute the following commands to rename the replica set:
     ```javascript
     var newId = 'rs2';
     var doc = db.getSiblingDB("local").system.replset.findOne();
     var oldId = doc._id;
     doc._id = newId;
     db.getSiblingDB("local").system.replset.insertOne(doc);
     db.getSiblingDB("local").system.replset.deleteOne({_id: oldId});
     ```

4. **Shut Down the Containers Again**:
   - After the update, shut down the MongoDB containers again.
     ```bash
     docker-compose down
     ```

5. **Restore the Original Ports and Reconfigure Replica Set**:
   - Restore the original port mappings in the `docker-compose.yml` files.
   - Start the containers with the `--replSet` option using the new replica set name:
     ```yaml
     command: mongod --replSet "rs2" --bind_ip_all
     ```

6. **Start Containers**:
   - Start the MongoDB containers on both servers with the updated configuration:
     ```bash
     docker-compose up -d
     ```

### 3. **Verify the Replica Set**

1. **Connect to the MongoDB Instance**:
   - Connect to the MongoDB instance and check the replica set status:
     ```bash
     docker exec -it mongo-server mongo
     ```

2. **Verify Replica Set Name**:
   - Run the following command in the `mongo` shell:
     ```javascript
     rs.status() or rs.status().set
     ```
   - Ensure that the replica set name is updated to `rs2` and all members are listed.

This process ensures that your replica set is renamed correctly and all nodes are synchronized with the new configuration.

Upvotes: 0

kris
kris

Reputation: 23592

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.

Upvotes: 37

Thomas Lane
Thomas Lane

Reputation: 81

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'})

Upvotes: 8

Adam Monsen
Adam Monsen

Reputation: 9420

No.

See thread "Change replica set name" in mongodb-user... Kristina Chodorow asserts that downtime is necessary.

I'll add instructions on how to change the replica set name here (with downtime) once I figure it out (unless someone else beats me to it).

Upvotes: 0

Related Questions