溫馨提示×

溫馨提示×

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

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

怎樣利用MySQL學(xué)習(xí)MongoDB中的備份和恢復(fù)

發(fā)布時(shí)間:2021-09-29 10:37:00 來源:億速云 閱讀:123 作者:柒染 欄目:數(shù)據(jù)庫

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎樣利用MySQL學(xué)習(xí)MongoDB中的備份和恢復(fù),文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

  在數(shù)據(jù)庫表丟失或損壞的情況下,備份你的數(shù)據(jù)庫是很重要的。如果發(fā)生系統(tǒng)崩潰,你肯定想能夠?qū)⒛愕谋肀M可能丟失最少的數(shù)據(jù)恢復(fù)到崩潰發(fā)生時(shí)的狀態(tài)。

  如何利用MySQL學(xué)習(xí)MongoDB之備份和恢復(fù)

  1、MySQL備份和恢復(fù)

  MySQL備份方式大體上分為以下3種:

  直接拷貝數(shù)據(jù)庫文件

  使用mysqlhotcopy備份數(shù)據(jù)庫

  使用mysqldump備份數(shù)據(jù)庫

  (1)、直接拷貝數(shù)據(jù)庫文件

  最為直接、快速、方便,缺點(diǎn)是基本上不能實(shí)現(xiàn)增量備份。為了保證數(shù)據(jù)的一致性,需要在靠背文件前,執(zhí)行以下SQL語句:

  FLUSHTABLESWITHREADLOCK;

  也就是把內(nèi)存中的數(shù)據(jù)都刷新到磁盤中,同時(shí)鎖定數(shù)據(jù)表,以保證拷貝過程中不會有新的數(shù)據(jù)寫入。這種方法備份出來的數(shù)據(jù)恢復(fù)也很簡單,直接拷貝回原來的數(shù)據(jù)庫目錄下即可。

  但對于Innodb類型表來說,還需要備份其日志文件,即ib_logfile*文件。因?yàn)楫?dāng)Innodb表損壞時(shí),就可以依靠這些日志文件來恢復(fù)。

  (2)、使用mysqlhotcopy備份數(shù)據(jù)庫

  mysqlhotcopy是perl程序。它使用LOCKTABLES、FLUSHTABLES和cp或scp來快速備份數(shù)據(jù)庫。對于備份數(shù)據(jù)庫或單個(gè)表來說它是最快的途徑,但它只能運(yùn)行在本地服務(wù)器上,且mysqlhotcopy只能備份MyISAM表,對于Innodb表則無招可施了。

  (3)、使用mysqldump備份數(shù)據(jù)庫

  mysqldump是SQL級別的備份,它將數(shù)據(jù)表導(dǎo)成SQL腳該篇文章件,在不同的MySQL版本之間升級時(shí)相對比較合適,這也是最主流的備份方法。

  2、MongoDB備份和恢復(fù)

  MongoDB提供了兩個(gè)命令來備份(mongodump)和恢復(fù)(mongorestore)數(shù)據(jù)庫。

  (1)、mongodump備份工具

  我們先看一下此工具的幫助信息:

  [chinastor.com-root@localhostbin]#./mongodump--helpoptions:--helpproducehelpmessage-v[--verbose]bemoreverbose(includemultipletimesformoreverbositye.g.-vvvvv)-h[--host]argmongohosttoconnectto(/s1,s2forsets)--portargserverport.Canalsouse--hosthostname:port--ipv6enableIPv6support(disabledbydefault)-u[--username]argusername-p[--password]argpassword--dbpathargdirectlyaccessmongoddatabasefilesinthegivenpath,insteadofconnectingtoamongodserver-needstolockthedatadirectory,socannotbeusedifamongodiscurrentlyaccessingthesamepath--directoryperdbifdbpathspecified,eachdbisinaseparatedirectory-d[--db]argdatabasetouse-c[--collection]argcollectiontouse(somecommands)-o[--out]arg(=dump)outputdirectoryor"-"forstdout-q[--query]argjsonquery--oplogUseoplogforpoint-in-timesnapshotting--repairtrytorecoveracrasheddatabase[chinastor.com-root@localhostbin]#

  如何利用MySQL學(xué)習(xí)MongoDB之備份和恢復(fù)

  例如我們的系統(tǒng)中有一個(gè)叫做”foo”庫,下面我們將演示如何將這個(gè)庫備份出來:

  [chinastor.com-root@localhostbin]#./mongodump-dfoo-o/data/dumpconnectedto:127.0.0.1DATABASE:footo/data/dump/foofoo.system.indexesto/data/dump/foo/system.indexes.bson3objectsfoo.system.usersto/data/dump/foo/system.users.bson1objectsfoo.t2to/data/dump/foo/t2.bson1objectsfoo.t1to/data/dump/foo/t1.bson2objects[chinastor.com-root@localhostbin]#

  通過工具返回信息,我們看到的是foo中的數(shù)據(jù)已經(jīng)被備份成bson格式的文件了,接下來我們到備份的目錄下去驗(yàn)證一下:

  [chinastor.com-root@localhostdump]#ll/data/dump/foo/總計(jì)16-rw-r--r--1rootroot19304-2211:55system.indexes.bson-rw-r--r--1rootroot9104-2211:55system.users.bson-rw-r--r--1rootroot6604-2211:55t1.bson-rw-r--r--1rootroot4904-2211:55t2.bson[chinastor.com-root@localhostdump]#

  結(jié)果證明foo庫中的表已經(jīng)被成功備份出來,接下來我們將演示如何將備份恢復(fù)回去。

  (2)、mongorestore恢復(fù)工具

  我們先看一下此工具的幫助信息:

  [chinastor.com-root@localhostbin]#./mongorestore--helpusage:./mongorestore[options][directoryorfilenametorestorefrom]options:--helpproducehelpmessage-v[--verbose]bemoreverbose(includemultipletimesformoreverbositye.g.-vvvvv)-h[--host]argmongohosttoconnectto(/s1,s2forsets)--portargserverport.Canalsouse--hosthostname:port--ipv6enableIPv6support(disabledbydefault)-u[--username]argusername-p[--password]argpassword--dbpathargdirectlyaccessmongoddatabasefilesinthegivenpath,insteadofconnectingtoamongodserver-needstolockthedatadirectory,socannotbeusedifamongodiscurrentlyaccessingthesamepath--directoryperdbifdbpathspecified,eachdbisinaseparatedirectory-d[--db]argdatabasetouse-c[--collection]argcollectiontouse(somecommands)--objcheckvalidateobjectbeforeinserting--filterargfiltertoapplybeforeinserting--dropdropeachcollectionbeforeimport--oplogReplayreplayoplogforpoint-in-timerestore[chinastor.com-root@localhostbin]#

  例如我們先將”foo”庫刪除了:

  [chinastor.com-root@localhostbin]#./mongoMongoDBshellversion:1.8.1connectingto:test>usefooswitchedtodbfoo>db.dropDatabase();{"dropped":"foo","ok":1}>showdbsadmin0.0625GBlocal(empty)test0.0625GB>

  然后下面我們將演示如何恢復(fù)這個(gè)庫:

  [chinastor.com-root@localhostbin]#./mongorestore--directoryperdb/data/dumpconnectedto:127.0.0.1SunApr2212:01:27/data/dump/foo/t1.bsonSunApr2212:01:27goingintonamespace[foo.t1]SunApr2212:01:272objectsfoundSunApr2212:01:27/data/dump/foo/t2.bsonSunApr2212:01:27goingintonamespace[foo.t2]SunApr2212:01:271objectsfoundSunApr2212:01:27/data/dump/foo/system.users.bsonSunApr2212:01:27goingintonamespace[foo.system.users]SunApr2212:01:271objectsfoundSunApr2212:01:27/data/dump/foo/system.indexes.bsonSunApr2212:01:27goingintonamespace[foo.system.indexes]SunApr2212:01:27{name:"_id_",ns:"foo.system.users",key:{_id:1},v:0}SunApr2212:01:27{name:"_id_",ns:"foo.t2",key:{_id:1},v:0}SunApr2212:01:27{name:"_id_",ns:"foo.t1",key:{_id:1},v:0}SunApr2212:01:273objectsfound[chinastor.com-root@localhostbin]#

  通過工具返回信息,我們看到的是foo中的數(shù)據(jù)已經(jīng)被恢復(fù)回來了,接下來我們到庫里去驗(yàn)證一下:

  [chinastor.com-root@localhostbin]#./mongoMongoDBshellversion:1.8.1connectingto:test>usefooswitchedtodbfoo>showcollections;system.indexessystem.userst1t2>

  結(jié)果證明foo庫表已經(jīng)被成功恢復(fù)回來了。

上述就是小編為大家分享的怎樣利用MySQL學(xué)習(xí)MongoDB中的備份和恢復(fù)了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

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

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

AI