在上一篇文章中我們提到熱備,熱備也就是在MySQL或者其他數據庫服務在運行的情況下進行備份。本文分享另外一種備份的方法,也就是熱拷貝。熱拷貝跟熱備很類似,只不過熱備使用mysqldump命令,熱拷貝使用mysqlhotcopy命令。熱拷貝的優勢在於支持服務運行中進行備份,速度快,性能好;劣勢在於只能備份MyIsam的表,無法備份InnoDB的表。所以在生產環境中應該酌情使用。
示意圖
熱備模擬
第一步,熱拷貝
[root@serv01 databackup]# mysqlhotcopy -uroot -p123456 --database larrydb > larrydb_hostcopy.sql Can't locate DBI.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/local/mysql/bin/mysqlhotcopy line 25. BEGIN failed--compilation aborted at /usr/local/mysql/bin/mysqlhotcopy line 25.
第二步,報錯。因為這個命令是用perl寫的或者此命令需要perl支持,所以需要安裝perl
[root@serv01 databackup]# yum install perl* -y
第三步,對數據庫larrydb熱拷貝
[root@serv01 databackup]# mysqlhotcopy --help
#第一種寫法
[root@serv01 databackup]# mysqlhotcopy --user=root --password=123456 larrydb /databackup/
#第二種寫法
[root@serv01 databackup]# mysqlhotcopy -u root -p 123456 larrydb /databackup/ Flushed 2 tables with read lock (`larrydb`.`class`, `larrydb`.`stu`) in 0 seconds. Locked 0 views () in 0 seconds. Copying 5 files... Copying indices for 0 files... Unlocked tables. mysqlhotcopy copied 2 tables (5 files) in 0 seconds (0 seconds overall).
第四步,模擬數據丟失
[root@serv01 databackup]# ll larrydb total 36 -rw-rw----. 1 mysql mysql 8590 Sep 10 19:07 class.frm -rw-rw----. 1 mysql mysql 65 Sep 10 19:07 db.opt -rw-rw----. 1 mysql mysql 8618 Sep 10 19:07 stu.frm -rw-rw----. 1 mysql mysql 48 Sep 10 19:07 stu.MYD -rw-rw----. 1 mysql mysql 1024 Sep 10 19:07 stu.MYI mysql> use larrydb; Database changed mysql> show tables; +-------------------+ | Tables_in_larrydb | +-------------------+ | class | | stu | +-------------------+ 2 rows in set (0.00 sec) mysql> show create table class \G; *************************** 1. row *************************** Table: class Create Table: CREATE TABLE `class` ( `cid` int(11) DEFAULT NULL, `cname` varchar(30) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec) ERROR: No query specified mysql> show create table stu \G; *************************** 1. row *************************** Table: stu Create Table: CREATE TABLE `stu` ( `sid` int(11) DEFAULT NULL, `sname` varchar(30) DEFAULT NULL, `cid` int(11) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 1 row in set (0.00 sec) ERROR: mysql> drop table class,stu; Query OK, 0 rows affected (0.01 sec) mysql> show tables; Empty set (0.00 sec) #這樣刪除會出錯,不要這樣刪除 [root@serv01 databackup]# rm -rf /usr/local/mysql/data/larrydb/* [root@serv01 databackup]# rm -rf /usr/local/mysql/data/larrydb/
第五步,恢復數據
[root@serv01 databackup]# cp larrydb /usr/local/mysql/data/ -arvf `larrydb' -> `/usr/local/mysql/data/larrydb' `larrydb/stu.MYI' -> `/usr/local/mysql/data/larrydb/stu.MYI' `larrydb/stu.MYD' -> `/usr/local/mysql/data/larrydb/stu.MYD' `larrydb/stu.frm' -> `/usr/local/mysql/data/larrydb/stu.frm' `larrydb/db.opt' -> `/usr/local/mysql/data/larrydb/db.opt' `larrydb/class.frm' -> `/usr/local/mysql/data/larrydb/class.frm' mysql> use larrydb; Database changed mysql> show tables; +-------------------+ | Tables_in_larrydb | +-------------------+ | class | | stu | +-------------------+ 2 rows in set (0.00 sec) mysql> select * from class; ERROR 1146 (42S02): Table 'larrydb.class' doesn't exist mysql> select * from stu; +------+---------+------+ | sid | sname | cid | +------+---------+------+ | 1 | larry01 | 1 | | 2 | larry02 | 2 | +------+---------+------+ 2 rows in set (0.00 sec) mysql> drop database larrydb; Query OK, 2 rows affected (0.00 sec) #再次導入 [root@serv01 databackup]# mysql -uroot -p123456 < larrydb.sql mysql> use larrydb; Database changed mysql> show tables; +-------------------+ | Tables_in_larrydb | +-------------------+ | class | | stu | +-------------------+ 2 rows in set (0.00 sec) mysql> select * from stu; +------+---------+------+ | sid | sname | cid | +------+---------+------+ | 1 | larry01 | 1 | | 2 | larry02 | 2 | +------+---------+------+ 2 rows in set (0.00 sec) mysql> select * from class; +------+--------+ | cid | cname | +------+--------+ | 1 | linux | | 2 | oracle | +------+--------+ 2 rows in set (0.00 sec)
本文介紹了另一種備份--熱拷貝,與熱備的區別在於使用的命令不同,熱拷貝使用mysqlhotcopy命令,希望大家可以親自動手操作一下!