Linux系統中備份和恢復MongoDB數據的教程


版本:mongodb3.2.6mongodb

備份格式:數據庫

/data/mongodb/bin/mongodump -h IP --port 端口號 -u 用戶 -p 密碼-d 數據庫名-o 存儲路徑 安全

恢復格式:服務器

/mnt/mongodb/bin/mongorestore -h IP --port 端口號 -u 用戶名-p密碼  -d 數據庫名 備份的文件夾名/*app

注意,若是mongodb開啓了認證登陸,那麼須要加參數--authenticationDatabase=admin,ide

由於筆者使用的mongodb開啓了認證登陸,所以在備份和恢復中都使用了該參數。spa

查看過相關資料,說是開啓了認證(auth=true)會致使數據庫變慢,筆者暫時未遇到過,推測應該是在必定數據量的狀況纔會出現。畢竟要過濾下嘛。若是服務器是在公網,建議仍是要開啓認證的,若是實在內網,不用認證也行,可是要保證服務器安全哦,好比指定IP纔可鏈接mongodb數據庫.net

若是是在本地導入導出,端口也沒有更改的狀況下,-h和--port參數就不用加了。rest


還有一個,使用導入導出的用戶名須要有數據庫管理權限哦。server

解釋一下用到的命令

1.  -h:MongoDB所在服務器地址

2.  -d:須要恢復的數據庫實例,例如:test,固然這個名稱也能夠和備份時候的不同,好比test2

3.  -o:備份的數據存放位置,例如:/data/dump,固然該目錄須要提早創建,在備份完成後,系統自動在dump目錄下創建一個test目錄,這個目錄裏面存放該數據庫實例的備份數據。

4.  --directoryperdb:備份數據所在位置,例如:/data/dump/test,這裏爲何要多加一個test,而不是備份時候的dump,讀者本身查看提示吧!

5.  --drop:恢復的時候,先刪除當前數據,而後恢復備份的數據。就是說,恢復後,備份後添加修改的數據都會被刪除,慎用哦!

原始解釋:

?

-v [ --verbose ]           be more verbose (include multiple  times

                   for more verbosity e.g. -vvvvv)

--version               print the program's version and  exit

-h [ --host ] arg           mongo host to connect to ( <set

                   name>/s1,s2 for sets)

--port arg              server port. Can also use --host

                   hostname:port

--ipv6                enable IPv6 support (disabled by

                   default)

-u [ --username ] arg         username

-p [ --password ] arg         password

--authenticationDatabase arg     user source (defaults to dbname)

--authenticationMechanism arg  (=MONGODB-CR)

                   authentication mechanism

--dbpath arg             directly access mongod database  files

                   in the given path, instead of

                   connecting to a mongod server -  needs

                   to lock the data directory, so  cannot

                   be used if a mongod is currently

                   accessing the same path

--directoryperdb           each db is in a separate directly

                   (relevant only if dbpath specified)

--journal               enable journaling (relevant only  if

                   dbpath specified)

-d [ --db ] arg            database to use

-c [ --collection ] arg        collection to use (some commands)

--objcheck              validate object before inserting

                   (default)

--noobjcheck             don't validate object before  inserting

--filter arg             filter to apply before inserting

--drop                drop each collection before import

--oplogReplay             replay oplog for point-in-time  restore

--oplogLimit arg           include oplog entries before the

                   provided Timestamp  (seconds[:ordinal])

                   during the oplog replay; the  ordinal

                   value is optional

--keepIndexVersion          don't upgrade indexes to newest  version

--noOptionsRestore          don't restore collection options

--noIndexRestore           don't restore indexes

--w arg (=0)             minimum number of replicas per  write

實戰操做:

mongodb數據庫的備份:

/data/mongodb/bin/mongodump -u root -p 123456 -d test -o/data/mongodb_$(date +%F) --authenticationDatabase=admin

mongodb數據庫的恢復,筆者的恢復文件夾路徑爲/mnt/mongodb20160905/:

參考以下代碼:

[root@host1 bin]# /mnt/mongodb/bin/mongorestore -uroot –p123456 -d test /mnt/mongodb20160905/ --authenticationDatabase=admin


若是導入有報錯:能夠在文件夾後面加*試試:

[root@host1 bin]# /mnt/mongodb/bin/mongorestore -uroot –p123456 -d test /mnt/mongodb20160905/* --authenticationDatabase=admin

相關文章
相關標籤/搜索