You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MongoDB 6.0修复失效无法启动,无mongodump如何备份数据库?

问题概述

MongoDB 6.0实例中某集合出现硬件级损坏,执行mongod --repair无效;删除损坏集合后触发断言错误,无法启动服务,需恢复未损坏集合或备份可用数据。

错误日志

存储层校验错误

{"t":{"$date":"2022-10-23T10:34:56.814+03:00"},"s":"E",  "c":"STORAGE",  "id":22435,   "ctx":"initandlisten","msg":"WiredTiger error","attr":{"error":0,"message":"[1666510496:814423][245805:0x7f99fc399c80], file:collection-591-2337122025107551252.wt, WT_CURSOR.next: __wt_block_read_off, 296: collection-591-2337122025107551252.wt: potential hardware corruption, read checksum error for 16384B block at offset 4537556992: calculated block checksum doesn't match expected checksum"}}

断言失败错误

{"t":{"$date":"2022-10-23T11:13:48.644+03:00"},"s":"F",  "c":"-",        "id":23081,   "ctx":"initandlisten","msg":"Invariant failure","attr":{"expr":"buildUUID","msg":"collection: mydatabase.birds_collection:_id_","file":"src/mongo/db/catalog/index_catalog_impl.cpp","line":169}}
{"t":{"$date":"2022-10-23T11:13:48.644+03:00"},"s":"F",  "c":"-",        "id":23082,   "ctx":"initandlisten","msg":"\n\n***aborting after invariant() failure\n\n"}
{"t":{"$date":"2022-10-23T11:13:48.646+03:00"},"s":"F",  "c":"CONTROL",  "id":6384300, "ctx":"initandlisten","msg":"Writing fatal message","attr":{"message":"Got signal: 6 (Aborted).\n"}}
恢复未损坏集合的操作步骤

1. 强制启动并导出可用数据

  • 执行以下命令强制启动MongoDB,绕过损坏的索引和集合校验:
    mongod --dbpath /path/to/your/data/directory --repairpath /path/to/temporary/repair/dir --forceRecovery
    
  • 若服务成功启动,立即用mongodump导出排除损坏集合的数据库数据:
    mongodump --db mydatabase --excludeCollection birds_collection
    
  • 导出完成后关闭服务,清理原数据目录,新建MongoDB实例并导入备份数据。

2. 手动清理损坏文件后重建元数据

  • 停止MongoDB服务,复制整个数据库目录到安全位置做全量备份。
  • 在原数据库目录中,删除损坏集合对应的collection-xxx-xxx.wt(数据文件)和index-xxx-xxx.wt(索引文件),同时删除_mdb_catalog.wt(目录元数据文件)。
  • 再次用--forceRecovery参数启动MongoDB,服务会自动重建目录元数据,识别剩余未损坏集合。
  • 启动成功后立即导出所有可用集合。
无法启动时的直接数据备份方案

如果上述方法无法启动服务,使用MongoDB内置的WiredTiger工具直接提取数据:

  • 找到MongoDB安装目录下的wt工具(通常位于bin文件夹)。
  • 进入目标数据库的文件夹(如/path/to/data/mydatabase),执行以下命令:
    # 验证集合文件是否可读取
    wt -h . verify collection-xxx-xxx.wt
    # 导出可读取的集合数据到CSV文件
    wt -h . dump collection-xxx-xxx.wt > valid_collection_data.csv
    
  • 导出的CSV文件可通过mongoimport工具导入到新的MongoDB实例中。

内容的提问来源于stack exchange,提问作者Axel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 01:31:07