Mysql 数据恢复实战 (Win & Liunx 版本)

mysql 日志有两种,一个是redolog 用于事务提交,一个就是我们今天要用到的binlog 了

binlog 作用

  • 数据恢复:通过mysqlbinog工具进行恢复;
  • 数据复制:MySQL Replication在Master端开启binlog,Mster把它的二进制日志传递给slaves来达到master-slave数据一致的目的。
  1. 查看MySQL的日志情况(是否开启等),当MySQL还没开启时候,通过命令:show variables like ‘log_bin%'查看
mysql> show variables like 'log_bin%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| log_bin                         | OFF   |
| log_bin_basename                |       |
| log_bin_index                   |       |
| log_bin_trust_function_creators | OFF   |
| log_bin_use_v1_row_events       | OFF   |
+---------------------------------+-------+
5 rows in set (0.02 sec)

那如何在Wnidows在修改log_bin状态为ON开启呢?

找到C:\ProgramData\MySQL\MySQL Server 5.7\my.ini文件(注意:是C盘下隐藏文件夹ProgramData,而不是Program Files下)  

增加/修改常用的配置属性

常用简单属性说明:除了以上log_bin、binlog_format两个简单的配置外,还可以有其他的属性配置
log-bin = /xxx/xxx/mysql_bin #binlog日志文件,以mysql_bin开头,六个数字结尾的文件:mysql_bin.000001,并且会将文件存储在相应的xxx/xxx路径下,如果只配置mysql_bin的话默认在C:\ProgramData\MySQL\MySQL Server 5.7\Data下;
binlog_format = ROW #binlog日志格式,默认为STATEMENT:每一条SQL语句都会被记录;ROW:仅记录哪条数据被修改并且修改成什么样子,是binlog开启并且能恢复数据的关键;
expire_logs_days= 7 #binlog过期清理时间;
max_binlog_size = 100m #binlog每个日志文件大小;
binlog_cache_size = 4m #binlog缓存大小;
max_binlog_cache_size = 512m #最大binlog缓存大小。

2、恢复数据测试

2、恢复数据测试
(1)准备表user

mysql> select * from user;
+----+----------+----------------------------------+
| id | name     | password                         |
+----+----------+----------------------------------+
|  1 | Zhangsan | 2d7284808e5111e8af74201a060059ce |
|  2 | Lisi     | 2d73641c8e5111e8af74201a060059ce |
|  3 | Wangwu   | 2d73670c8e5111e8af74201a060059ce |
+----+----------+----------------------------------+

3 rows in set
(2)误update恢复,比如我在update user set name = 'Lijian' where id = 1;的时候忘写where id = 1重要条件,结果导致全部数据被更新

mysql> update user set name ='Lijian';
Query OK, 3 rows affected
Rows matched: 3  Changed: 3  Warnings: 0
mysql> select*from user;
+----+--------+----------------------------------+
| id | name   | password                         |
+----+--------+----------------------------------+
|  1 | Lijian | 2d7284808e5111e8af74201a060059ce |
|  2 | Lijian | 2d73641c8e5111e8af74201a060059ce |
|  3 | Lijian | 2d73670c8e5111e8af74201a060059ce |
+----+--------+----------------------------------+
3 rows in set

  这个时候你肯定很慌,但是先不要慌(实际上慌也没用),先看没有备份,如果没有再看是否开启binlog(show variables like ‘log_bin%'),如果两者都没有(我相信大家都会定时备份+binlog)从数据库这个层面是无法恢复的了,如果binlog开启的话,一切都好说。就开始执行下面几步恢复吧!

  第一步:找到当前mysql记录的binlog文件,执行show master status;

  第二步:查看binlog,定位误操作的pos或者时间段。执行show binlog events in 'mysql_bin.000001';

mysql> show binlog events in 'mysql_bin.000001';
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                  |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql_bin.000001 |   4 | Format_desc    |         1 |         123 | Server ver: 5.7.12-log, Binlog ver: 4 |
| mysql_bin.000001 | 123 | Previous_gtids |         1 |         154 |                                       |
| mysql_bin.000001 | 154 | Anonymous_Gtid |         1 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql_bin.000001 | 219 | Query          |         1 |         291 | BEGIN                                 |
| mysql_bin.000001 | 291 | Table_map      |         1 |         344 | table_id: 108 (test.user)             |
| mysql_bin.000001 | 344 | Update_rows    |         1 |         650 | table_id: 108 flags: STMT_END_F       |
| mysql_bin.000001 | 650 | Xid            |         1 |         681 | COMMIT /* xid=22 */                   |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
7 rows in set

  第三步:进入C:\ProgramData\MySQL\MySQL Server 5.7\Data执行mysqlbinlog --start-position=219 --stop-position=681 mysql-bin.000001 > e:\update.sql将update部分单独备份出来到E盘下为update.sql  

  

  第四步:登录mysql(mysql -uroot -p123);

  第五步:执行source e:update.sql恢复数据,部分截图如下:

  

  第六步:查看结果

mysql> select * from user;
+----+----------+----------------------------------+
| id | name     | password                         |
+----+----------+----------------------------------+
|  1 | Zhangsan | 2d7284808e5111e8af74201a060059ce |
|  2 | Lisi     | 2d73641c8e5111e8af74201a060059ce |
|  3 | Wangwu   | 2d73670c8e5111e8af74201a060059ce |
+----+----------+----------------------------------+
3 rows in set

posted @ 2020-12-29 14:08  朝明  阅读(168)  评论(0编辑  收藏  举报