Note: Converting a MongoDB replica set member into a standalone server is a destructive operation from the perspective of high availability. Before you proceed, ensure you understand the implications (no automatic failover, different read behavior, and possible data resynchronization needs).
Overview โ
This guide explains how to safely convert a MongoDB replica set member into a standalone mongod instance. It covers both converting a secondary (recommended) and a primary (requires extra care), verification steps, configuration examples, and troubleshooting.
Preconditions & checklist ๐ก
- Back up your data (mongodump or filesystem snapshots).
- Ensure you have access to the replica set primary and the member to be converted.
- Confirm MongoDB versions are compatible with your intended recovery plan.
- If the member participates in a sharded cluster, do NOT convert it in place without understanding shard metadata implications.
High-level approaches
- Convert a secondary (safer): remove it from the replica set, stop it, remove replication config, and restart as standalone.
- Convert a primary (riskier): step it down so another member becomes primary, then remove it and convert as above.
Step-by-step: Convert a secondary (recommended) ๐ง
- Connect to the primary and check replica set health:
// connect to primary
rs.status()
rs.printReplicationInfo && rs.printReplicationInfo()
- Ensure the secondary is up-to-date (caught up with primary):
// on primary
rs.status().members // check member states and optime
- Remove the secondary from the replica set:
// on primary
rs.remove('secondary-hostname:27017')
- Stop mongod on the removed host:
# systemd example
sudo systemctl stop mongod
- Edit
mongod.confand remove or comment out thereplication:section (or removereplication.replSetName), for example:
# before
replication:
replSetName: rs0
# after
# replication:
# replSetName: rs0
- Restart mongod (now it will run as a standalone):
sudo systemctl start mongod
- Verify standalone mode:
// connect to the instance
db.isMaster() // should not include 'setName'
Step-by-step: Convert a primary (use caution) โ ๏ธ
- If possible, avoid making the current primary standalone in-place. Instead transfer primary role:
// on current primary
rs.stepDown(60) // step down for 60 seconds
- After the primary steps down and a new primary is elected, remove the host from the replica set as shown earlier:
// on the new primary
rs.remove('old-primary-host:27017')
- Then follow the same stop/edit/restart steps from the secondary conversion.
If there are no other data-bearing members and you still convert the lone primary to standalone, note that your deployment will no longer be a replica set: clients expecting a replica set may failover or read differently.
Special cases & considerations
- Sharded clusters: do not convert shard replica members without following sharding migration steps (drain, remove shard, etc.).
- Single-member replica set: you can run a single-member replica set if you need local writes with the same semantics, but it still differs from a true standalone in some tooling and monitoring flows.
- Data consistency: if data divergence is suspected after conversion, restore from a known good backup or resync from a healthy member.
Verification & useful commands โ
// verify replica set health from any member
rs.status()
// check server role
db.isMaster()
// view current config
rs.conf()
After converting to standalone, rs.status() will not show a valid replica set status for that instance and db.isMaster() will not return setName.
Troubleshooting
- mongod refuses to start after removing replSet: check logs (
/var/log/mongodb/mongod.log) for startup errors. - If the instance re-joins the replica set unexpectedly, ensure other members’ configs do not reference it and that you restarted without
--replSetorreplication.replSetNameinmongod.conf. - If data seems inconsistent, do not use the instance for writes; restore from backup or re-initialize from a healthy member.
Summary โ
Converting a secondary is straightforward and safest: remove it from the replica set, then restart without replication configured. Converting a primary requires you to step it down first and ensure you don’t accidentally break availability. Always backup and plan downtime.
Tip: Keep a tested backup and a rollback plan before making topology changes in production.
See also
- MongoDB documentation: replica set administration and
rscommands
Comments