mongodb數(shù)據(jù)遷移

mongodb數(shù)據(jù)庫通過 mongoexport/mongoimport 及 mongodump/mongorestore 對表和數(shù)據(jù)庫進行備份恢復(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

遠程數(shù)據(jù)庫連接進行遷移

指定遠程數(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”。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容