database runtime_error ai_generated partial

MongoServerError:在副本集中未找到主节点,无法执行操作

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

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

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
1证据数
2024-05-12首次发现

版本兼容性

版本状态引入弃用备注
MongoDB 6.0 active — — —
MongoDB 7.0 active — — —
MongoDB 8.0 active — — —

根因分析

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

English

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

generic

官方文档

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

解决方案

  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.

无效尝试

常见但无效的做法:

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

    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% 失败

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