欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL备份与恢复之热拷贝(4)

程序员文章站 2024-02-25 20:13:15
       在上一篇文章中我们提到热备,热备也就是在mysql或者其他数据库服务在运行的情况下进行备份。本文分...

       在上一篇文章中我们提到热备,热备也就是在mysql或者其他数据库服务在运行的情况下进行备份。本文分享另外一种备份的方法,也就是热拷贝。热拷贝跟热备很类似,只不过热备使用mysqldump命令,热拷贝使用mysqlhotcopy命令。热拷贝的优势在于支持服务运行中进行备份,速度快,性能好;劣势在于只能备份myisam的表,无法备份innodb的表。所以在生产环境中应该酌情使用。

示意图

MySQL备份与恢复之热拷贝(4)

热备模拟

第一步,热拷贝

[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命令,希望大家可以亲自动手操作一下!