Linux系統中備份和恢複MongoDB資料的教程

來源:互聯網
上載者:User

標籤:伺服器安全   使用者名稱   資料庫管理   檔案夾   mongodb3.2.6   備份   


版本:mongodb3.2.6

備份格式:

/data/mongodb/bin/mongodump -h IP --port 連接埠號碼 -u 使用者 -p 密碼-d 資料庫名-o 儲存路徑

恢複格式:

/mnt/mongodb/bin/mongorestore -h IP --port 連接埠號碼 -u 使用者名稱-p密碼  -d 資料庫名 備份的檔案夾名/*

注意,如果mongodb開啟了認證登入,那麼需要加參數--authenticationDatabase=admin,

因為筆者使用的mongodb開啟了認證登入,因此在備份和恢複中都使用了該參數。

查看過相關資料,說是開啟了認證(auth=true)會導致資料庫變慢,筆者暫時未遇到過,推測應該是在一定資料量的情況才會出現。畢竟要過濾下嘛。如果伺服器是在公網,建議還是要開啟認證的,如果實在內網,不用認證也行,但是要保證伺服器安全哦,比如指定IP才可串連mongodb資料庫

如果是在本地匯入匯出,連接埠也沒有更改的情況下,-h和--port參數就不用加了。


還有一個,使用匯入匯出的使用者名稱需要有資料庫系統管理權限哦。

解釋一下用到的命令

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/:

參考如下代碼:

[[email protected] bin]# /mnt/mongodb/bin/mongorestore -uroot –p123456 -d test /mnt/mongodb20160905/* --authenticationDatabase=admin

 

注意恢複資料庫是檔案夾後面跟著的“/*”,一定要加,反正我的沒加就報錯,不能匯入了。


Linux系統中備份和恢複MongoDB資料的教程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.