Linux学习-MySQL备份和还原

Posted 丢爸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux学习-MySQL备份和还原相关的知识,希望对你有一定的参考价值。

备份:副本
RAID:保证硬件损坏而不会业务中止

备份类型
热备份:读写不受影响
温备份:仅可以执行读操作
冷备份:读写均不能执行

物理备份:复制数据文件
逻辑备份:将数据导出至文本文件中

完全备份:备份全部数据
增量备份:仅备份上次完全备份或增量备份以后变化的数据
差异备份:仅备份上次完全备份以来变化的数据

还原

备份什么?
数据、配置文件、二进制日志、事务日志
热备份:
MyISAM:几乎不可能,借助于LVM实现温备份
InnoDB:可以热备,xtrabackup,mysqldump

物理备份:速度快,直接复制文件。
逻辑备份:速度慢,需要将数据导出为文本文件保存,会丢失浮点数精度,方便使用处理工具对其处理,可移植性好。

备份策略:完全+增量,完全+差异

mysql备份工具:
mysqldump:逻辑备份工具,MyISAM(温备),InnoDB(热备)
mysqlhotcopy:物理备份工具,温备份
文件系统工具:
cp:冷备
lvm:逻辑卷的快照,几乎热备
mysql > flush tables;
mysql > lock tables;
创建快照,释放锁,而后复制数据

	InnoDB:

第三方工具
ibbackup:商业工具
xtrabackup:开源工具

mysqldump:逻辑备份
mysqldump(完全备份)+二进制日志
完全+增量
备份单个数据库,或库中特定表
mysqldump DB_NAME[tb1][tb2]
–master-data=n[0,1,2]
0表示不记录二进制日志文件及位置
1以CHANGE MASTER TO 的方式记录位置,可用于恢复后直接启动从服务器
2以CHANGE MASTER TO 的方式记录位置,默认为被注释
–lock-all-tables:锁定所有表
–flush-logs:滚动二进制日志
如果指定库中的表类型均为InnoDB,可以使用–single-transaction启动热备
–all-databases:备份所有库
–databases DB_NAME,DB_NAME,…备份指定库
–events:备份事件
–routines:备份存储过程和存储函数
–triggers:备份触发器

#备份之前先进行锁表(使用读锁)
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
#滚动二进制日志
mysql> flush logs;
Query OK, 0 rows affected (0.00 sec)
#查看二进制日志列表,查看最后二进制日志的位置
mysql> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000002 |       120 |
| mysql-bin.000003 |       120 |
| mysql-bin.000004 |      5268 |
| mysql-bin.000005 |       120 |
+------------------+-----------+
4 rows in set (0.00 sec)

#导出mysql数据库为.sql文件
[root@mail ~]# mysqldump -uroot -ppassword students >/root/students.sql
Warning: Using a password on the command line interface can be insecure.
#备份之后对表进行解锁
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

#重新创建一个数据库,用于数据导入
mysql> create database teststu;
Query OK, 1 row affected (0.02 sec)
[root@mail ~]# mysql -uroot -ppassword teststu < /root/students.sql
Warning: Using a password on the command line interface can be insecure.
#查看数据导入情况
mysql> use teststu;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-------------------+
| Tables_in_teststu |
+-------------------+
| courses           |
| student           |
| testcourse        |
| tutors            |
+-------------------+
4 rows in set (0.00 sec)

mysql> select * from courses;
+-----+------------------+------+
| CID | Cname            | TID  |
+-----+------------------+------+
|   1 | Hamagong         |    2 |
|   2 | Taijiquan        |    3 |
|   3 | Yiyangzhi        |    6 |
|   4 | Jinshejianfa     |    1 |
|   5 | Qianzhuwandushou |    4 |
|   6 | Qishangquan      |    5 |
|   7 | Qiankundanuoyi   |    7 |
|   8 | Wanliduxing      |    8 |
|   9 | Pixiejianfa      |    3 |
|  10 | Jiuyinbaiguzhua  |    7 |
+-----+------------------+------+
10 rows in set (0.00 sec)
#备份所有数据库,通过--master-data参数备份后,可以查看当前二进制日志的位置
[root@mail ~]# mysqldump -uroot -ppassword --all-databases --lock-all-tables --flush-logs --master-data=2 >/root/all.sql
Warning: Using a password on the command line interface can be insecure.
#查看/root/all.sql中可以查看到二进制日志的位置
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000006', MASTER_LOG_POS=120;

备份策略:每周完全+每日增量
完全备份:mysqldump
增量备份:备份二进制日志文件(flush logs)

#1.通过mysqldump进行数据库完全备份
[root@mail ~]# mysqldump -uroot -ppassword --all-databases --lock-all-tables --flush-logs --master-data=2 > /root/all.sql
#2.模拟第一天增备份,对teststu.tutors表中大于70的数据执行删除
mysql> select * from tutors;
+-----+--------------+--------+------+
| TID | Tname        | Gender | Age  |
+-----+--------------+--------+------+
|   2 | HuangYaoshi  | M      |   63 |
|   3 | Miejueshitai | F      |   72 |
|   4 | OuYangfeng   | M      |   76 |
|   6 | YuCanghai    | M      |   56 |
|   7 | Jinlunfawang | M      |   67 |
|   8 | HuYidao      | M      |   42 |
|   9 | NingZhongze  | M      |   49 |
|  10 | Tom          | F      |   30 |
|  11 | DingDian     | M      |   25 |
|  12 | HuFei        | M      |   31 |
|  13 | Xuzhu        | M      |   26 |
|  14 | LingHuchong  | M      |   70 |
|  15 | tye          | M      |   34 |
|  16 | L1           | F      |   12 |
+-----+--------------+--------+------+
14 rows in set (0.00 sec)
mysql> delete from tutors where Age>70;
Query OK, 2 rows affected (0.00 sec)

