mysqlbinlog的应用

 

<span><strong>一、mysql 日志的类型</strong></span>

分别为:

 错误日志:   -log-err   

 查询日志:   -log   
 慢查询日志:     -log-slow-queries   
 更新日志:     -log-update   
 二进制日志:   -log-bin  
事务性日志:ib_logfile
具体可以参看mysql官方文档或查看mysql基础篇:
<span><strong>二、mysqlbinlog进行数据</strong><strong>恢复的实例</strong></span>



<span>mysqlbinlog</span><span style="font-family:宋体;">工具的使用</span><span>,</span><span style="font-family:宋体;">大家可以看</span><span>MySQL</span><span style="font-family:宋体;">的帮助手册。里面有详细的用,在这个例子中,重点是</span><span>--start-position</span><span style="font-family:宋体;">参数和</span><span>--stop-position</span><span style="font-family:宋体;">参数的使用。</span>



<span></span>



<span style="font-family:宋体;color:red;">·</span><span style="color:red;">--start-position=N</span>



<span style="font-family:宋体;color:red;">从二进制日志中第</span><span style="color:red;">1</span><span style="font-family:宋体;color:red;">个位置等于</span><span style="color:red;">N</span><span style="font-family:宋体;color:red;">参量时的事件开始读。</span><span style="color:red;"></span>



<span style="font-family:宋体;color:red;">·</span><span style="color:red;">--stop-position=N</span>



<span style="font-family:宋体;color:red;">从二进制日志中第</span><span style="color:red;">1</span><span style="font-family:宋体;color:red;">个位置等于和大于</span><span style="color:red;">N</span><span style="font-family:宋体;color:red;">参量时的事件起停止读。</span><span style="color:red;"></span>



<span style="color:red;"></span>



<span style="color:black;">OK</span><span style="font-family:宋体;color:black;">,现在开始,要启动二进制日志记录,要先在</span><span style="color:black;">my.cnf / my.ini</span><span style="font-family:宋体;color:black;">文件的</span><span style="color:black;">mysqld</span><span style="font-family:宋体;color:black;">里添加</span><span style="color:black;"></span>

log-bin=日志名

<span style="font-family:宋体;color:black;">在这里,我的设置是</span><span style="color:black;">log-bin=boke</span>



<span style="color:black;"></span><span style="font-family:宋体;color:black;">然后再启动</span><span style="color:black;">mysql</span><span style="font-family:宋体;color:black;">服务,</span><span style="color:black;">windows</span><span style="font-family:宋体;color:black;">系统,就执行</span><span style="color:black;">net start mysql</span><span style="font-family:宋体;color:black;">命令,linux就执行service mysqld restart&nbsp;。</span><span style="color:black;"></span>



<span style="color:black;"></span>



<span style="font-family:宋体;color:black;">然后在一测试数据库里,创建一个表,并添加记录。</span><span style="color:black;"></span>

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;       删除表

<span style="font-family:'Courier New';font-size:10pt;">Query OK<span style="color:gray;">,</span> 0 row affected<span style="color:blue;"> </span><span style="color:gray;">(</span>0.05 sec<span style="color:gray;">)</span></span>



<span style="font-family:'Courier New';color:gray;font-size:10pt;"></span>



<span style="font-family:宋体;color:blue;font-size:10pt;">――――――――――――――――――――――――――――――――――</span><span style="font-family:'Courier New';color:blue;font-size:10pt;"></span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">OK</span><span style="font-family:宋体;color:black;font-size:10pt;">,现在测试数据已经建好了,要求是什么呢?</span><span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:宋体;color:black;font-size:10pt;">就是将</span><span style="font-family:'Courier New';color:black;font-size:10pt;">test</span><span style="font-family:宋体;color:black;font-size:10pt;">表的数据全部恢复出来。</span><span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:宋体;color:black;font-size:10pt;">先用</span><span style="font-family:'Courier New';color:black;font-size:10pt;">mysqlbinlog</span><span style="font-family:宋体;color:black;font-size:10pt;">工具将日志文件生成</span><span style="font-family:'Courier New';color:black;font-size:10pt;">txt</span><span style="font-family:宋体;color:black;font-size:10pt;">文件出来分析。</span><span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">F:Program FilesMySQL_Datadatalog>mysqlbinlog liangck.000001 > G:01.txt</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">F:Program FilesMySQL_Datadatalog>mysqlbinlog liangck.000002 > G:02.txt</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">F:Program FilesMySQL_Datadatalog>mysqlbinlog liangck.000003 > G:03.txt</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:宋体;color:black;font-size:10pt;">通过这三个命令,可以在</span><span style="font-family:'Courier New';color:black;font-size:10pt;">G</span><span style="font-family:宋体;color:black;font-size:10pt;">盘下生成</span><span style="font-family:'Courier New';color:black;font-size:10pt;">3</span><span style="font-family:宋体;color:black;font-size:10pt;">个文件,里面分别记录了日志文件的内容,也就是用户操作的步骤。</span><span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:宋体;color:black;font-size:10pt;">因为我们需要重做第一个日志文件的所有操作,所以这里只需要将第一个日志文件全恢复就行了。</span><span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">F:Program FilesMySQL_Datadatalog>mysqlbinlog liangck.000001 | mysql -uroot –p</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">Ok,</span><span style="font-family:宋体;color:black;font-size:10pt;">接着,我们需要分析的是第二个日志文件。为什么要分析它呢,因为它中途执行了一个操作是</span><span style="font-family:'Courier New';color:black;font-size:10pt;">DELETE</span><span style="font-family:宋体;color:black;font-size:10pt;">,因为我们要做的是恢复全部数据,也就是我们不希望去重做这个语句。所以在这里我们要想办法去绕开它。</span><span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:宋体;color:black;font-size:10pt;">我们先打开</span><span style="font-family:'Courier New';color:black;font-size:10pt;">002.txt</span><span style="font-family:宋体;color:black;font-size:10pt;">文件来分析一下。</span><span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:'Courier New';color:black;font-size:10pt;"># at 805</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">#090427 15:30:21 server id 1<span>&nbsp; </span>end_log_pos 875 <span></span>Query<span>&nbsp; </span>thread_id=1<span>&nbsp;&nbsp; </span>exec_time=0<span>&nbsp;&nbsp; </span>error_code=0</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">SET TIMESTAMP=1240817421/*!*/;</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">BEGIN</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">/*!*/;</span>



<span style="font-family:'Courier New';color:red;font-size:10pt;"># at 875</span>



<span style="font-family:'Courier New';color:red;font-size:10pt;">#090427 15:30:21 server id 1<span>&nbsp; </span>end_log_pos 981 <span></span>Query<span>&nbsp; </span>thread_id=1<span>&nbsp;&nbsp; </span>exec_time=0<span>&nbsp;&nbsp; </span>error_code=0</span>



<span style="font-family:'Courier New';color:red;font-size:10pt;">SET TIMESTAMP=1240817421/*!*/;</span>



<span style="font-family:'Courier New';color:red;font-size:10pt;">delete from test where id between 4 and 5</span>



<span style="font-family:'Courier New';color:red;font-size:10pt;">/*!*/;</span>



<span style="font-family:'Courier New';color:red;font-size:10pt;"># at 981</span>



<span style="font-family:'Courier New';color:red;font-size:10pt;">#090427 15:30:21 server id 1<span>&nbsp; </span>end_log_pos 1008 <span>&nbsp;&nbsp; </span>Xid = 15</span>



<span style="font-family:'Courier New';color:red;font-size:10pt;">COMMIT/*!*/;</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;"># at 1008</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">#090427 15:30:34 server id 1<span>&nbsp; </span>end_log_pos 1078 <span>&nbsp;&nbsp; </span>Query<span>&nbsp; </span>thread_id=1<span>&nbsp;&nbsp; </span>exec_time=0<span>&nbsp;&nbsp;&nbsp; </span>error_code=0</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">SET TIMESTAMP=1240817434/*!*/;</span>



<span style="font-family:宋体;color:black;font-size:10pt;">―――――――――――――――――――――――――――――――――――――</span><span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:宋体;color:black;font-size:10pt;">在这个文件中,我们可以看到</span><span style="font-family:'Courier New';color:black;font-size:10pt;">DELETE</span><span style="font-family:宋体;color:black;font-size:10pt;">的操作的起始位置是</span><span style="font-family:'Courier New';color:black;font-size:10pt;">875</span><span style="font-family:宋体;color:black;font-size:10pt;">,终止位置是</span><span style="font-family:'Courier New';color:black;font-size:10pt;">1008.</span><span style="font-family:宋体;color:black;font-size:10pt;">那么我们只要重做第二个日志文件的开头到</span><span style="font-family:'Courier New';color:black;font-size:10pt;">875</span><span style="font-family:宋体;color:black;font-size:10pt;">的操作,然后再从</span><span style="font-family:'Courier New';color:black;font-size:10pt;">1008</span><span style="font-family:宋体;color:black;font-size:10pt;">到末尾的操作,我们就可以把数据给恢复回来,而不会</span><span style="font-family:'Courier New';color:black;font-size:10pt;">DELETE</span><span style="font-family:宋体;color:black;font-size:10pt;">数据。所以执行两个命令:</span><span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">F:Program FilesMySQL_Datadatalog>mysqlbinlog liangck.000002 --stop-pos=875 | mysql -uroot -p</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">F:Program FilesMySQL_Datadatalog>mysqlbinlog liangck.000002 --start-pos=1008 | mysql -uroot -p mytest</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">OK,</span><span style="font-family:宋体;color:black;font-size:10pt;">现在第二个日志文件的数据了。</span><span style="font-family:'Courier New';color:black;font-size:10pt;"></span>

 为了避免昨时表所引的问题,官方并不推荐使用上面的方法进行恢复,官方推荐的两种方式如下:

shell> mysqlbinlog hostname-bin.000001 hostname-bin.000002 | mysql

另一个方法是:

shell> mysqlbinlog hostname-bin.000001 >  /tmp/statements.sql
shell> mysqlbinlog hostname-bin.000002 >> /tmp/statements.sql
shell> mysql -e "source /tmp/statements.sql"
<span style="font-family:宋体;color:black;font-size:10pt;">第三个日志文件也是同理,只要找到</span><span style="font-family:'Courier New';color:black;font-size:10pt;">DROP TABLE</span><span style="font-family:宋体;color:black;font-size:10pt;">的位置,就可以了。</span><span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">F:Program FilesMySQL_Datadatalog>mysqlbinlog liangck.000003 --stop-pos=574 | mysql -uroot –p</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:宋体;color:black;font-size:10pt;">现在我们再查一下数据看看</span><span style="font-family:'Courier New';color:black;font-size:10pt;">:</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">mysql> select * from test;</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">+----+------+-------+</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">| id | val<span>&nbsp; </span>| data<span>&nbsp; </span>|</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">+----+------+-------+</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">|<span>&nbsp; </span>1 |<span>&nbsp;&nbsp; </span>10 | liang |</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">|<span>&nbsp; </span>2 |<span>&nbsp;&nbsp; </span>20 | jia<span>&nbsp;&nbsp; </span>|</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">|<span>&nbsp; </span>3 |<span>&nbsp;&nbsp; </span>30 | hui<span>&nbsp;&nbsp; </span>|</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">|<span>&nbsp; </span>4 |<span>&nbsp;&nbsp; </span>40 | aaa<span>&nbsp;&nbsp; </span>|</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">|<span>&nbsp; </span>5 |<span>&nbsp;&nbsp; </span>50 | bbb<span>&nbsp;&nbsp; </span>|</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">|<span>&nbsp; </span>6 |<span>&nbsp;&nbsp; </span>60 | ccc<span>&nbsp;&nbsp; </span>|</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">|<span>&nbsp; </span>7 |<span>&nbsp;&nbsp; </span>70 | ddd<span>&nbsp;&nbsp; </span>|</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">|<span>&nbsp; </span>8 |<span>&nbsp;&nbsp; </span>80 | dddd<span>&nbsp; </span>|</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">|<span>&nbsp; </span>9 |<span>&nbsp;&nbsp; </span>90 | eeee<span>&nbsp; </span>|</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">+----+------+-------+</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;">9 rows in set (0.00 sec)</span>



<span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



<span style="font-family:宋体;color:black;font-size:10pt;">可以看到,全部数据都回来了。</span><span style="font-family:'Courier New';color:black;font-size:10pt;"></span>



binlog日志的清理:



清除指定的 binlog。



PURGE MASTER LOGS TO ‘mysql-bin.0010′;



该命令将清除 mysql-bin.0010 文件。



清除指定日期前的 binlog。



PURGE MASTER LOGS BEFORE ‘2010-08-01 00:00:00′;<br />

#清除2010年8月的binlog

在 contab 中设置:下面语句定期删除7天前的 binlog



0 1 * * *&nbsp;<wbr> `mysql -uroot -e ‘PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 7 DAY); </wbr>



&nbsp;mysql官方提供的mysqlbinlog的用法及恢复操作:



<a href="http://dev.mysql.com/doc/refman/5.1/zh/client-side-scripts.html#mysqlbinlog">http://dev.mysql.com/doc/refman/5.1/zh/client-side-scripts.html#mysqlbinlog</a>



<a href="http://dev.mysql.com/doc/refman/5.1/zh/database-administration.html#backup">http://dev.mysql.com/doc/refman/5.1/zh/database-administration.html#backup</a>



<strong>三、mysqlbinlog的相关调整参数</strong>

 

其实在my.cnf中有几个参数可以控制bin-log的,其中经常用到的有下面三个:

expire_logs_daysSQL_LOG_BINmax-binlog-size 、binlog-do-db、binlog-ignore-db

expire_logs_days为二进制日志自动删除的天数。默认值为0,表示没有自动删除

my.cnf中设置如下:

expire_logs_days = 10     设置自动删除10天前的日志文件

在运行时修改如下:
show binary logs;
show variables like ‘%log%’;
set global expire_logs_days = 10;


SQL_LOG_BIN
可以定义你此时的session的语句是否记到binlog中。关掉的话貌似可以提高1%的性能。

max-binlog-size与设置日志文件大小相关:

查看binlog大小设置 show variables like ‘max_%log_size’;
如果max_relay_log_size > 0,日志文件大小默认为:max_relay_log_size
否则如果max_relay_log_size = 0;则日志文件大小默认为 max_binlog_size
查看: select @@max_relay_log_size; || select @@max_binlog_size; 默认1G10737418241个,可以通过 select @@max_binlog_size; 查看,可以修改为256M左右,提高系统性能。
[mysqld]
expire-logs-days=2
max-binlog-size=268435456

&nbsp;binlog-do-db设置用于记录mysql二进制日志文件的数据库。如:设置binlog-do-db=db1,则除db1之外的库的操作都不会被记录。

binlog-ignore-db参数刚好与binlog-ignore-db参数的功能相反。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注