自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

教你如何利用MySQL學習MongoDB之備份和恢復

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

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

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

1、MySQL備份和恢復

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

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

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

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

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

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

FLUSH TABLES WITH READ LOCK;

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

但對于 Innodb 類型表來說,還需要備份其日志文件,即 ib_logfile* 文件。因為當 Innodb 表損壞時,就可以依靠這些日志文件來恢復。

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

mysqlhotcopy 是perl程序。它使用 LOCK TABLES、FLUSH TABLES 和 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備份工具

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

  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)中有一個叫做”foo”庫,下面我們將演示如何將這個庫備份出來:

  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格式的文件了, 接下來我們到備份的目錄下去驗證一下:

  1. [root@localhost dump]# ll /data/dump/foo/  
  2. 總計 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庫中的表已經(jīng)被成功備份出來,接下來我們將演示如何將備份恢復回去。

(2)、mongorestore恢復工具

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

  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”庫刪除了:

  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  

然后下面我們將演示如何恢復這個庫:

  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)被恢復回來了, 接下來我們到庫里去驗證一下:

  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庫表已經(jīng)被成功恢復回來了。

 

【編輯推薦】

  1. 教你如何利用MySQL學習MongoDB之SQL語法
  2. 教你如何利用MySQL學習MongoDB之數(shù)據(jù)存儲結(jié)構(gòu)
  3. 如何解決PHP+MySQL出現(xiàn)亂碼的現(xiàn)象
  4. 教你如何利用MySQL學習MongoDB之安裝篇
  5. MySQL配置時提示無法連接到MySQL本地服務(wù)器
責任編輯:艾婧 來源: itpub
相關(guān)推薦

2011-05-24 09:51:07

MySQLMongoDB

2011-05-24 09:23:16

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-05-26 13:50:15

MySQL備份

2010-03-31 10:39:40

RMANOracle

2017-06-22 08:41:58

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

2011-03-04 14:39:03

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

2010-05-21 18:15:41

MySQL 備份

2015-10-21 14:07:17

Oracle備份Oracle恢復

2017-11-13 13:33:09

MySQL全備份恢復表

2011-08-04 18:27:55

注冊表

2011-07-26 13:55:01

MongoDB備份與恢復

2017-07-10 14:26:03

Mysql數(shù)據(jù)備份數(shù)據(jù)恢復

2023-12-01 10:21:00

機器學習算法

2011-09-14 15:30:00

MongoDB
點贊
收藏

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