mysql> select * from tutors;
+-----+--------------+--------+------+
| TID | Tname        | Gender | Age  |
+-----+--------------+--------+------+
|   2 | HuangYaoshi  | M      |   63 |
|   6 | YuCanghai    | M      |   56 |
|   7 | Jinlunfawang | M      |   67 |
|   8 | HuYidao      | M      |   42 |
|   9 | NingZhongze  | M      |   49 |
|  10 | Tom          | F      |   30 |
|  11 | DingDian     | M      |   25 |
|  12 | HuFei        | M      |   31 |
|  13 | Xuzhu        | M      |   26 |
|  14 | LingHuchong  | M      |   70 |
|  15 | tye          | M      |   34 |
|  16 | L1           | F      |   12 |
+-----+--------------+--------+------+
12 rows in set (0.00 sec)
#删除完成后,对数据进行增量备份
#通过all.sql中查看二进制的位置
[root@mail data]# cp mysql-bin.000008 /root/
#3.模拟第3天备份,在teststu.tutors表中添加两条数据
[root@mail data]# cp mysql-bin.000009 /root/
#4.模拟数据库损坏,删除/data下所有数据
#删除所有mysqld进程
[root@mail data]# killall mysqld
#重新初使化数据库
[root@mail mysql]# scripts/mysql_install_db --user=mysql --datadir=/data
[root@mail mysql]# service mysqld start
Starting MySQL. SUCCESS! 
#修改管理员密码
[root@mail mysql]# mysqladmin -u root password '123456'
#先将完全备份all.sql进行导入
[root@mail mysql]# mysql -uroot -p < /root/all.sql
Enter password: 
[root@mail mysql]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \\g.
Your MySQL connection id is 6
Server version: 5.6.10-log MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cactidb            |
| cactiuser          |
| edb                |
| extmail            |
| filemanager        |
| mydb               |
| mysql              |
| performance_schema |
| students           |
| test               |
| teststu            |
| vsftpd             |
+--------------------+
13 rows in set (0.00 sec)
#导入后的数据如下没有第一天和第二天操作的数据:
mysql> use teststu;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from tutors;
+-----+--------------+--------+------+
| TID | Tname        | Gender | Age  |
+-----+--------------+--------+------+
|   2 | HuangYaoshi  | M      |   63 |
|   3 | Miejueshitai | F      |   72 |
|   4 | OuYangfeng   | M      |   76 |
|   6 | YuCanghai    | M      |   56 |
|   7 | Jinlunfawang | M      |   67 |
|   8 | HuYidao      | M      |   42 |
|   9 | NingZhongze  | M      |   49 |
|  10 | Tom          | F      |   30 |
|  11 | DingDian     | M      |   25 |
|  12 | HuFei        | M      |   31 |
|  13 | Xuzhu        | M      |   26 |
|  14 | LingHuchong  | M      |   70 |
|  15 | tye          | M      |   34 |
|  16 | L1           | F      |   12 |
+-----+--------------+--------+------+
14 rows in set (0.00 sec)
#先对第一天数据和第二天数据进行导入,先将二进制文件转换为.sql文件
[root@mail ~]# mysqlbinlog mysql-bin.000008 > first.sql
[root@mail ~]# mysqlbinlog mysql-bin.000009 > second.sql
#恢复第1天数据
[root@mail ~]# mysql -uroot -p < first.sql
Enter password: 
#恢复完第1天数据后,查看tutors表
mysql> select * from tutors;
+-----+--------------+--------+------+
| TID | Tname        | Gender | Age  |
+-----+--------------+--------+------+
|   2 | HuangYaoshi  | M      |   63 |
|   6 | YuCanghai    | M      |   56 |
|   7 | Jinlunfawang | M      |   67 |
|   8 | HuYidao      | M      |   42 |
|   9 | NingZhongze  | M      |   49 |
|  10 | Tom          | F      |   30 |
|  11 | DingDian     | M      |   25 |
|  12 | HuFei        | M      |   31 |
|  13 | Xuzhu        | M      |   26 |
|  14 | LingHuchong  | M      |   70 |
|  15 | tye          | M      |   34 |
|  16 | L1           | F      |   12 |
+-----+--------------+--------+------+
12 rows in set (0.00 sec)
#恢复完第2天数据后,查看tutors表,数据恢复完成
mysql> select * from tutors;
+-----+--------------+--------+------+
| TID | Tname        | Gender | Age  |
+-----+--------------+--------+------+
|   2 | HuangYaoshi  | M      |   63 |
|   6 | YuCanghai    | M      |   56 |
|   7 | Jinlunfawang | M      |   67 |
|   8 | HuYidao      | M      |   42 |
|   9 | NingZhongze  | M      |   49 |
|  10 | Tom          | F      |   30 |
|  11 | DingDian     | M      |   25 |
|  12 | HuFei        | M      |   31 |
|  13 | Xuzhu        | M      |   26 |
|  14 | LingHuchong  | M      |   70 |
|  15 | tye          | M      |   34 |
|  16 | L1           | F      |   12 |
|  17 | L2           | M      |   34 |
|  18 | L3           | F      |   87 |
+-----+--------------+--------+------+
14 rows in set (0.00 sec)

以上是关于Linux学习-MySQL备份和还原的主要内容,如果未能解决你的问题,请参考以下文章

[知了堂学习笔记]_Java代码实现MySQL数据库的备份与还原

[Linux] PHP程序员玩转Linux系列-备份还原MySQL

linux命令备份和还原mysql数据库

linux下 mysql数据库的备份和还原

linux下 mysql数据库的备份和还原

Linux命令:MySQL系列之十二--MySQL备份与还原(重要章节)