起因
因为我本地安装了mongodb
5版本,在给领导提交脚本的时候出现不兼容的问题。
领导让我装mongodb
4版本,卸载太麻烦了,于是想到使用docker
安装一个4版本。
下面是在windows
和docker
备份还原mongodb
的方法
备份还原
安装mongdb-database-tool(不安装命令无法执行)
https://www.mongodb.com/try/download/database-tools
备份命令
备份命令直接在cmd或者powershell执行
mongodump -h 127.0.0.1:27017 -d xykj_cpl -o "D:\dbback"
mongodump -h 127.0.0.1:27017 -d xykj_admin -o "D:\dbback"
mongodump -h 127.0.0.1:27017 -d xykj_analyze -o "D:\dbback"
mongodump -h 127.0.0.1:27017 -d xykj_xykj_cpl -o "D:\dbback"
mongodump -h 127.0.0.1:27017 -d xykj_pay -o "D:\dbback"
mongodump -h 127.0.0.1:27017 -d xykj_theme -o "D:\dbback"
mongodump -h 127.0.0.1:27017 -d xykj_user -o "D:\dbback"
mongodump -h 127.0.0.1:27017 -d xykj_vip -o "D:\dbback"
mongodump -h 127.0.0.1:27017 -d xykj_analyze -o "D:\dbback"
还原命令
还原的时候不是在mongo终端操作,需要在其他终端,比如cmd,powershell
windows还原操作
mongorestore -h 127.0.0.1:27017 -d xykj_cpl -u root -p test123 --dir D:\dbback\xykj_cpl --authenticationDatabase admin
mongorestore -h 127.0.0.1:27017 -d xykj_admin -u root -p test123 --dir D:\dbback\xykj_admin --authenticationDatabase admin
mongorestore -h 127.0.0.1:27017 -d xykj_analyze -u root -p test123 --dir D:\dbback\xykj_analyze --authenticationDatabase admin
mongorestore -h 127.0.0.1:27017 -d xykj_xykj_cpl -u root -p test123 --dir D:\dbback\xykj_xykj_cpl --authenticationDatabase admin
mongorestore -h 127.0.0.1:27017 -d xykj_pay -u root -p test123 --dir D:\dbback\xykj_pay --authenticationDatabase admin
mongorestore -h 127.0.0.1:27017 -d xykj_theme -u root -p test123 --dir D:\dbback\xykj_theme --authenticationDatabase admin
mongorestore -h 127.0.0.1:27017 -d xykj_user -u root -p test123 --dir D:\dbback\xykj_user --authenticationDatabase admin
mongorestore -h 127.0.0.1:27017 -d xykj_vip -u root -p test123 --dir D:\dbback\xykj_vip --authenticationDatabase admin
mongorestore -h 127.0.0.1:27017 -d xykj_vip2020 -u root -p test123 --dir D:\dbback\xykj_vip2020 --authenticationDatabase admin
docker还原操作
- 进入wsl
docker exec -it ContainerID bin/bash
- 先创建备份文件夹
mkdir dbback
- 复制备份到
docker container
对应的文件夹下创建的dbback
目录 cd data/dbback/
到备份文件目录- 执行下面还原命令
mongorestore -h 127.0.0.1:27017 -d xykj_xykj_cpl -u root -p test123 --dir xykj_xykj_cpl --authenticationDatabase admin
mongorestore -h 127.0.0.1:27017 -d xykj_cpl -u root -p test123 --dir xykj_cpl --authenticationDatabase admin
mongorestore -h 127.0.0.1:27017 -d xykj_admin -u root -p test123 --dir xykj_admin --authenticationDatabase admin
mongorestore -h 127.0.0.1:27017 -d xykj_analyze -u root -p test123 --dir xykj_analyze --authenticationDatabase admin
mongorestore -h 127.0.0.1:27017 -d xykj_pay -u root -p test123 --dir xykj_pay --authenticationDatabase admin
mongorestore -h 127.0.0.1:27017 -d xykj_theme -u root -p test123 --dir xykj_theme --authenticationDatabase admin
mongorestore -h 127.0.0.1:27017 -d xykj_user -u root -p test123 --dir xykj_user --authenticationDatabase admin
mongorestore -h 127.0.0.1:27017 -d xykj_vip -u root -p test123 --dir xykj_vip --authenticationDatabase admin
mongorestore -h 127.0.0.1:27017 -d xykj_vip2020 -u root -p test123 --dir xykj_vip2020 --authenticationDatabase admin