mongodb 數(shù)據(jù)庫備份和導(dǎo)入導(dǎo)出

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

使用 mongodump 命令備份數(shù)據(jù)庫

mongodump -h 主機(jī)名:端口 -d 數(shù)據(jù)庫名稱 -o 備份路徑

例子:
/usr/local/mongodb/bin/mongodump -d sns -o /data/sns-all-20170823

使用 mongorestore 命令還原數(shù)據(jù)庫

mongorestore -h 主機(jī)名:端口 -d 數(shù)據(jù)庫名稱 --dir 數(shù)據(jù)庫備份文件所在目錄

例子:
/usr/local/mongodb/bin/mongorestore -d sns --dir /data/sns-all-20170823/sns


mongodb 數(shù)據(jù)導(dǎo)入導(dǎo)出

使用 mongoexport 導(dǎo)出

mongoexport -d dbname -c collectionname -o file --type json/csv -f field

參數(shù)說明:
-d :數(shù)據(jù)庫名
-c :collection名
-o :輸出的文件名,備份名稱
--type : 輸出的格式,默認(rèn)為json
-f :輸出的字段,如果-type為csv,則需要加上-f "字段名"

示例:

/usr/local/mongodb/bin/mongoexport -d mongotest -c users -o /home/python/Desktop/mongoDB/users.json --type json -f "id,user_id,user_name,age,status"

數(shù)據(jù)導(dǎo)入:mongoimport

mongoimport -d dbname -c collectionname --file filename --headerline --type json/csv -f field

參數(shù)說明:
-d :數(shù)據(jù)庫名
-c :collection名
--type :導(dǎo)入的格式默認(rèn)json,json或者csv
-f :導(dǎo)入的字段名
--headerline :如果導(dǎo)入的格式是csv,則可以使用第一行的標(biāo)題作為導(dǎo)入的字段
--file :要導(dǎo)入的文件

示例:

/usr/local/mongodb/bin/mongoimport -d mongotest -c users --file /home/mongodump/articles.json --type json

例子:

常用命令格

mongodump -h IP --port 端口 -u 用戶名 -p 密碼 -d 數(shù)據(jù)庫 -o 文件存在路徑

如果沒有用戶,可以去掉-u和-p。
 如果導(dǎo)出本機(jī)的數(shù)據(jù)庫,可以去掉-h。
 如果是默認(rèn)端口,可以去掉--port。
 如果想導(dǎo)出所有數(shù)據(jù)庫,可以去掉-d。

導(dǎo)出全部數(shù)據(jù)庫

mongodump -h 127.0.0.1 -o /data/mongodb/

導(dǎo)出指定數(shù)據(jù)庫

mongodump -h 127.0.0.1 -d test -o /data/mongodb

mogodb 過濾條件導(dǎo)出

./mongoexport -d sns_pre -c snsPosts -q '{tid:123123}'' -o sns.json

導(dǎo)入

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

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