這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)怎樣利用MySQL學習MongoDB中的備份和恢復,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
成都創(chuàng)新互聯(lián)公司成立十余年來,這條路我們正越走越好,積累了技術(shù)與客戶資源,形成了良好的口碑。為客戶提供成都做網(wǎng)站、網(wǎng)站設(shè)計、網(wǎng)站策劃、網(wǎng)頁設(shè)計、域名注冊、網(wǎng)絡(luò)營銷、VI設(shè)計、網(wǎng)站改版、漏洞修補等服務(wù)。網(wǎng)站是否美觀、功能強大、用戶體驗好、性價比高、打開快等等,這些對于網(wǎng)站建設(shè)都非常重要,成都創(chuàng)新互聯(lián)公司通過對建站技術(shù)性的掌握、對創(chuàng)意設(shè)計的研究為客戶提供一站式互聯(lián)網(wǎng)解決方案,攜手廣大客戶,共同發(fā)展進步。
在數(shù)據(jù)庫表丟失或損壞的情況下,備份你的數(shù)據(jù)庫是很重要的。如果發(fā)生系統(tǒng)崩潰,你肯定想能夠?qū)⒛愕谋肀M可能丟失最少的數(shù)據(jù)恢復到崩潰發(fā)生時的狀態(tài)。
如何利用MySQL學習MongoDB之備份和恢復
1、MySQL備份和恢復
MySQL備份方式大體上分為以下3種:
直接拷貝數(shù)據(jù)庫文件
使用mysqlhotcopy備份數(shù)據(jù)庫
使用mysqldump備份數(shù)據(jù)庫
(1)、直接拷貝數(shù)據(jù)庫文件
最為直接、快速、方便,缺點是基本上不能實現(xiàn)增量備份。為了保證數(shù)據(jù)的一致性,需要在靠背文件前,執(zhí)行以下SQL語句:
FLUSHTABLESWITHREADLOCK;
也就是把內(nèi)存中的數(shù)據(jù)都刷新到磁盤中,同時鎖定數(shù)據(jù)表,以保證拷貝過程中不會有新的數(shù)據(jù)寫入。這種方法備份出來的數(shù)據(jù)恢復也很簡單,直接拷貝回原來的數(shù)據(jù)庫目錄下即可。
但對于Innodb類型表來說,還需要備份其日志文件,即ib_logfile*文件。因為當Innodb表損壞時,就可以依靠這些日志文件來恢復。
(2)、使用mysqlhotcopy備份數(shù)據(jù)庫
mysqlhotcopy是perl程序。它使用LOCKTABLES、FLUSHTABLES和cp或scp來快速備份數(shù)據(jù)庫。對于備份數(shù)據(jù)庫或單個表來說它是最快的途徑,但它只能運行在本地服務(wù)器上,且mysqlhotcopy只能備份MyISAM表,對于Innodb表則無招可施了。
(3)、使用mysqldump備份數(shù)據(jù)庫
mysqldump是SQL級別的備份,它將數(shù)據(jù)表導成SQL腳該篇文章件,在不同的MySQL版本之間升級時相對比較合適,這也是最主流的備份方法。
2、MongoDB備份和恢復
MongoDB提供了兩個命令來備份(mongodump)和恢復(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學習MongoDB之備份和恢復
例如我們的系統(tǒng)中有一個叫做”foo”庫,下面我們將演示如何將這個庫備份出來:
[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格式的文件了,接下來我們到備份的目錄下去驗證一下:
[chinastor.com-root@localhostdump]#ll/data/dump/foo/總計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)被成功備份出來,接下來我們將演示如何將備份恢復回去。
(2)、mongorestore恢復工具
我們先看一下此工具的幫助信息:
[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>
然后下面我們將演示如何恢復這個庫:
[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)被恢復回來了,接下來我們到庫里去驗證一下:
[chinastor.com-root@localhostbin]#./mongoMongoDBshellversion:1.8.1connectingto:test>usefooswitchedtodbfoo>showcollections;system.indexessystem.userst1t2>
結(jié)果證明foo庫表已經(jīng)被成功恢復回來了。
上述就是小編為大家分享的怎樣利用MySQL學習MongoDB中的備份和恢復了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站標題:怎樣利用MySQL學習MongoDB中的備份和恢復
分享路徑:http://aaarwkj.com/article48/gghshp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護、定制網(wǎng)站、商城網(wǎng)站、網(wǎng)站營銷、網(wǎng)站排名、自適應網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)