mongo備份恢復(fù), 異構(gòu)平臺(tái)遷移案例

1. 備份恢復(fù)工具:

(1)**   mongoexport/mongoimport
(2)***** mongodump/mongorestore

2. 備份工具區(qū)別

  1. 導(dǎo)出格式不同

    mongoexport/mongoimport  導(dǎo)入/導(dǎo)出的是JSON格式或者CSV格式
    mongodump/mongorestore  導(dǎo)入/導(dǎo)出的是BSON格式
    
  2. JSON可讀性強(qiáng)但體積較大,BSON則是二進(jìn)制文件,體積小但對(duì)人類幾乎沒有可讀性。

  3. 在一些mongodb版本之間,BSON格式可能會(huì)隨版本不同而有所不同,所以不同版本之間用mongodump/mongorestore可能不會(huì)成功,

    具體要看版本之間的兼容性。當(dāng)無法使用BSON進(jìn)行跨版本的數(shù)據(jù)遷移的時(shí)候,
    使用JSON格式即mongoexport/mongoimport是一個(gè)可選項(xiàng)。
    跨版本的mongodump/mongorestore個(gè)人并不推薦,
    實(shí)在要做請(qǐng)先檢查文檔看兩個(gè)版本是否兼容(大部分時(shí)候是的)。
    
  4. JSON雖然具有較好的跨版本通用性,但其只保留了數(shù)據(jù)部分,不保留索引,賬戶等其他基礎(chǔ)信息。使用時(shí)應(yīng)該注意。

mongoexport/mongoimport:json csv
1、異構(gòu)平臺(tái)遷移 mysql <---> mongodb
2、同平臺(tái),跨大版本:mongodb 2 ----> mongodb 3

3. 導(dǎo)出工具mongoexport

Mongodb中的mongoexport工具可以把一個(gè)collection導(dǎo)出成JSON格式或CSV格式的文件。
可以通過參數(shù)指定導(dǎo)出的數(shù)據(jù)項(xiàng),也可以根據(jù)指定的條件導(dǎo)出數(shù)據(jù)。

(1)版本差異較大
(2)異構(gòu)平臺(tái)數(shù)據(jù)遷移

4. mongoexport具體用法

  1. 參數(shù)介紹

    mongoexport --help  
    參數(shù)說明:
    -h:指明數(shù)據(jù)庫(kù)宿主機(jī)的IP
    
    -u:指明數(shù)據(jù)庫(kù)的用戶名
    
    -p:指明數(shù)據(jù)庫(kù)的密碼
    
    -d:指明數(shù)據(jù)庫(kù)的名字
    
    -c:指明collection的名字
    
    -f:指明要導(dǎo)出那些列
    
    -o:指明到要導(dǎo)出的文件名
    
    -q:指明導(dǎo)出數(shù)據(jù)的過濾條件
    
    --authenticationDatabase admin  :認(rèn)證庫(kù)
    
  2. 單表備份至json格式

    mongoexport -uroot -proot123 --port 27017 --authenticationDatabase admin -d oldguo -c log -o /mongodb/log.json

    注:備份文件的名字可以自定義,默認(rèn)導(dǎo)出了JSON格式的數(shù)據(jù)。

  3. 單表備份至csv格式

    如果我們需要導(dǎo)出CSV格式的數(shù)據(jù),則需要使用----type=csv參數(shù):

    mongoexport -uroot -proot123 --port 27017 --authenticationDatabase admin -d oldguo -c log --type=csv -f uid,name,age,date -o /mongodb/log.csv

5. 導(dǎo)入工具mongoimport

Mongodb中的mongoimport工具可以把一個(gè)特定格式文件中的內(nèi)容導(dǎo)入到指定的collection中。該工具可以導(dǎo)入JSON格式數(shù)據(jù),也可以導(dǎo)入CSV格式數(shù)據(jù)。具體使用如下所示:

  1. 參數(shù)說明

    $ mongoimport --help
    參數(shù)說明:
    -h:指明數(shù)據(jù)庫(kù)宿主機(jī)的IP
    
    -u:指明數(shù)據(jù)庫(kù)的用戶名
    
    -p:指明數(shù)據(jù)庫(kù)的密碼
    
    -d:指明數(shù)據(jù)庫(kù)的名字
    
    -c:指明collection的名字
    
    -f:指明要導(dǎo)入那些列
    
    -j, --numInsertionWorkers=<number>  number of insert operations to run concurrently                                                  (defaults to 1)
    //并行
    
    
  2. 數(shù)據(jù)恢復(fù):

    恢復(fù)json格式表數(shù)據(jù)到log

    mongoimport -uroot -proot123 --port 27017 --authenticationDatabase admin -d oldguo -c log1 /mongodb/log.json

    恢復(fù)csv格式的文件到log2

    上面演示的是導(dǎo)入JSON格式的文件中的內(nèi)容,如果要導(dǎo)入CSV格式文件中的內(nèi)容,則需要通過--type參數(shù)指定導(dǎo)入格式,具體如下所示:

    1)csv格式的文件頭行,有列名字

    mongoimport -uroot -proot123 --port 27017 --authenticationDatabase admin -d oldguo -c log2 --type=csv --headerline --file /mongodb/log.csv

    2)csv格式的文件頭行,沒有列名字

    mongoimport -uroot -proot123 --port 27017 --authenticationDatabase admin -d oldguo -c log3 --type=csv -f id,name,age,date --file /mongodb/log1.csv

    --headerline:指明第一行是列名,不需要導(dǎo)入。

    補(bǔ):導(dǎo)入CSV文件P_T_HISTXN_20160616.csv(csv文件中沒有列明,fields指定列明)
    $ tail -5 P_T_HISTXN_20160616.csv

6. 異構(gòu)平臺(tái)遷移案例 mysql --> mongodb

  • world數(shù)據(jù)庫(kù)下city表進(jìn)行導(dǎo)出,導(dǎo)入到mongodb
  1. mysql開啟安全路徑

    vim /etc/my.cnf   --->添加以下配置
    secure-file-priv=/tmp
    
    --重啟數(shù)據(jù)庫(kù)生效
    /etc/init.d/mysqld restart
    
  2. 導(dǎo)出mysql的city表數(shù)據(jù)

    source /root/world.sql
    
    select * from world.city into outfile '/tmp/city1.csv' fields terminated by ',';
    
  3. 處理備份文件

    vim /tmp/city.csv   ----> 添加第一行列名信息
    ID,Name,CountryCode,District,Population
    
  4. 在mongodb中導(dǎo)入備份

    mongoimport -uroot -proot123 --port 27017 --authenticationDatabase admin -d world  -c city --type=csv -f ID,Name,CountryCode,District,Population --file  /tmp/city1.csv
    
    use world
    db.city.find({CountryCode:"CHN"});
    
  • world共100張表,全部遷移到mongodb

    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';

    導(dǎo)入:
    ? 提示,使用infomation_schema.columns + information_schema.tables

  1. mysql導(dǎo)入csv:

    load data infile '/tmp/test.csv'   
    
    into table test_info    
    
    fields terminated by ','  
    
    optionally enclosed by '"' 
    
    escaped by '"'   
    
    lines terminated by '\r\n'; 
    
?著作權(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),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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