mongoDB的導出、導入、運行時的備份

1、單個集合的導出,我用的都是帶驗證的開啓方式   

C:\Users\Administrator>mongoexport -d text1 -c poot1 -o D:/aaaa.json -u NO1 -p 1
23 --authenticationDatabase admin
    -d 是數據庫名

    -c是集合名

    -o是導入的存放地址

    -u是用戶名,我這裏用的是超級管理員

    -p是密碼,

   --authenticationDatabase admin   這是是必須的,否則會報如下錯誤:

    2016-07-09T17:35:49.237+0800    error connecting to db server: server returned e
rror on SASL authentication step: Authentication failed.


2、導入單個的集合,我用的都是帶驗證的開啓方式  

C:\Users\Administrator>mongoimport --db text1 --collection poot1 -u NO1 -p 123 -
-file D:/aaaa.json  --authenticationDatabase admin
2016-07-09T18:00:28.769+0800    connected to: localhost
2016-07-09T18:00:29.095+0800    imported 4 documents

上面的2個導入、導出有個很大的問題,就是在操作的時候,一切的增刪改查都不能執行了,相當於是中斷掉了。這是我們不可能允許的。

3、4、5、6講的就是運行時的備份

3、整個數據庫的導出,我用的都是帶驗證的開啓方式  

C:\Users\Administrator>mongodump -d text1 -o D:/aaaa.bak -u NO1 -p 123 --authent
icationDatabase admin

      和上觀的解釋一樣一樣的。

4、導入整個的數據庫,我用的都是帶驗證的開啓方式   ----------------注意,得把要導入的庫找到才行 --dorp上留意

C:\Users\Administrator>mongorestore -u NO1 -p 123 -d text1 --drop D:\aaaa\text1
 --authenticationDatabase admin
2016-07-09T18:16:26.423+0800    building a list of collections to restore from D
:\aaaa\text1 dir
2016-07-09T18:16:26.427+0800    reading metadata for text1.poot1 from D:\aaaa\te
xt1\poot1.metadata.json
2016-07-09T18:16:26.428+0800    reading metadata for text1.aaa from D:\aaaa\text
1\aaa.metadata.json
2016-07-09T18:16:26.765+0800    restoring text1.poot1 from D:\aaaa\text1\poot1.b
son
2016-07-09T18:16:27.095+0800    restoring text1.aaa from D:\aaaa\text1\aaa.bson
2016-07-09T18:16:27.095+0800    restoring indexes for collection text1.poot1 fro
m metadata
2016-07-09T18:16:27.096+0800    restoring indexes for collection text1.aaa from
metadata
2016-07-09T18:16:27.097+0800    finished restoring text1.poot1 (4 documents)
2016-07-09T18:16:27.097+0800    finished restoring text1.aaa (1 document)
2016-07-09T18:16:27.098+0800    done

C:\Users\Administrator>

5、遠程數據導出。----------------沒有測試過

<span style="color:#FF0000;"><span style="color:#000000;">C:\Users\Administrator>mongodump --host 134.22.33.55 --port 27017 -d text1 -o D:/aaaa.bak -u NO1 -p 123 --authent
icationDatabase admin</span></span>

6、遠程導入整個庫,我用的都是帶驗證的開啓方式   ----------------沒有測試過

C:\Users\Administrator>mongorestore --host 134.22.33.55 --port 27017 -u NO1 -p 123 -d text1 --drop D:\aaaa\text1
 --authenticationDatabase admin

但運行時的備份也有問題,那就是緩衝區的數據不會被備份起來,也就是說會丟數據。下面就需要用到鎖的概念,備份之前先把緩衝區的數據存到數據庫再備份,備份完了再解鎖。這樣做出來就完美了。

7、加鎖 -------------------------記住要在admin裏做操作

> use admin
switched to db admin
> db.runCommand({fsync:1,lock:1})
{
        "info" : "now locked against writes, use db.fsyncUnlock() to unlock",
        "seeAlso" : "http://dochub.mongodb.org/core/fsynccommand",
        "ok" : 1
}
>

8、解鎖

<span style="color:#000000;">> db.currentOp()</span>

9、數據庫修復,在有數據不全,髒數據的情況下使用,及耗性能。不建意在生產環境使用

> db.repairDatabase()


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章