mongodb的备份与恢复

近日,公司需要对线上mongdb数据进行迁移,此处对操作过程以及过程中遇到的问题做一记录。(公司数据库port=20000,与官方端口27017有别)

一、备份

官方:mongodump -h dbhost -d dbname -o dbdirectory
实操:./mongodump -h 192.168.2.144:20000 -d monitor -o /db_back

以上/db_back路径需提前建好

问题1

如果开启了验证登录,是无法直接备份和恢复数据库的,报如下错误,(Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed也可试此办法,但未亲测)

解决办法:

1、添加:-u <username> -p=<password>

2、添加:--authenticationDatabase admin

./mongodump -h 192.168.2.144:20000 -d monitor -u monitor -p=monitor@123 -o /db_back --authenticationDatabase monitor

备份已完成,所得备份文件如下。

二、恢复

官方:mongorestore -h <hostname><:port> -d dbname <path>
实操:./mongorestore -h 192.168.2.144:20000 -d monitor -u monitor -p=monitor*** -o /db_back/monitor --authenticationDatabase monitor

问题2

版本问题,不支持 -o 选项,使用 --dre=<dbpath> 代替

./mongorestore -h 192.168.2.144:20000 -d monitor -u monitor -p=password  --dir=/db_back/monitor --authenticationDatabase monitor
##如恢复单个集合
./mongorestore -h 192.168.2.144:20000 -d monitor -u monitor -p=password --dir=/db_back/monitor/evaluate.archive.bson --authenticationDatabase monitor

恢复成功

原文地址:https://www.cnblogs.com/lansetuerqi/p/13428107.html