日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
教你如何利用MySQL學(xué)習(xí)MongoDB之備份和恢復(fù)

在上文中,我們了解了教你如何利用MySQL學(xué)習(xí)MongoDB之授權(quán)和權(quán)限,本文中我們繼續(xù)我們的學(xué)習(xí)之旅,學(xué)習(xí)兩者的備份和恢復(fù)。

創(chuàng)新互聯(lián)是一家專業(yè)提供鐵山企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)、H5頁(yè)面制作、小程序制作等業(yè)務(wù)。10年已為鐵山眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。

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

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

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

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

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

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

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

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

FLUSH TABLES WITH READ LOCK;

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

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

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

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

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

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

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

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

(1)、mongodump備份工具

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

 
 
 
  1. [root@localhost bin]# ./mongodump --help  
  2. options:  
  3. --help produce help message  
  4. -v [ --verbose ] be more verbose (include multiple times for more  
  5. verbosity e.g. -vvvvv)  
  6. -h [ --host ] arg mongo host to connect to ( /s1,s2 for  
  7. sets)  
  8. --port arg server port. Can also use --host hostname:port  
  9. --ipv6 enable IPv6 support (disabled by default)  
  10. -u [ --username ] arg username  
  11. -p [ --password ] arg password  
  12. --dbpath arg directly access mongod database files in the given  
  13. path, instead of connecting to a mongod server -  
  14. needs to lock the data directory, so cannot be used  
  15. if a mongod is currently accessing the same path  
  16. --directoryperdb if dbpath specified, each db is in a separate  
  17. directory  
  18. -d [ --db ] arg database to use  
  19. -c [ --collection ] arg collection to use (some commands)  
  20. -o [ --out ] arg (=dump) output directory or "-" for stdout  
  21. -q [ --query ] arg json query  
  22. --oplog Use oplog for point-in-time snapshotting  
  23. --repair try to recover a crashed database  
  24. [root@localhost bin]#  

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

 
 
 
  1. [root@localhost bin]# ./mongodump -d foo -o /data/dump  
  2. connected to: 127.0.0.1  
  3. DATABASE: foo to /data/dump/foo  
  4. foo.system.indexes to /data/dump/foo/system.indexes.bson  
  5. 3 objects  
  6. foo.system.users to /data/dump/foo/system.users.bson  
  7. 1 objects  
  8. foo.t2 to /data/dump/foo/t2.bson  
  9. 1 objects  
  10. foo.t1 to /data/dump/foo/t1.bson  
  11. 2 objects  
  12. [root@localhost bin]#  

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

 
 
 
  1. [root@localhost dump]# ll /data/dump/foo/  
  2. 總計(jì) 16  
  3. -rw-r--r-- 1 root root 193 04-22 11:55 system.indexes.bson  
  4. -rw-r--r-- 1 root root 91 04-22 11:55 system.users.bson  
  5. -rw-r--r-- 1 root root 66 04-22 11:55 t1.bson  
  6. -rw-r--r-- 1 root root 49 04-22 11:55 t2.bson  
  7. [root@localhost dump]#  

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

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

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

 
 
 
  1. [root@localhost bin]# ./mongorestore --help  
  2. usage: ./mongorestore [options] [directory or filename to restore from]  
  3. options:  
  4. --help produce help message  
  5. -v [ --verbose ] be more verbose (include multiple times for more  
  6. verbosity e.g. -vvvvv)  
  7. -h [ --host ] arg mongo host to connect to ( /s1,s2 for sets)  
  8. --port arg server port. Can also use --host hostname:port  
  9. --ipv6 enable IPv6 support (disabled by default)  
  10. -u [ --username ] arg username  
  11. -p [ --password ] arg password  
  12. --dbpath arg directly access mongod database files in the given  
  13. path, instead of connecting to a mongod server -  
  14. needs to lock the data directory, so cannot be used  
  15. if a mongod is currently accessing the same path  
  16. --directoryperdb if dbpath specified, each db is in a separate  
  17. directory  
  18. -d [ --db ] arg database to use  
  19. -c [ --collection ] arg collection to use (some commands)  
  20. --objcheck validate object before inserting  
  21. --filter arg filter to apply before inserting  
  22. --drop drop each collection before import  
  23. --oplogReplay replay oplog for point-in-time restore  
  24. [root@localhost bin]# 

例如我們先將”foo”庫(kù)刪除了:

 
 
 
  1. [root@localhost bin]# ./mongo  
  2. MongoDB shell version: 1.8.1  
  3. connecting to: test  
  4. > use foo  
  5. switched to db foo  
  6. > db.dropDatabase();  
  7. { "dropped" : "foo", "ok" : 1 }  
  8. > show dbs  
  9. admin 0.0625GB  
  10. local (empty)  
  11. test 0.0625GB  

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

 
 
 
  1. [root@localhost bin]# ./mongorestore --directoryperdb /data/dump  
  2. connected to: 127.0.0.1  
  3. Sun Apr 22 12:01:27 /data/dump/foo/t1.bson  
  4. Sun Apr 22 12:01:27 going into namespace [foo.t1]  
  5. Sun Apr 22 12:01:27 2 objects found  
  6. Sun Apr 22 12:01:27 /data/dump/foo/t2.bson  
  7. Sun Apr 22 12:01:27 going into namespace [foo.t2]  
  8. Sun Apr 22 12:01:27 1 objects found  
  9. Sun Apr 22 12:01:27 /data/dump/foo/system.users.bson  
  10. Sun Apr 22 12:01:27 going into namespace [foo.system.users]  
  11. Sun Apr 22 12:01:27 1 objects found  
  12. Sun Apr 22 12:01:27 /data/dump/foo/system.indexes.bson  
  13. Sun Apr 22 12:01:27 going into namespace [foo.system.indexes]  
  14. Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.system.users", key: { _id: 1 }, v: 0 }  
  15. Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.t2", key: { _id: 1 }, v: 0 }  
  16. Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.t1", key: { _id: 1 }, v: 0 }  
  17. Sun Apr 22 12:01:27 3 objects found  
  18. [root@localhost bin]# 

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

 
 
 
  1. [root@localhost bin]# ./mongo  
  2. MongoDB shell version: 1.8.1  
  3. connecting to: test  
  4. > use foo  
  5. switched to db foo  
  6. > show collections;  
  7. system.indexes  
  8. system.users  
  9. t1  
  10. t2  

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

【編輯推薦】

  1. 教你如何利用MySQL學(xué)習(xí)MongoDB之SQL語(yǔ)法
  2. 教你如何利用MySQL學(xué)習(xí)MongoDB之?dāng)?shù)據(jù)存儲(chǔ)結(jié)構(gòu)
  3. 如何解決PHP+MySQL出現(xiàn)亂碼的現(xiàn)象
  4. 教你如何利用MySQL學(xué)習(xí)MongoDB之安裝篇
  5. MySQL配置時(shí)提示無(wú)法連接到MySQL本地服務(wù)器

文章名稱:教你如何利用MySQL學(xué)習(xí)MongoDB之備份和恢復(fù)
文章出自:http://www.5511xx.com/article/dhjocss.html