mongodb資料備份與恢復

2021-08-27 14:49:25 字數 2139 閱讀 1627

mongodb提供了備份和恢復的功能,分別是mongdump和mongorestore兩個命令

先介紹下命令語法:

#mongodump -h dbhost -d dbname -o dbdirectory

-d:需要備份的資料庫例項,例如:test

-o:備份的資料存放位置,例如:/home,當然該目錄需要提前建立,在備份完成後,系統自動在dump目錄下建立乙個test目錄,這個目錄裡面存放該資料庫例項的備份資料。

#mongorestore -h dbhost -d dbname --directoryperdb dbdirectory

-d:需要恢復的資料庫例項,例如:test,當然這個名稱也可以和備份時候的不一樣,比如test2

--directoryperdb:備份資料所在位置,例如:/home/test,這裡為什麼要多加乙個test,而不是備份時候的dump,讀者自己檢視提示吧!

--drop:恢復的時候,先刪除當前資料,然後恢復備份的資料。就是說,恢復後,備份後新增修改的資料都會被刪除,慎用!

具體操作如下:

[root@ay1308021452325580baz ~]# mongodump -h localhost -d test -o /home/

connected to: localhost

mon sep 16 17:25:28.143 database: test to /home/test

mon sep 16 17:25:28.146 test.system.indexes to /home/test/system.indexes.bson

mon sep 16 17:25:28.147 2 objects

mon sep 16 17:25:28.147 test.test to /home/test/test.bson

mon sep 16 17:25:28.147 1 objects

mon sep 16 17:25:28.147 metadata for test.test to /home/test/test.metadata.json

mon sep 16 17:25:28.147 test.testcollection to /home/test/testcollection.bson

mon sep 16 17:25:28.148 2 objects

mon sep 16 17:25:28.148 metadata for test.testcollection to /home/test/testcollection.metadata.json

[root@ay1308021452325580baz test]# mongorestore -h localhost -d test -directoryperdb /home/test --drop

connected to: localhost

mon sep 16 17:26:40.540 /home/test/testcollection.bson

mon sep 16 17:26:40.540 going into namespace [test.testcollection]

mon sep 16 17:26:40.540 dropping

2 objects found

mon sep 16 17:26:40.540 creating index: , ns: "test.testcollection", name: "_id_" }

mon sep 16 17:26:40.541 /home/test/test.bson

mon sep 16 17:26:40.541 going into namespace [test.test]

mon sep 16 17:26:40.541 dropping

1 objects found

mon sep 16 17:26:40.543 creating index: , ns: "test.test", name: "_id_" }

mongodb資料備份與恢復

1.備份的語法 2.恢復語法 3.匯出 用於和其他平台進行互動對接,將資料匯出成指定格式檔案進行使用,比如資料分析常用的csv檔案 用於給非計算機行業的使用者檢視資料,對於他們來說csv檔案 開啟之後是電子 更方便 匯出語法 mongoexport h dbhost d dbname c colna...

mongodb資料備份與恢復

1.1.概念 mongdb中的mongoexport 工具可以將collection 匯出成json格式或者csv格式的檔案。可以通過引數指定匯出的資料項,也可以根據指定的條件匯出資料。1.2.語法 mongoexport d dbname c collectionname o fiepath ty...

MongoDB備份與恢復

任何資料庫都需要備份和恢復,這個重要性就不多說了,大家都懂的。首先說下語法 備份 mongodump hdbhost ddbname odbdirectory h mongdb所在伺服器位址,例如 127.0.0.1,當然也可以指定埠號 127.0.0.1 27017 d 需要備份的資料庫例項,例如...