Mongodb备份和还原

MongoDB数据库备份还原可以使用自带的mongodump和mongorestore命令,

命令行中用到的一些选项代表的含义,如下:
-h  数据库访问地址

--port  数据库端口

-u  用户名

-p  密码

--authenticationDatabase  账号认证库(一般账号属于哪个库就是用哪个库认证)

 

-d  数据库

-c  集合(表)

-o  备份输出目录

--备份单个表

/usr/local/mongodb/bin/mongodump -h 192.168.10.17 --port 37017 -u root -p password --authenticationDatabase admin -d testdb -c person -o myTest20190114

--备份单个库

/usr/local/mongodb/bin/mongodump -h 192.168.10.17 --port 37017 -u root -p password --authenticationDatabase admin -d testdb -o myTest20190114

--恢复单个表

/usr/local/mongodb/bin/mongorestore -h 192.168.10.17 --port 37017 -u root -p password --authenticationDatabase admin -d test2019 -c person myTest20190114/test/person.bson

--恢复单个库

/usr/local/mongodb/bin/mongorestore -h 192.168.10.17 --port 37017 -u root -p password  --authenticationDatabase admin -d test201901 backup20190114/test/

--备份所有库

/usr/local/mongodb/bin/mongodump -h 192.168.10.17 --port 37017 -u root -p password --authenticationDatabase admin -o myTest20190114

--恢复所有库

/usr/local/mongodb/bin/mongorestore -h 192.168.10.17 --port 37017 -u root -p password  --authenticationDatabase admin backup20190114/

如果是副本集,则副本集备份还原全库稍微有些不同,
--备份所有库推荐使用添加--oplog参数的命令,这样的备份是基于某一时间点的快照,只能用于备份全部库时才可用,单库和单表不适用:

/usr/local/mongodb/bin/mongodump -h 192.168.10.17 --port 37017 -u root -p password --authenticationDatabase admin --oplog -o myTest20190114

--同时,恢复时也要加上--oplogReplay参数,具体命令如下(下面是恢复单库的命令):

 

posted @ 2019-01-31 15:11  重启一把梭  阅读(320)  评论(0编辑  收藏  举报