当前位置:Gxlcms > mysql > MySQL备份与恢复之热拷贝(4)_MySQL

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

时间:2021-07-01 10:21:17 帮助过:37人阅读

在上一篇文章中我们提到热备,热备也就是在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命令,希望大家可以亲自动手操作一下!

人气教程排行