1. 程式人生 > 實用技巧 >MongoDB備份(mongoexport)與恢復(mongoimport)

MongoDB備份(mongoexport)與恢復(mongoimport)

1.備份恢復工具介紹:

  1. mongoexport/mongoimport
  2. mongodump/mongorestore(本文未涉及)

2.備份工具區別在哪裡?

2.1

  • mongoexport/mongoimport 匯入/匯出的是JSON格式或者CSV格式
  • mongodump/mongorestore 匯入/匯出的是BSON格式

2.2

JSON可讀性強但體積較大,BSON則是二進位制檔案,體積小但對人類幾乎沒有可讀性。

2.3

  • 在一些mongodb版本之間,BSON格式可能會隨版本不同而有所不同,所以不同版本之間用mongodump/mongorestore可能不會成功,具體要看版本之間的相容性。
  • 當無法使用BSON進行跨版本的資料遷移的時候,使用JSON格式即mongoexport/mongoimport是一個可選項。
  • 跨版本的mongodump/mongorestore個人並不推薦,實在要做請先檢查文件看兩個版本是否相容(大部分時候是的)

2.4

JSON雖然具有較好的跨版本通用性,但其只保留了資料部分,不保留索引,賬戶等其他基礎資訊。使用時應該注意。

2.5

  • mongoexport不支援普通匯出單個db的所有的collection
  • mongodump支援普通匯出單個db的所有的collection

3.應用場景總結:

mongoexport/mongoimport

1、異構平臺遷移 mysql <---> mongodb
2、同平臺,跨大版本:mongodb2.x ---> mongodb3.x

mongodump/mongorestore

日常備份恢復時使用.

4.版本介紹

5.匯出工具mongoexport

  • mongoexport工具可以把一個collection匯出成JSON格式或CSV格式的檔案。
  • 可以通過引數指定匯出的資料項,也可以根據指定的條件匯出資料。

5.1 mongoexport關鍵引數如下所示:

mongoexport --help檢視幫助命令

  • -h,--host :代表遠端連線的資料庫地址,預設連線本地Mongo資料庫;
  • --port:代表遠端連線的資料庫的埠,預設連線的遠端埠27017;
  • -u,--username:代表連線遠端資料庫的賬號,如果設定資料庫的認證,需要指定使用者賬號;
  • -p,--password:代表連線資料庫的賬號對應的密碼;
  • -d,--db:代表連線的資料庫;
  • -c,--collection:代表連線資料庫中的集合;
  • -f, --fields:代表集合中的欄位,可以根據設定選擇匯出的欄位;
  • --type:代表匯出輸出的檔案型別,包括csv和json檔案;
  • -o, --out:代表匯出的檔名;
  • -q, --query:代表查詢條件;
  • --skip:跳過指定數量的資料;
  • --limit:讀取指定數量的資料記錄;
  • --sort:對資料進行排序,可以通過引數指定排序的欄位,並使用 1 和 -1 來指定排序的方式,其中 1 為升序排列,而-1是用於降序排列,如sort({KEY:1})。
  • --authenticationDatabase:指定使用者鑑定庫

5.2 備份示例

單表備份至json格式
mongoexport -u -p --port 28019 --authenticationDatabase admin -d test -c customer -o /tmp/customer.json

[root@zhouweibing tmp]# head customer.json 
{"_id":{"$oid":"5ff55c6ac70afd201b763036"},"name":"user0"}
{"_id":{"$oid":"5ff55c6ac70afd201b763037"},"name":"user1"}

注:備份檔案的名字和路徑可以自定義,預設匯出了JSON格式的資料。

單表備份至csv格式
mongoexport -u -p --port 28019 --authenticationDatabase admin -d replset -c customer --type=csv -f _id,name -o /tmp/customer.csv

[root@zhouweibing tmp]# head customer.csv 
_id,name
ObjectId(5ff55c6ac70afd201b763036),user0
ObjectId(5ff55c6ac70afd201b763037),user1

6.匯入工具mongoimport

  • mongoimport工具可以把一個特定格式檔案中的內容匯入到指定的collection中。
  • 該工具可以匯入JSON格式資料,也可以匯入CSV格式資料。

6.1 mongoimport關鍵引數如下所示:

mongoimport --help檢視幫助命令

  • h,--host :代表遠端連線的資料庫地址,預設連線本地Mongo資料庫;
  • --port:代表遠端連線的資料庫的埠,預設連線的遠端埠27017;
  • -u,--username:代表連線遠端資料庫的賬號,如果設定資料庫的認證,需要指定使用者賬號;
    -p,--password:代表連線資料庫的賬號對應的密碼;
  • -d,--db:代表連線的資料庫;
  • -c,--collection:代表連線資料庫中的集合;
  • -f, --fields:代表匯入集合中的欄位;
  • --type:代表匯入的檔案型別,包括csv和json,tsv檔案,預設json格式;
  • --file:匯入的檔名稱;
  • --headerline:匯入csv檔案時,指明第一行是列名,不需要匯入;
  • --authenticationDatabase:指定使用者鑑定庫

6.2 恢復示例

恢復json格式表資料到json
mongoimport -u -p --port 28019 --authenticationDatabase admin -d test -c json /tmp/customer.json
恢復csv格式的檔案到csv

(1)csv格式的檔案頭行,有列名字

mongoimport -u -p --port 28019 --authenticationDatabase admin -d test -c csv --type=csv --headerline  /tmp/customer.csv

(2)csv格式的檔案頭行,沒有列名字

mongoimport -u -p --port 28019 --authenticationDatabase admin -d test -c csv1 --type=csv -f _id,name  /tmp/customer1.csv

注意:
--headerline和-f不能同時使用

7.異構平臺遷移案例

  • mysql ---> mongodb
  • 將MySQL test庫下data表匯出成csv格式,再匯入到mongodb

7.1 開啟mysql開啟安全路徑

vim /etc/my.cnf   --->新增以下配置
secure-file-priv=/tmp

重啟MySQL使配置生效

7.2 匯出mysql的data表資料

select * from data limit 1000 into outfile "/tmp/data.csv" fields terminated by ',';

7.3 新增列名資訊到data.csv

desc test.data;     #檢視列名資訊

vim /tmp/data.csv   #在第一行新增列名資訊

d,com_pid,com_name,com_image,com_craw_url_leyu,add_time,status

7.4 在mongodb中匯入data.csv

mongoimport -u -p --port 28019  --authenticationDatabase admin -d test -c data --headerline --type=csv /tmp/data.csv

7.5 將MySQL world庫下多張表匯出

select concat("select * from ",table_schema,".",table_name ," into outfile '/tmp/",table_schema,"_",table_name,".csv' fields terminated by ',';") from information_schema.tables where table_schema ='world';

7.6 mysql匯出csv格式轉換:

select * from test_info   
into outfile '/tmp/test.csv'   
fields terminated by ','    ------欄位間以,號分隔
optionally enclosed by '"'   ------欄位用"號括起
escaped by '"'           ------欄位中使用的轉義符為"
lines terminated by '\r\n';  ------行以\r\n結束

因為有悔,所以披星戴月;因為有夢,所以奮不顧身! 個人部落格首發:easydb.net 微信公眾號:easydb 關注我,不走丟!