MongoDB的备份和恢复

236 阅读1分钟

如果设置了环境变量直接输入,没有环境变量则需要进入到mongo Bin

备份的命令

mongodump -h  参数(ip地址,本机127.0.0.1) -d  参数(数据库名字) -u   参数(用户名) -p   参数(密码) -o  参数(备份到哪里)

恢复的命令

mongorestore -h  参数(默认为127.0.0.1:27017,默认可以不写) -d  参数(数据库名字,可以和导出的时候不一致) 参数(备份的数据库地址,比如tables数据库备份到c:\back中,恢复时地址为c:\back\tables)

常犯的错误

是因为我的库开启了验证需要指定的账户才有权限

2020-05-28T16:39:56.088+0800    the --db and --collection args should only be us
ed when restoring from a BSON file. Other uses are deprecated and will not exist
 in the future; use --nsInclude instead
2020-05-28T16:39:56.135+0800    building a list of collections to restore from C
:\昌黎\$$$文件夹7602356\backup\mongoBack1906281149\smartonet dir
2020-05-28T16:39:56.141+0800    finished restoring smartonet.ST32_data (0 docume
nts, 0 failures)
2020-05-28T16:39:56.141+0800    Failed: smartonet.ST32_data: error reading datab
ase: (Unauthorized) not authorized on smartonet to execute command { listCollect
ions: 1, filter: {}, lsid: { id: UUID("00ed8dfe-dadd-406a-9764-f03515220dc8") },
 $db: "smartonet" }
2020-05-28T16:39:56.142+0800    0 document(s) restored successfully. 0 document(
s) failed to restore.

到bin目录下

mongorestore -h 127.0.0.1:27017 -u 账户 -p 密码 -d 想恢复到哪个库(没有就创建) 路径