MongoDB 备份还原

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" } )

参考链接:

Upgrade a Replica Set to 5.0

Upgrade a Replica Set to 6.0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值