What's Actually Happening
MongoDB replica set is stuck in an inconsistent state. Elections not completing or members not syncing.
The Error You'll See
rs.status()
{
"members": [
{
"name": "mongo1:27017",
"stateStr": "PRIMARY",
"health": 1
},
{
"name": "mongo2:27017",
"stateStr": "STARTUP2", // Stuck!
"health": 1
}
]
}Why This Happens
- 1.Network partition - Members cannot communicate
- 2.Election tie - Equal votes, no majority
- 3.Oplog too small - Initial sync failing
- 4.Hostnames wrong - DNS resolution issues
- 5.Arbiter missing - Not enough voters
Step 1: Check Replica Set Status
```javascript // Check status: rs.status()
// Check config: rs.conf()
// Check member health: rs.status().members.forEach(m => print(m.name + " " + m.stateStr)) ```
Step 2: Fix Network Issues
```bash # Test connectivity between members: mongo1: ping mongo2
# Check firewall: iptables -L -n | grep 27017
# Check MongoDB port: netstat -tlnp | grep 27017 ```
Step 3: Force Reconfigure
```javascript // Get current config: cfg = rs.conf()
// Fix hostnames if needed: cfg.members[0].host = "mongo1.example.com:27017"
// Force reconfig: rs.reconfig(cfg, {force: true}) ```
Step 4: Add Arbiter
// If odd number of members needed:
rs.addArb("arbiter:27017")MongoDB Replica Set Checklist
| Check | Command | Expected |
|---|---|---|
| Primary exists | rs.status() | One PRIMARY |
| All healthy | rs.status() | health: 1 |
| Network OK | ping | Connected |
Verify the Fix
rs.status()
// Output: All members PRIMARY or SECONDARYRelated Issues
- [Fix MongoDB Connection Timeout](/articles/fix-mongodb-connection-timeout)
- [Fix MongoDB Oplog Too Small](/articles/fix-mongodb-oplog-too-small)