1.先進(jìn)入到mongodb目錄的bin目錄下,Linux windos是一樣

image.png
2.集合備份和恢復(fù)
使用mongo自帶命令來遷移數(shù)據(jù),思路是先導(dǎo)出集合數(shù)據(jù)再導(dǎo)入到數(shù)據(jù)庫中
導(dǎo)出命令:mongoexport
語法: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
導(dǎo)入命令:mongoimport
語法:mongoimport -d dbname -c collectionname --file filename --headerline --type json/csv -f field
-d:數(shù)據(jù)庫名
-c:集合名稱
--file : 選擇導(dǎo)入的文件
-type : 文件類型,默認(rèn)json
-f : 字段,type為csv是必須設(shè)置此項
實戰(zhàn)命令 推薦直接使用json格式就可以啦
導(dǎo)出:mongoexport -d local -c userInfo -o D:\data\data.json --type json
導(dǎo)入:mongoimport -d config -c userInfo --file D:\data\data.json --type json
3.數(shù)據(jù)庫備份和恢復(fù)
使用mongo備份還原命令
數(shù)據(jù)庫備份語法:mongodump -h dbhost -d dbname -o dbdirectory
數(shù)據(jù)庫恢復(fù)語法:mongorestore -h dbhost -d dbname --dir dbdirectory
-h:數(shù)據(jù)庫服務(wù)器地址
-d:數(shù)據(jù)庫名
-o:備份文件路徑
--file:恢復(fù)文件的路徑
實戰(zhàn)命令
數(shù)據(jù)庫備份語法:mongodump -h 127.0.0.1 -d local -o D:\data
數(shù)據(jù)庫恢復(fù)語法:mongorestore -h 127.0.0.1 -d dump --dir D:\data\local
備注:如果原有集合或者數(shù)據(jù)庫存在則是把相同名稱原有數(shù)據(jù)覆蓋
參考:
https://jingyan.baidu.com/article/63f236287e91c40208ab3d11.html
https://www.cnblogs.com/lmh001/p/10069958.html