# MongoServerError：RollbackDetected：副本集成员 rs1:27017 上发生回滚

- **ID:** `mongodb/rollback-detected-on-replica-set`
- **领域:** mongodb
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

副本集中的从节点发生了回滚，因为它应用了后来被主节点覆盖的写入操作。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| mongodb-5.0 | active | — | — |
| mongodb-6.0 | active | — | — |
| mongodb-7.0 | active | — | — |

## 解决方案

1. ```
   Check the rollback data in the rollback/ directory on the affected node and manually restore any necessary documents using mongorestore.
   ```
2. ```
   Ensure all secondary nodes have caught up with the primary by monitoring replication lag and increasing oplog size if needed.
   ```
3. ```
   Set writeConcern to majority to reduce the chance of rollbacks by ensuring writes are acknowledged by a majority of nodes.
   ```

## 无效尝试

- **** — Ignoring the rollback and continuing operations can cause data inconsistency between nodes. (50% 失败率)
- **** — Manually forcing the secondary to become primary without resolving the rollback can cause data loss. (50% 失败率)
- **** — Decreasing the oplog size to save disk space increases the likelihood of future rollbacks. (50% 失败率)
