Ubuntu上MongoDB数据如何恢复
导读:Ubuntu 上 MongoDB 数据恢复指南 一 场景与准备 明确备份类型: 逻辑备份(由 mongodump 生成,目录内含 .bson 与 .metadata.json 文件),适合跨版本迁移与库/集合级恢复。 物理备份(拷贝 W...
Ubuntu 上 MongoDB 数据恢复指南
一 场景与准备
- 明确备份类型:
- 逻辑备份(由 mongodump 生成,目录内含 .bson 与 .metadata.json 文件),适合跨版本迁移与库/集合级恢复。
- 物理备份(拷贝 WiredTiger 数据目录等),适合同版本、同存储引擎的快速恢复。
- 版本与工具兼容:恢复工具的版本需与目标 MongoDB 版本匹配,避免 mongorestore 不兼容导致失败。
- 权限与服务:准备好具有相应权限的账号(常见为 admin 库用户),并确认 mongod 服务可正常启动。
- 目标环境:确保目标机器的 Ubuntu、存储路径、端口与资源配置满足恢复需求。
二 使用 mongorestore 恢复逻辑备份(最常见)
- 基本步骤
- 安装与备份一致版本的 MongoDB(含 mongorestore)。
- 如在生产环境操作,建议先停写或切换到维护窗口,必要时对现有数据做逻辑备份:
- 停止服务:
sudo systemctl stop mongod - 备份现有数据:
mongodump --out /path/to/backup/folder
- 停止服务:
- 执行恢复(示例):
- 恢复整个数据库目录:
mongorestore --uri='mongodb://127.0.0.1:27017/?authSource=admin' /path/to/backup/folder - 恢复单库:
mongorestore --uri='mongodb://127.0.0.1:27017/?authSource=admin' --db mydb /path/to/backup/folder/mydb - 恢复单表:
mongorestore --uri='mongodb://127.0.0.1:27017/?authSource=admin' --db mydb --collection mycol /path/to/backup/folder/mydb/mycol.bson - 覆盖写入(谨慎):在命令中加入 –drop,先删除同名集合再恢复。
- 恢复整个数据库目录:
- 启动服务并校验:
- 启动:
sudo systemctl start mongod - 校验:
mongo --host 127.0.0.1 -u < user> -p < pwd> --authenticationDatabase admin,在 shell 中执行show dbs、use < db>、db.< col> .countDocuments({ } )。
- 启动:
- 常见要点
- 认证库通常为 admin,请在 URI 中使用
?authSource=admin。 - 分片集群的云盘逻辑备份解压后不含路由信息,可恢复到任意单节点/副本集/分片架构,但分片需自行预分片。
- 若下载的备份被切分为多个 .bson 分片,需先合并再恢复(见下文“分片 bson 合并”脚本)。
- 认证库通常为 admin,请在 URI 中使用
三 分片备份的 bson 合并与恢复
- 现象:下载的库表目录中,集合数据可能被拆成多个 data/*.bson 分片(如:myCollection_0_part0.bson)。mongorestore 会受文件名前缀影响,直接恢复可能失败或重复。
- 处理思路:将同一集合的多个 .bson 合并为一个,再恢复。
- 合并脚本(Python 3,保存为 merge_bson_files.py):
import os, shutil def merge_bson_dir(input_dir: str, output_dir: str): for root, dirs, files in os.walk(input_dir): for f in files: if f.endswith('.bson') and not f.endswith('.metadata.json'): rel = os.path.relpath(root, input_dir) ns = rel.replace(os.sep, '.') # db.collection out_dir = os.path.join(output_dir, rel) os.makedirs(out_dir, exist_ok=True) out_file = os.path.join(out_dir, f"{ ns} .bson") with open(out_file, 'ab') as of: with open(os.path.join(root, f), 'rb') as inf: shutil.copyfileobj(inf, of) # 复制元数据 meta = os.path.join(root, f"{ f} .metadata.json") if os.path.exists(meta): shutil.copy2(meta, os.path.join(out_dir, f"{ ns} .bson.metadata.json")) if __name__ == '__main__': import argparse p = argparse.ArgumentParser() p.add_argument('input_root') p.add_argument('-o', '--output_root', default=None) args = p.parse_args() merge_bson_files_dir(args.input_root, args.output_root or args.input_root) - 使用方式:
- 合并:
python3 merge_bson_files.py /path/to/backup/folder -o /path/to/merged - 恢复:
mongorestore --uri='mongodb://127.0.0.1:27017/?authSource=admin' /path/to/merged
- 合并:
- 说明:空表也会生成包含库表名的空 .bson,可直接被 mongorestore 处理。
四 物理备份恢复 WiredTiger 数据目录(同版本)
- 适用:同版本、同存储引擎(如 WiredTiger)的物理恢复,常用于整机/目录级灾难恢复。
- 基本步骤
- 安装与目标实例版本一致的 MongoDB。
- 准备配置文件(示例,存储引擎为 WiredTiger):
systemLog: destination: file path: /test/mongo/mongod.log logAppend: true security: authorization: enabled storage: dbPath: /test/mongo/data directoryPerDB: true net: port: 27017 unixDomainSocket: enabled: false processManagement: fork: true pidFilePath: /test/mongo/mongod.pid - 使用准备好的数据文件替换/放置到 dbPath(如 /test/mongo/data),确保文件属主与权限正确(如 mongodb:mongodb)。
- 启动并检查:
- 启动:
mongod -f /test/mongo/mongod.conf - 校验:
mongo --host 127.0.0.1 -u < user> -p < pwd> --authenticationDatabase admin,执行show dbs等。
- 启动:
- 重要限制
- 版本需严格匹配;存储引擎需一致(如 WiredTiger)。
- 如为 RocksDB 引擎,需使用带 RocksDB 编译的 MongoDB 版本。
- 副本集的物理备份可能带有原实例的副本集配置,需清理后再以副本集模式启动。
五 常见问题与排错
- 版本不兼容:出现 “Unrecognized BSON type/Invalid BSON” 等,多为 mongorestore 版本过低,升级到与目标库一致的版本后重试。
- 认证失败:确认 –authenticationDatabase=admin 与账号权限;密码含特殊字符请用单引号包裹。
- 集合未恢复或重复:分片/并行导出的 .bson 未合并,按上文脚本合并后再恢复。
- 恢复后数据不一致:备份期间写入导致不一致,建议在低峰期执行,或使用复制集/事务机制提升一致性。
- 物理恢复启动失败:检查 dbPath 权限、目录是否为空、配置文件参数(如 directoryPerDB)、以及 WiredTiger 版本匹配。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Ubuntu上MongoDB数据如何恢复
本文地址: https://pptw.com/jishu/753195.html
