新聞中心

數(shù)據(jù)備份
在 MongoDB 中我們可以使用 mongodump 命令來對(duì) MongoDB 進(jìn)行數(shù)據(jù)備份,該命令可以導(dǎo)出所有數(shù)據(jù)到指定目錄中,也可以通過參數(shù)將導(dǎo)出數(shù)據(jù)轉(zhuǎn)存的服務(wù)器。其語法格式如下:
mongodump -h dbhost -d dbname -o dbdirectory
參數(shù)說明如下:
- -h:MongDB 所在服務(wù)器的地址,例如:127.0.0.1,同時(shí)也可以指定端口號(hào),例如:127.0.0.1:27017;
- -d:需要備份的數(shù)據(jù)庫實(shí)例,例如:test;
- -o:備份數(shù)據(jù)存放的位置,例如:c:\data\dump,該目錄需要提前建立,在備份完成后,系統(tǒng)會(huì)自動(dòng)在 dump 目錄下建立一個(gè) test 目錄,并在這個(gè)目錄里面存放該數(shù)據(jù)庫實(shí)例的備份數(shù)據(jù)。
【示例】假設(shè)在電腦本地(127.0.0.1)的 27017 端口運(yùn)行 MongoDB 服務(wù),然后打開命令提示符(CMD)窗口并進(jìn)入 MongoDB 安裝目錄下的 bin 目錄,最后輸入
mongodump 命令,運(yùn)行結(jié)果如下:
C:\Users\79330>D:
D:\>cd install/MongoDB/bin
D:\install\Mongodb\bin>mongodump
2021-02-25T11:22:39.468+0800 writing admin.system.version to
2021-02-25T11:22:39.516+0800 done dumping admin.system.version (1 document)
2021-02-25T11:22:39.516+0800 writing bianchengbang.course to
2021-02-25T11:22:39.517+0800 done dumping bianchengbang.course (3 documents)
mongodump 命令將連接到運(yùn)行于
127.0.0.1:27017 的 MongoDB 服務(wù)器上,并將服務(wù)器中的所有數(shù)據(jù)備份到 MongoDB 安裝目錄下的 /bin/dump/ 目錄下。
下表中列舉了可以與
mongodump 命令一起使用的參數(shù)列表:
| 語法 | 描述 | 實(shí)例 |
|---|---|---|
| mongodump --host HOST_NAME --port PORT_NUMBER | 該命令將備份所有 MongoDB 數(shù)據(jù) | mongodump --host runoob.com --port 27017 |
| mongodump --dbpath DB_PATH --out BACKUP_DIRECTORY | 該命令將僅備份指定路徑上的指定數(shù)據(jù)庫 | mongodump --dbpath /data/db/ --out /data/backup/ |
| mongodump --collection COLLECTION --db DB_NAME | 該命令將備份指定數(shù)據(jù)庫的集合 | mongodump --collection mycol --db test |
恢復(fù)數(shù)據(jù)
要想還原備份的數(shù)據(jù),您可以使用 MongoDB 的
mongorestore 命令,此命令會(huì)從備份目錄還原所有數(shù)據(jù),語法格式如下:
mongorestore
【示例】下面通過一個(gè)簡(jiǎn)單的示例演示一下
mongorestore 命令的使用:
D:\install\Mongodb\bin>mongorestore
2021-02-25T13:26:27.757+0800 using default 'dump' directory
2021-02-25T13:26:27.775+0800 preparing collections to restore from
2021-02-25T13:26:27.776+0800 reading metadata for bianchengbang.course from dump\bianchengbang\course.metadata.json
2021-02-25T13:26:27.791+0800 restoring bianchengbang.course from dump\bianchengbang\course.bson
2021-02-25T13:26:27.800+0800 restoring indexes for collection bianchengbang.course from metadata
2021-02-25T13:26:27.813+0800 finished restoring bianchengbang.course (3 documents)
2021-02-25T13:26:27.814+0800 done
當(dāng)前文章:MongoDB備份和恢復(fù)
網(wǎng)站路徑:http://m.fisionsoft.com.cn/article/dhhsioj.html


咨詢
建站咨詢
