mongodb數(shù)據(jù)庫通過 mongoexport/mongoimport 及 mongodump/mongorestore 對表和數(shù)據(jù)庫進(jìn)行備份恢復(fù)操作蚁趁。
在4.4以上版本梆造,這些工具被剔除了基礎(chǔ)安裝包含滴,需額外從官網(wǎng)下載MongoDB Compass Download | MongoDB溶弟,解壓使用沙廉。
對集合的備份及回復(fù)
導(dǎo)出:
mongoexport -d dbname -c collectionname -o filepath --type json/csv -f field
-d:數(shù)據(jù)庫名
-c:集合名稱
-o : 導(dǎo)出數(shù)據(jù)文件的路徑
-type : 導(dǎo)出數(shù)據(jù)類型扶叉,默認(rèn)json
-f: type為csv時嗓节,需設(shè)置此項
導(dǎo)入:
mongoimport -d dbname -c collectionname --file filename --type json/csv -f field --drop filepath
--file : 選擇導(dǎo)入的文件
-f: type為csv時荧缘,需設(shè)置此項
--drop 先刪除所有的數(shù)據(jù),再恢復(fù)赦政,當(dāng)不需要刪除時可不加
對數(shù)據(jù)庫的備份及恢復(fù)
備份:mongodump -h dbhost -d dbname -o dbdirectory
恢復(fù):mongorestore -h dbhost -d dbname --dir dbdirectory --drop filepath
遠(yuǎn)程數(shù)據(jù)庫連接進(jìn)行遷移
指定遠(yuǎn)程數(shù)據(jù)庫IP胜宇,端口
./mongoXXXX -h IP --port port --authenticationDatabase admin -u user -p pwd -X XXXXX
默認(rèn)-h 的IP是127.0.0.1,連接本地數(shù)據(jù)庫恢着。
當(dāng)數(shù)據(jù)庫創(chuàng)建了超級管理員時桐愉,需添加參數(shù) --authenticationDatabase admin,否則報錯:unable to authenticate using mechanism “SCRAM-SHA-1”掰派。