偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

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

在數(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)說(shuō),還需要備份其日志文件,即 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)說(shuō)它是最快的途徑,但它只能運(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ù)器
責(zé)任編輯:艾婧 來(lái)源: itpub
相關(guān)推薦

2011-05-24 09:23:16

MySQLMongoDB

2011-05-24 09:51:07

MySQLMongoDB

2011-05-23 09:23:19

MySQLMongoDB

2011-05-24 09:10:24

MySQLMongoDB

2011-05-23 13:30:00

MySQLMongoDB

2023-08-03 07:39:10

MongoDB數(shù)據(jù)備份

2010-04-22 18:37:18

Aix系統(tǒng)

2023-12-07 15:09:23

2010-03-31 10:39:40

RMANOracle

2010-05-26 13:50:15

MySQL備份

2017-06-22 08:41:58

MySQLibd文件恢復(fù)數(shù)據(jù)

2011-03-04 14:39:03

MySQL數(shù)據(jù)庫(kù)mysqldump

2015-10-21 14:07:17

Oracle備份Oracle恢復(fù)

2011-08-04 18:27:55

注冊(cè)表

2010-05-21 18:15:41

MySQL 備份

2017-11-13 13:33:09

MySQL全備份恢復(fù)表

2013-11-08 09:31:21

數(shù)據(jù)備份企業(yè)安全

2012-05-15 13:55:18

Linux備份

2023-11-02 13:34:00

云計(jì)算聯(lián)合學(xué)習(xí)

2009-11-20 09:29:53

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)