教你如何利用MySQL學(xué)習(xí)MongoDB之備份和恢復(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備份工具
我們先看一下此工具的幫助信息:
- [root@localhost bin]# ./mongodump --help
- options:
- --help produce help message
- -v [ --verbose ] be more verbose (include multiple times for more
- verbosity e.g. -vvvvv)
- -h [ --host ] arg mongo host to connect to ( /s1,s2 for
- sets)
- --port arg server port. Can also use --host hostname:port
- --ipv6 enable IPv6 support (disabled by default)
- -u [ --username ] arg username
- -p [ --password ] arg password
- --dbpath arg directly access mongod database files in the given
- path, instead of connecting to a mongod server -
- needs to lock the data directory, so cannot be used
- if a mongod is currently accessing the same path
- --directoryperdb if dbpath specified, each db is in a separate
- directory
- -d [ --db ] arg database to use
- -c [ --collection ] arg collection to use (some commands)
- -o [ --out ] arg (=dump) output directory or "-" for stdout
- -q [ --query ] arg json query
- --oplog Use oplog for point-in-time snapshotting
- --repair try to recover a crashed database
- [root@localhost bin]#
例如我們的系統(tǒng)中有一個(gè)叫做”foo”庫(kù),下面我們將演示如何將這個(gè)庫(kù)備份出來(lái):
- [root@localhost bin]# ./mongodump -d foo -o /data/dump
- connected to: 127.0.0.1
- DATABASE: foo to /data/dump/foo
- foo.system.indexes to /data/dump/foo/system.indexes.bson
- 3 objects
- foo.system.users to /data/dump/foo/system.users.bson
- 1 objects
- foo.t2 to /data/dump/foo/t2.bson
- 1 objects
- foo.t1 to /data/dump/foo/t1.bson
- 2 objects
- [root@localhost bin]#
通過工具返回信息,我們可以看到foo中的數(shù)據(jù)已經(jīng)被備份成bson格式的文件了, 接下來(lái)我們到備份的目錄下去驗(yàn)證一下:
- [root@localhost dump]# ll /data/dump/foo/
- 總計(jì) 16
- -rw-r--r-- 1 root root 193 04-22 11:55 system.indexes.bson
- -rw-r--r-- 1 root root 91 04-22 11:55 system.users.bson
- -rw-r--r-- 1 root root 66 04-22 11:55 t1.bson
- -rw-r--r-- 1 root root 49 04-22 11:55 t2.bson
- [root@localhost dump]#
結(jié)果證明foo庫(kù)中的表已經(jīng)被成功備份出來(lái),接下來(lái)我們將演示如何將備份恢復(fù)回去。
(2)、mongorestore恢復(fù)工具
我們先看一下此工具的幫助信息:
- [root@localhost bin]# ./mongorestore --help
- usage: ./mongorestore [options] [directory or filename to restore from]
- options:
- --help produce help message
- -v [ --verbose ] be more verbose (include multiple times for more
- verbosity e.g. -vvvvv)
- -h [ --host ] arg mongo host to connect to ( /s1,s2 for sets)
- --port arg server port. Can also use --host hostname:port
- --ipv6 enable IPv6 support (disabled by default)
- -u [ --username ] arg username
- -p [ --password ] arg password
- --dbpath arg directly access mongod database files in the given
- path, instead of connecting to a mongod server -
- needs to lock the data directory, so cannot be used
- if a mongod is currently accessing the same path
- --directoryperdb if dbpath specified, each db is in a separate
- directory
- -d [ --db ] arg database to use
- -c [ --collection ] arg collection to use (some commands)
- --objcheck validate object before inserting
- --filter arg filter to apply before inserting
- --drop drop each collection before import
- --oplogReplay replay oplog for point-in-time restore
- [root@localhost bin]#
例如我們先將”foo”庫(kù)刪除了:
- [root@localhost bin]# ./mongo
- MongoDB shell version: 1.8.1
- connecting to: test
- > use foo
- switched to db foo
- > db.dropDatabase();
- { "dropped" : "foo", "ok" : 1 }
- > show dbs
- admin 0.0625GB
- local (empty)
- test 0.0625GB
- >
然后下面我們將演示如何恢復(fù)這個(gè)庫(kù):
- [root@localhost bin]# ./mongorestore --directoryperdb /data/dump
- connected to: 127.0.0.1
- Sun Apr 22 12:01:27 /data/dump/foo/t1.bson
- Sun Apr 22 12:01:27 going into namespace [foo.t1]
- Sun Apr 22 12:01:27 2 objects found
- Sun Apr 22 12:01:27 /data/dump/foo/t2.bson
- Sun Apr 22 12:01:27 going into namespace [foo.t2]
- Sun Apr 22 12:01:27 1 objects found
- Sun Apr 22 12:01:27 /data/dump/foo/system.users.bson
- Sun Apr 22 12:01:27 going into namespace [foo.system.users]
- Sun Apr 22 12:01:27 1 objects found
- Sun Apr 22 12:01:27 /data/dump/foo/system.indexes.bson
- Sun Apr 22 12:01:27 going into namespace [foo.system.indexes]
- Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.system.users", key: { _id: 1 }, v: 0 }
- Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.t2", key: { _id: 1 }, v: 0 }
- Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.t1", key: { _id: 1 }, v: 0 }
- Sun Apr 22 12:01:27 3 objects found
- [root@localhost bin]#
通過工具返回信息,我們可以看到foo中的數(shù)據(jù)已經(jīng)被恢復(fù)回來(lái)了, 接下來(lái)我們到庫(kù)里去驗(yàn)證一下:
- [root@localhost bin]# ./mongo
- MongoDB shell version: 1.8.1
- connecting to: test
- > use foo
- switched to db foo
- > show collections;
- system.indexes
- system.users
- t1
- t2
- >
結(jié)果證明foo庫(kù)表已經(jīng)被成功恢復(fù)回來(lái)了。
【編輯推薦】