← Back to list

Changing Replica Set Name in MongoDB

How to change the Replica set server hostnames and replica set name ?

murali Krishna · 2025-02-27 07:09 · 1 claps · 3.1 min read
#mongodb-replica-set #mongodb #nosql #database-administration #performance
Open on Medium ↗

How to Change the MongoDB Replica Set Name and Host names with Minimal Downtime?

Introduction

In multi-tenant MongoDB environments, teams often need to segregate shared data by creating new replica sets while ensuring minimal downtime. Instead of migrating data manually, a more efficient approach is to:

✅ Add hidden nodes to sync data before a maintenance window. ✅ Update the replica set name and server hostnames. ✅ Ensure secure replication using a different key file.

By following this step-by-step guide, you can efficiently reconfigure your MongoDB replica set without disrupting other teams or service production workloads

Step 1: Add Hidden Nodes for Minimal Downtime

To avoid downtime, add hidden nodes to the existing replica set. These nodes will sync in the background before migration.

1️⃣ Add a Hidden Node

Run the following command on your primary node to add a hidden secondary nodes:

rs.add({"host" :"new-hidden-node1.xx.local:27017",  "priority" : 0,  "hidden" : true})
rs.add({"host" :"new-hidden-node2.xx.local:27017",  "priority" : 0,  "hidden" : true})
rs.add({"host" :"new-hidden-node3.xx.local:27017",  "priority" : 0,  "hidden" : true})

This ensures that hidden nodes stay in sync without impacting read/write traffic and make sure to chain replication from secondary node to avoid load on primary . Re sync is recommended to avoid data compaction and reduce storage usage. If the dataset is too large, snapshot restoration is a better option for faster syncing.

2️⃣ Monitor Sync Progress

Check the sync status using:

rs.status();

Wait for replication lag to be 0 before proceeding and during the maintenance window remove the hidden nodes from shared replica set.

rs.remove("hiddennode1:27017");

rs.remove("hidennode2:27017");

rs.remove("hiddennode3:27017");

Step 2: Change the Replica Set Name

MongoDB does not allow renaming a replica set directly, so we need to update the system metadata.

1️⃣ Start MongoDB in Standalone Mode

Run the following command or directly u can comment replica set name and security parameters in /etc/mongod.conf

mongod --dbpath /data/db --port 27017 --replSet "" --bind_ip xx.xxx.xx

2️⃣ Update the Replica Set Name

Switch to the local database and modify the replica set name:

use local
var doc = db.system.replset.findOne({ "_id": "oldReplicaSet" });  
doc._id = "newReplicaSet";  
db.system.replset.insertOne(doc);
db.system.replset.remove({ _id: "oldReplicaSet" });

Replica set name changed successfully.

Step 3: Update Hostnames

Modify the hostnames to match the new environment.

For a Standard Setup (Primary + 2 Secondaries)

use local
cfg = db.system.replset.findOne({ "_id": "newReplicaSet" });
cfg.members[0].host = "mongo01.xx.xxx.xxx";
cfg.members[1].host = "mongo02.xx.xxxx.xx";
cfg.members[2].host = "mongo03.xx.xx.xx";
db.system.replset.update({ "_id": "newReplicaSet" }, cfg);

For a Setup with an Arbiter (Primary + Secondary + Arbiter)

use local
cfg = db.system.replset.findOne({ "_id": "newReplicaSet" });
cfg.members[2].host = "qa-mongo03.xx.xx.local";
cfg.members[2].arbiterOnly = true;  // Mark as an arbiter
db.system.replset.update({ "_id": "newReplicaSet" }, cfg);

Hostnames updated successfully.

New replica set initialized.

Step 5: Secure the New Replica Set

To prevent cross-communication between old and new replica sets, generate a new key file.

1️⃣ Generate a New Key File

openssl rand -base64 756 > /etc/mongod.key
chmod 600 /etc/mongod.key

2️⃣ Modify MongoDB Config File

Edit /etc/mongod.conf and set:

security:
    keyFile: /etc/mongod.key

Step 4: Restart MongoDB with the New Configuration

1️⃣ Stop MongoDB

sudo systemctl stop mongod

2️⃣ Restart with the new replica set name: By un comment the replica set name

sudo systemctl start mongod

New replica set is now isolated and secure.

Step 6: Validate the Configuration

Run:

rs.status();

Ensure: ✅ The new replica set name is in effect. ✅ All nodes have updated hostnames. ✅ Replication is working as expected

Summary: Steps to Change Replica Set Name & Hostnames

1 Add Hidden NodesSync data in the background to minimize downtime.

2️⃣ Change Replica Set NameStart MongoDB in standalone mode and update system.replset

3️⃣ Update HostnamesModify cfg.members[].host values. Restart MongoDBStart MongoDB with the new replica set name.

5️⃣ Secure the New Replica SetUse a different key file to avoid cross-communication.

6️⃣ Verify ConfigurationRun rs.status() to confirm everything is working.

🔹 Troubleshooting Guide

Issue: “Ghost Nodes” After Migration

Fix: Run:

rs.status();
rs.remove("ghost-node:27017");

Issue: Application Syncing with Wrong Nodes

Fix: Update the connection string in your application:

mongodb://node1:27017,node2:27017,node3:27017/?replicaSet=newReplicaSet

Issue: Hidden Node Becoming Primary

Fix: Set hidden node parameters:

cfg.members[3].hidden = true;
cfg.members[3].priority = 0;
rs.reconfig(cfg);

🔹 Validation Checklist

🔹 Application Connection Review: Ensure all services point to the correct replica set.

Conclusion:

By following this structured approach, you can safely rename your MongoDB replica set and update hostnames while ensuring: ✅ Minimal downtimeData consistencySecure cluster segregation

If you’re planning a MongoDB migration, this method ensures seamless transition with minimal production impact

Originally published at https://muralidba.blogspot.com on February 04, 2018

https://muralidba.blogspot.com/2018/04/how-to-change-replica-set-server.html


메타데이터
post_id
079180c4e67a
slug
changing-replica-set-name-in-mongodb-079180c4e67a
url
https://medium.com/@kmkrishna1223/changing-replica-set-name-in-mongodb-079180c4e67a
canonical_url
https://medium.com/@kmkrishna1223/changing-replica-set-name-in-mongodb-079180c4e67a
author_url
https://medium.com/@kmkrishna1223
status
ok
fetched_at
2026-06-11 05:11:55