database runtime_error ai_generated partial

MongoServerError: no primary found in replica set, cannot perform operation

ID: database/mongodb-replica-set-no-primary

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
1Evidence
2024-05-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
MongoDB 6.0 active — — —
MongoDB 7.0 active — — —
MongoDB 8.0 active — — —

Root Cause

No member of the MongoDB replica set is in PRIMARY state, often due to an election failure, network partition, or majority loss.

generic

中文

MongoDB副本集中没有成员处于PRIMARY状态,通常是由于选举失败、网络分区或多数节点丢失。

Official Documentation

https://www.mongodb.com/docs/manual/reference/method/rs.status/

Workarounds

  1. 80% success Check replica set status: rs.status(); identify which nodes are up and their states. If a node is SECONDARY but has high replication lag, wait for it to catch up.
    Check replica set status: rs.status(); identify which nodes are up and their states. If a node is SECONDARY but has high replication lag, wait for it to catch up.
  2. 75% success Force re-election by connecting to a secondary and running: rs.reconfigForPSASet(); or manually reconfigure with rs.reconfig() to set a new primary.
    Force re-election by connecting to a secondary and running: rs.reconfigForPSASet(); or manually reconfigure with rs.reconfig() to set a new primary.
  3. 85% success If a node is in ROLLBACK state, allow rollback to complete: use rs.stepDown() on the former primary to let the rollback finish.
    If a node is in ROLLBACK state, allow rollback to complete: use rs.stepDown() on the former primary to let the rollback finish.

中文步骤

  1. Check replica set status: rs.status(); identify which nodes are up and their states. If a node is SECONDARY but has high replication lag, wait for it to catch up.
  2. Force re-election by connecting to a secondary and running: rs.reconfigForPSASet(); or manually reconfigure with rs.reconfig() to set a new primary.
  3. If a node is in ROLLBACK state, allow rollback to complete: use rs.stepDown() on the former primary to let the rollback finish.

Dead Ends

Common approaches that don't work:

  1. Restart all MongoDB instances in the replica set simultaneously 70% fail

    Restarting all nodes at once can cause a complete outage and may not resolve underlying election issues; a step restart is safer.

  2. Forcibly step down a secondary with rs.stepDown() to trigger election 60% fail

    If no qualified secondary exists (e.g., all are behind in replication), stepDown() will not elect a new primary.