mongo备份失败
mongodump -h 127.0.0.1:27017 -d stu -u python -p python -o /opt
报错:
2019-08-01T03:05:21.655-0700 Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.
更改如下:
mongodump -h 127.0.0.1:27017 -d stu -u python -p python --authenticationDatabase admin -o /opt
成功
备份恢复
mongorestore -h 127.0.0.1:27017 -u python -p python -d haha --dir /opt/stu
报错:
Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.
然后执行如下:
mongorestore -h 127.0.0.1:27017 -u python -p python --authenticationDatabase admin -d haha --dir /opt/stu
成功
二:数据的导入与导出
导出
模板:
mongoexport -h dbhost -d dbname -c colname -o filename --type json/csv -f field
-h: 服务器地址
-d: 数据库名
-c: 集合名
-o: 导出文件名
--type: 文件类型,默认json格式,可选数据类型json,csv
-f: 需要导出的字段,导出为json格式的数据时可以不指定导出哪些字段,默认全部,导出成csv文件是必须指定
mongoexport -h 127.0.0.1:27017 -u python -p python -d haha --authenticationDatabase admin -c stu -o /opt/haha.json --type json
导入
模板:
mongoimport -d dbname -c collectionname --file filename --headerline --type json/csv -f field
-d :数据库名
-c :collection名
--type :导入的格式默认json
-f :导入的字段名
--headerline :如果导入的格式是csv,则可以使用第一行的标题作为导入的字段
--file :要导入的文件
eg:mongoimport -d two -c newstu --file ./test.json --type json
eg:mongoimport -d two -c newstu --file ./test.csv --type csv --headerline
mongoimport -d haha -u python -p python --authenticationDatabase admin -c jjj --file ./haha.json --type json