1、单库导出
./mongodump -h ip:port -u username -p password -d databasename -o pathToSave
示例
./mongodump -h 127.0.0.1:27017 -u uname -p pwd -d my_demo -o ../databak/
2、单库还原
./mongorestore -h ip:port -u uname -p pwd -d databasename path/my_demo [--drop]
./mongorestore -h ip:port [--authenticationDatabase admin -u admin -p adminpwd] -d databasename path/my_demo [--drop]
错误示例
./mongorestore -h 127.0.0.1:27017 -d my_demo ../databak/my_demo (×错误)
报错信息:the the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
正确示例
- 不删除原有数据的还原
./mongorestore -h 127.0.0.1:27017 -u uname -p pwd -d my_demo ../databak/my_demo (√正确)
- 删除原有数据的还原,加 --drop
./mongorestore -h 127.0.0.1:27017 -u uname -p pwd -d my_demo ../databak/my_demo --drop (√正确)
- 如果还原的时候报权限问题,需要加上管理员的账号密码 --authenticationDatabase admin -u admin -p adminpwd
auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed.
./mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -u admin -p adminpwd -d my_demo ../databak/my_demo --drop
更多推荐
MongoDB导入导出
发布评论