溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Linux系統(tǒng)中怎么備份和恢復(fù)MongoDB數(shù)據(jù)

發(fā)布時間:2021-07-16 16:33:53 來源:億速云 閱讀:373 作者:Leah 欄目:數(shù)據(jù)庫

今天就跟大家聊聊有關(guān)Linux系統(tǒng)中怎么備份和恢復(fù)MongoDB數(shù)據(jù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

版本:mongodb3.2.6

備份格式:

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

恢復(fù)格式:

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

注意,如果mongodb開啟了認(rèn)證登錄,那么需要加參數(shù)--authenticationDatabase=admin,

因為筆者使用的mongodb開啟了認(rèn)證登錄,因此在備份和恢復(fù)中都使用了該參數(shù)。

查看過相關(guān)資料,說是開啟了認(rèn)證(auth=true)會導(dǎo)致數(shù)據(jù)庫變慢,筆者暫時未遇到過,推測應(yīng)該是在一定數(shù)據(jù)量的情況才會出現(xiàn)。畢竟要過濾下嘛。如果服務(wù)器是在公網(wǎng),建議還是要開啟認(rèn)證的,如果實在內(nèi)網(wǎng),不用認(rèn)證也行,但是要保證服務(wù)器安全哦,比如指定IP才可連接mongodb數(shù)據(jù)庫

如果是在本地導(dǎo)入導(dǎo)出,端口也沒有更改的情況下,-h和--port參數(shù)就不用加了。

還有一個,使用導(dǎo)入導(dǎo)出的用戶名需要有數(shù)據(jù)庫管理權(quán)限哦。

解釋一下用到的命令

1.  -h:MongoDB所在服務(wù)器地址

2.  -d:需要恢復(fù)的數(shù)據(jù)庫實例,例如:test,當(dāng)然這個名稱也可以和備份時候的不一樣,比如test2

3.  -o:備份的數(shù)據(jù)存放位置,例如:/data/dump,當(dāng)然該目錄需要提前建立,在備份完成后,系統(tǒng)自動在dump目錄下建立一個test目錄,這個目錄里面存放該數(shù)據(jù)庫實例的備份數(shù)據(jù)。

4.  --directoryperdb:備份數(shù)據(jù)所在位置,例如:/data/dump/test,這里為什么要多加一個test,而不是備份時候的dump,讀者自己查看提示吧!

5.  --drop:恢復(fù)的時候,先刪除當(dāng)前數(shù)據(jù),然后恢復(fù)備份的數(shù)據(jù)。就是說,恢復(fù)后,備份后添加修改的數(shù)據(jù)都會被刪除,慎用哦!

原始解釋:

?

-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

實戰(zhàn)操作:

mongodb數(shù)據(jù)庫的備份:

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

mongodb數(shù)據(jù)庫的恢復(fù),筆者的恢復(fù)文件夾路徑為/mnt/mongodb20160905/:

參考如下代碼:

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

如果導(dǎo)入有報錯:可以在文件夾后面加*試試:

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

看完上述內(nèi)容,你們對Linux系統(tǒng)中怎么備份和恢復(fù)MongoDB數(shù)據(jù)有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI