1. 下载工具 MongoDB Database Tools
2. 将下载的文件解压到指定地方,如:D:/xx/
3. 进入解压后的文件夹下的 bin 目录 D:/xx/mongodb-database-tools-windows-x86_64-100.9.0/bin
备份
备份命令
mongodump -h <hostname><:port> <-u> <用户名> <-p> <密码> -d dbname -o dbdirectory
出现 Authentication failed
添加 --authenticationDatabase admin
选项
mongodump -h <hostname><:port> <-u> <用户名> <-p> <密码> -d dbname <--authenticationDatabase > <admin> -o dbdirectory
- 全库备份
mongodump -h -u -p --port 27017 -o /tmp/backup
- 备份指定的库
mongodump -h -u admin -p 123456 --port 27017 -d dbname -o /tmp/test
- 压缩备份
# 压缩备份单库 mongodump -h -u -p --port 27017 -d test -o /tmp/backup --gzip # 压缩备份单表 mongodump -h -u -p --port 27017 -d test -c customer -o /tmp/backup --gzip
恢复
恢复命令
mongorestore -h <hostname><:port> -d dbname <path>
- 全库恢复
mongorestore -h -u -p --port 27017 /tmp/backup/test
- 单库恢复
mongorestore -h -u -p --port 27017 -d dbname /tmp/backup/test
- 单表恢复
mongorestore -h -u -p --port 27017 -d dbname -c collectionname /tmp/back
-
gzip文件恢复
# 全库恢复 mongorestore -h -u -p --port 27017 --gzip /tmp/backup # 单库恢复 mongorestore -h 127.0.0.1 -u admin -p 123456 --port 27017 -d dbname --gzip /tmp/backup
恢复问题
2022-07-07T16:01:44.751+0800 The –db and –collection flags are deprecated for this use-case; please use –nsInclude instead, i.e. with –nsInclude=${DATABASE}.${COLLECTION} 2022-07-07T16:01:44.751+0800 building a list of collections to restore from test dir 2022-07-07T16:01:44.751+0800 don’t know what to do with subdirectory “test/test”, skipping… 2022-07-07T16:01:44.752+0800 0 document(s) restored successfully. 0 document(s) failed to restore.
没有任何报错,但是也没有恢复任何数据。
mongorestore -u admin -p 123456 --port 27017 --nsInclude="*" test
可以看到恢复1个document
版本升级
mongo不支持跨版本升级
升级过程 4.2.3→4.4→5.0→6.0.2
查看当前功能兼容性版本
db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
设置功能兼容性版本
db.adminCommand( { setFeatureCompatibilityVersion: "4.4" } )
db.adminCommand( { setFeatureCompatibilityVersion: "5.0" } )
db.adminCommand( { setFeatureCompatibilityVersion: "6.0" } )
参考链接: