MySQL使用二进制日志来恢复数据

mysqlbinlog工具的使用,大家可以看MySQL的帮助手册。里面有详细的用,

在这个例子中,重点是--start-position参数和--stop-position参数的使用。

 

·--start-position=N

 

从二进制日志中第个位置等于N参量时的事件开始读。

 

·--stop-position=N

 

从二进制日志中第个位置等于和大于N参量时的事件起停止读。

 

 

 

OK,现在开始,要启动二进制日志记录,要先在my.cnf / my.ini文件的mysqld里添加

 

log-bin=日志名

 

在这里,偶是的设置是log-bin=liangck

 

然后再启动mysql服务,因为偶是用windows系统,所以执行net start mysql命令即可。

 

 

 

然后在一测试数据库里,创建一个表,并添加记录。

 

mysql> create table test(id int auto_increment not null primary key,val int,data varchar(20));

 

mysql> insert into test(val,data) values(10,'liang');

 

Query OK, 1 row affected (0.03 sec)

 

mysql> insert into test(val,data) values(20,'jia');

 

Query OK, 1 row affected (0.08 sec)

 

mysql> insert into test(val,data) values(30,'hui');

 

Query OK, 1 row affected (0.03 sec)

 

mysql> flush logs;   --产生第二个日志文件

 

Query OK, 0 rows affected (0.09 sec)

 

mysql> insert into test(val,data) values(40,'aaa');

 

Query OK, 1 row affected (0.05 sec)

 

mysql> insert into test(val,data) values(50,'bbb');

 

Query OK, 1 row affected (0.03 sec)

 

mysql> insert into test(val,data) values(60,'ccc');

 

Query OK, 1 row affected (0.03 sec)

 

mysql> delete from test where id between 4 and 5;  --删除记录

 

Query OK, 2 rows affected (0.05 sec)

 

mysql> insert into test(val,data) values(70,'ddd');

 

Query OK, 1 row affected (0.03 sec)

 

mysql> flush logs;          --产生第三个文件文件

 

Query OK, 0 rows affected (0.11 sec)

 

mysql> insert into test(val,data) values(80,'dddd');

 

Query OK, 1 row affected (0.05 sec)

 

mysql> insert into test(val,data) values(90,'eeee');

 

Query OK, 1 row affected (0.03 sec)

 

mysql> drop table test;       --删除表

 

Query OK, 0 row affected (0.05 sec)

 

――――――――――――――――――――――――――――――――――

 

OK,现在测试数据已经建好了,要求是什么呢?

 

就是将test表的数据全部恢复出来。

 

先用mysqlbinlog工具将日志文件生成txt文件出来分析。

 

F:/Program Files/MySQL_Data/data/log>mysqlbinlog liangck.000001 > G:/001.txt

 

F:/Program Files/MySQL_Data/data/log>mysqlbinlog liangck.000002 > G:/002.txt

 

F:/Program Files/MySQL_Data/data/log>mysqlbinlog liangck.000003 > G:/003.txt

 

通过这三个命令,可以在G盘下生成个文件,里面分别记录了日志文件的内容,也就是用户操作的步骤。

 

因为我们需要重做第一个日志文件的所有操作,所以这里只需要将第一个日志文件全恢复就行了。

 

F:/Program Files/MySQL_Data/data/log>mysqlbinlog liangck.000001 | mysql -uroot –p

 

Ok,接着,我们需要分析的是第二个日志文件。为什么要分析它呢,因为它中途执行了一个操作是DELETE,因为我们要做的是恢复全部数据,也就是我们不希望去重做这个语句。所以在这里我们要想办法去绕开它。

 

我们先打开.txt文件来分析一下。

 

/*

 

 

/*!40019 SET @@session.max_insert_delayed_threads=0*/;

 

/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;

 

DELIMITER /*!*/;

 

# at 4

 

#090427 15:27:56 server id 1  end_log_pos 106 Start: binlog v 4, server v 5.1.32-community-log created 090427 15:27:56

 

BINLOG '

 

fF71SQ8BAAAAZgAAAGoAAAAAAAQANS4xLjMyLWNvbW11bml0eS1sb2cAAAAAAAAAAAAAAAAAAAAA

 

AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC

 

'/*!*/;

 

# at 106

 

#090427 15:28:37 server id 1  end_log_pos 176 Query  thread_id=1   exec_time=0   error_code=0

 

use mytest/*!*/;

 

SET TIMESTAMP=1240817317/*!*/;

 

SET @@session.pseudo_thread_id=1/*!*/;

 

SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1, @@session.autocommit=1/*!*/;

 

SET @@session.sql_mode=1344274432/*!*/;

 

SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;

 

/*!/C gbk *//*!*/;

 

SET @@session.character_set_client=28,@@session.collation_connection=28,@@session.collation_server=28/*!*/;

 

SET @@session.lc_time_names=0/*!*/;

 

SET @@session.collation_database=DEFAULT/*!*/;

 

BEGIN

 

/*!*/;

 

# at 176

 

#090427 15:28:37 server id 1  end_log_pos 204 Intvar

 

SET INSERT_ID=4/*!*/;

 

# at 204

 

#090427 15:28:37 server id 1  end_log_pos 312 Query  thread_id=1   exec_time=0   error_code=0

 

