mysqldump完全备份 和 二进制即时点还原数据库

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysqldump完全备份 和 二进制即时点还原数据库相关的知识,希望对你有一定的参考价值。

一 实验目标

1 掌握mysqldump用法
2 掌握 mysqldump 完全备份,增量备份
3 掌握 mysqldump 恢复数据库,二进制日志即时点还原

二 实验环境

操作系统:CentOS release 6.8 (Final)
应用软件:percona-server-5.6.29-76.2

三 备份策略

周完全+每日增量
完全备份: 使用mysqldump备份所有库
增量备份: 备份二进制日志文件

四 模拟过程

4.1 实验前准备

导入jiaowu.sql 数据
[[email protected] ~]# mysql < /root/jiaowu.sql
[[email protected] ~]# mysql
mysql> show databases;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.10 sec)
mysql> use jiaowu;

mysql> show tables;
+------------------+
| Tables_in_jiaowu |
+------------------+
| courses |
| scores |
| students |
| tutors |
+------------------+

4.2 用mysqldump 做完全备份

[[email protected] ~]# mysqldump -uroot -p --master-data=2 --lock-all-tables --flush-logs --all-databases > /root/alldatabases.sql

连接mysql

[[email protected] ~]# mysql
 mysql> SHOW BINARY LOGS;

+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 10389 |
| mysql-bin.000002 | 120 |
+------------------+-----------+

删掉旧二进制日志
mysql> PURGE BINARY LOGS TO ‘mysql-bin.000002‘;
Query OK, 0 rows affected (0.01 sec)

4.3 第一天增量备份

模拟数据库修改,删掉部分数据

[[email protected] ~]# mysql
mysql> use jiaowu;
mysql> SELECT * FROM tutors;

+-----+--------------+--------+------+
| TID | Tname | Gender | Age |
+-----+--------------+--------+------+
| 1 | HongQigong | M | 93 |
| 2 | HuangYaoshi | M | 63 |
| 3 | Miejueshitai | F | 72 |
| 4 | OuYangfeng | M | 76 |
| 5 | YiDeng | M | 90 |
| 6 | YuCanghai | M | 56 |
| 7 | Jinlunfawang | M | 67 |
| 8 | HuYidao | M | 42 |
| 9 | NingZhongze | F | 49 |
+-----+--------------+--------+------+

mysql> DELETE FROM tutors WHERE Age>90;
Query OK, 1 row affected (0.00 sec)

mysql> FLUSH LOGS;

mysql> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000002 | 390 |
| mysql-bin.000003 | 120 |
+------------------+-----------+
mysql> quit;
[[email protected] ~]# ls -l /data/mysql
[[email protected] ~]# cp -a /data/mysql/mysql-bin.000002 /root/
将二进制日志文件转换为sql脚本
[[email protected] ~]# mysqlbinlog /root/mysql-bin.000002 > /root/mon-incremental.sql

4.4 第二天数据库增量备份

[[email protected] ~]# mysql

mysql> use jiaowu;
mysql> INSERT INTO tutors (Tname) VALUES (‘stu123‘);
mysql> FLUSH LOGS;
mysql> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000002 | 390 |
| mysql-bin.000003 | 435 |
| mysql-bin.000004 | 120 |
+------------------+-----------+
mysql> QUIT;

备份第二天二进制日志文件
[[email protected] ~]# cp -a /data/mysql/mysql-bin.000003 /root/

4.5 删掉数据库,模拟数据损坏(已提前备份好数据和二进制日志文件)

[[email protected] ~]# rm -rf /data/mysql/*
[[email protected] ~]# service mysqld stop
MySQL (Percona Server) PID file could not be found! [FAILED]
[[email protected] ~]# killall mysqld
初始化mysql

[[email protected] ~]# /usr/local/mysql/scripts/mysql_install_db
--user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
[[email protected] ~]# service mysqld start

4.6 还原完全备份

[[email protected] ~]# mysql -uroot -p < /root/alldatabases.sql

4.7 恢复第一天增量备份

[[email protected] ~]# mysqlbinlog /root/mysql-bin.000002 | mysql -uroot –p

4.8 恢复第二天增量备份

[[email protected] ~]# mysqlbinlog /root/mysql-bin.000003 | mysql -uroot -p

以上是关于mysqldump完全备份 和 二进制即时点还原数据库的主要内容,如果未能解决你的问题,请参考以下文章

使用mysqldump工具对mysql数据库实现完全备份和还原

mysql数据库-备份与还原实操

MySQL之XtraBackup实现完全备份增量备份数据还原

MySQL之数据备份与和即时点还原

mysqlbinglog基于即时点还原

mysql的备份和恢复