SET TIMESTAMP=1240817317/*!*/;

 

insert into test(val,data) values(40,'aaa')

 

/*!*/;

 

# at 312

 

#090427 15:28:37 server id 1  end_log_pos 339 Xid = 12

 

COMMIT/*!*/;

 

# at 339

 

#090427 15:28:46 server id 1  end_log_pos 409 Query  thread_id=1   exec_time=0   error_code=0

 

SET TIMESTAMP=1240817326/*!*/;

 

BEGIN

 

/*!*/;

 

# at 409

 

#090427 15:28:46 server id 1  end_log_pos 437 Intvar

 

SET INSERT_ID=5/*!*/;

 

# at 437

 

#090427 15:28:46 server id 1  end_log_pos 545 Query  thread_id=1   exec_time=0   error_code=0

 

SET TIMESTAMP=1240817326/*!*/;

 

insert into test(val,data) values(50,'bbb')

 

/*!*/;

 

# at 545

 

#090427 15:28:46 server id 1  end_log_pos 572 Xid = 13

 

COMMIT/*!*/;

 

# at 572

 

#090427 15:29:35 server id 1  end_log_pos 642 Query  thread_id=1   exec_time=0   error_code=0

 

SET TIMESTAMP=1240817375/*!*/;

 

BEGIN

 

/*!*/;

 

# at 642

 

#090427 15:29:35 server id 1  end_log_pos 670 Intvar

 

SET INSERT_ID=6/*!*/;

 

# at 670

 

#090427 15:29:35 server id 1  end_log_pos 778 Query  thread_id=1   exec_time=0   error_code=0

 

SET TIMESTAMP=1240817375/*!*/;

 

insert into test(val,data) values(60,'ccc')

 

/*!*/;

 

# at 778

 

#090427 15:29:35 server id 1  end_log_pos 805 Xid = 14

 

COMMIT/*!*/;

 

# at 805

 

#090427 15:30:21 server id 1  end_log_pos 875 Query  thread_id=1   exec_time=0   error_code=0

 

SET TIMESTAMP=1240817421/*!*/;

 

BEGIN

 

/*!*/;

 

# at 875

 

#090427 15:30:21 server id 1  end_log_pos 981 Query  thread_id=1   exec_time=0   error_code=0

 

SET TIMESTAMP=1240817421/*!*/;

 

delete from test where id between 4 and 5

 

/*!*/;

 

# at 981

 

#090427 15:30:21 server id 1  end_log_pos 1008    Xid = 15

 

COMMIT/*!*/;

 

# at 1008

 

#090427 15:30:34 server id 1  end_log_pos 1078    Query  thread_id=1   exec_time=0    error_code=0

 

SET TIMESTAMP=1240817434/*!*/;

 

BEGIN

 

/*!*/;

 

# at 1078

 

#090427 15:30:34 server id 1  end_log_pos 1106    Intvar

 

SET INSERT_ID=7/*!*/;

 

# at 1106

 

#090427 15:30:34 server id 1  end_log_pos 1214    Query  thread_id=1   exec_time=0    error_code=0

 

SET TIMESTAMP=1240817434/*!*/;

 

insert into test(val,data) values(70,'ddd')

 

/*!*/;

 

# at 1214

 

#090427 15:30:34 server id 1  end_log_pos 1241    Xid = 16

 

COMMIT/*!*/;

 

# at 1241

 

#090427 15:30:41 server id 1  end_log_pos 1282    Rotate to liangck.000003  pos: 4

 

DELIMITER ;

 

# End of log file

 

ROLLBACK /* added by mysqlbinlog */;

 

/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;

 

 

 

―――――――――――――――――――――――――――――――――――――

 

*/

 

 

在这个文件中,我们可以看到DELETE的操作的起始位置是,终止位置是.那么我们只要重做第二个日志文件的开头到的操作,然后再从到末尾的操作,我们就可以把数据给恢复回来,而不会DELETE数据。所以执行两个命令:

 

F:/Program Files/MySQL_Data/data/log>mysqlbinlog liangck.000002 --stop-pos=875 | mysql -uroot -p

 

 

 

F:/Program Files/MySQL_Data/data/log>mysqlbinlog liangck.000002 --start-pos=1008 | mysql -uroot -p mytest

 

 

 

OK,现在第二个日志文件的数据了。

 

第三个日志文件也是同理,只要找到DROP TABLE的位置,就可以了。

 

F:/Program Files/MySQL_Data/data/log>mysqlbinlog liangck.000003 --stop-pos=574 | mysql -uroot –p

 

 

 

现在我们再查一下数据看看:

 

mysql> select * from test;

 

+----+------+-------+

 

| id | val  | data  |

 

+----+------+-------+

 

|  1 |   10 | liang |

 

|  2 |   20 | jia   |

 

|  3 |   30 | hui   |

 

|  4 |   40 | aaa   |

 

|  5 |   50 | bbb   |

 

|  6 |   60 | ccc   |

 

|  7 |   70 | ddd   |

 

|  8 |   80 | dddd  |

 

|  9 |   90 | eeee  |

 

+----+------+-------+

 

9 rows in set (0.00 sec)

 

 

 

可以看到,全部数据都回来了。

原文地址:https://www.cnblogs.com/ylqmf/p/2098160.html