首页 > 代码库 > mysqbinlog解析二进制日志测试随笔

mysqbinlog解析二进制日志测试随笔

在此之前需要了解二进制日志的格式:

    row  --行

    statement  --语句

    Mixed  --混合

1、当前的binlog_format = statement

mysql> show variables like ‘binlog_format‘;

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

| binlog_format | STATEMENT |

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

对库表进行插入数据操作:

insert into tb2 values (3,‘son‘)

查看当前格式下的二进制日志内容:

# mysqlbinlog /mydata/master.000001

[root@localhost mydata]# mysqlbinlog --start-position=851 --stop-position=957

[root@localhost mydata]# mysqlbinlog --base64-output=decode-rows  --start-position=851 --stop-position=957 /mydata/master.000001

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;

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

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

DELIMITER /*!*/;

# at 851

#161118  0:59:38 server id 1  end_log_pos 957 CRC32 0xd89c9cd3 Query thread_id=2 exec_time=0 error_code=0

use `test`/*!*/;

SET TIMESTAMP=1479401978/*!*/;

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

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

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

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

/*!\C utf8 *//*!*/;

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

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

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

insert into tb2 values (3,‘son‘)

/*!*/;

DELIMITER ;

# End of log file

ROLLBACK /* added by mysqlbinlog */;

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

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

[root@localhost mydata]#

2、binlog_format=row

mysql> show variables like ‘binlog_format‘;

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

| binlog_format | ROW   |

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

同样对表进行插入和删除的操作

mysql> insert into tb2 values(2,‘helen‘);

mysql> delete from tb2 where id = 1 ;

查看二进制日志:   直观的反馈就是你压根就看不到你刚才所执行过的语句,只有些许关键字如insert 、delete表达了你动作的性质 。

[root@localhost mydata]# mysqlbinlog --base64-output=decode-row --start-position=120  master.000001

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;

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

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

DELIMITER /*!*/;

# at 120

#161118  1:14:58 server id 1  end_log_pos 192 CRC32 0x8882b974 Query thread_id=1 exec_time=0 error_code=0

SET TIMESTAMP=1479402898/*!*/;

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

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

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

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

/*!\C utf8 *//*!*/;

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

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

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

BEGIN

/*!*/;

# at 192

#161118  1:14:58 server id 1  end_log_pos 241 CRC32 0xa120e7f3 Table_map: `test`.`tb2` mapped to number 71

# at 241

#161118  1:14:58 server id 1  end_log_pos 287 CRC32 0x6da9f0d9 Write_rows: table id 71 flags: STMT_END_F

# at 287

#161118  1:14:58 server id 1  end_log_pos 318 CRC32 0x952ced1d Xid = 16

COMMIT/*!*/;

# at 318

#161118  1:15:06 server id 1  end_log_pos 390 CRC32 0x37f19e7d Query thread_id=1 exec_time=0 error_code=0

SET TIMESTAMP=1479402906/*!*/;

BEGIN

/*!*/;

# at 390

#161118  1:15:06 server id 1  end_log_pos 439 CRC32 0x8f0bf21e Table_map: `test`.`tb2` mapped to number 71

# at 439

#161118  1:15:06 server id 1  end_log_pos 483 CRC32 0x9fe2e907 Write_rows: table id 71 flags: STMT_END_F

# at 483

#161118  1:15:06 server id 1  end_log_pos 514 CRC32 0xa7ed825e Xid = 17

COMMIT/*!*/;

# at 514

#161118  1:17:13 server id 1  end_log_pos 586 CRC32 0x3e2b7b53 Query thread_id=1 exec_time=0 error_code=0

SET TIMESTAMP=1479403033/*!*/;

BEGIN

/*!*/;

# at 586

#161118  1:17:13 server id 1  end_log_pos 635 CRC32 0xc5b403fd Table_map: `test`.`tb2` mapped to number 71

# at 635

#161118  1:17:13 server id 1  end_log_pos 679 CRC32 0xdc5e885f Delete_rows: table id 71 flags: STMT_END_F

# at 679

#161118  1:17:13 server id 1  end_log_pos 710 CRC32 0x2df182e5 Xid = 18

COMMIT/*!*/;

DELIMITER ;

# End of log file

ROLLBACK /* added by mysqlbinlog */;

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

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

[root@localhost mydata]#

3、binlog_format = MIXED

mysql> show variables like ‘binlog_format‘;

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

| Variable_name | Value |

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

| binlog_format | MIXED |

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

对test库中的表tb2做了如下动作:

mysql> insert into tb2 values(2,‘helen‘);

mysql> delete from tb2 where id = 1 ;

查看当前的二进制日志:

[root@localhost mydata]# mysqlbinlog --base64-output=decode-rows  --start-position=120 /mydata/master.000001

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;

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

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

DELIMITER /*!*/;

# at 120

#161118  1:21:27 server id 1  end_log_pos 199 CRC32 0x6e07aaac Query thread_id=1 exec_time=0 error_code=0

SET TIMESTAMP=1479403287/*!*/;

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

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

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

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

/*!\C utf8 *//*!*/;

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

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

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

BEGIN

/*!*/;

# at 199

#161118  1:21:27 server id 1  end_log_pos 306 CRC32 0xbc338f2d Query thread_id=1 exec_time=0 error_code=0

use `test`/*!*/;

SET TIMESTAMP=1479403287/*!*/;

insert into tb2 values(2,‘helen‘)

/*!*/;

# at 306

#161118  1:21:27 server id 1  end_log_pos 337 CRC32 0xe0c099a6 Xid = 12

COMMIT/*!*/;

# at 337

#161118  1:21:58 server id 1  end_log_pos 416 CRC32 0x21caca8f Query thread_id=1 exec_time=0 error_code=0

SET TIMESTAMP=1479403318/*!*/;

BEGIN

/*!*/;

# at 416

#161118  1:21:58 server id 1  end_log_pos 518 CRC32 0x08994991 Query thread_id=1 exec_time=0 error_code=0

SET TIMESTAMP=1479403318/*!*/;

delete from tb2 where id = 1

/*!*/;

# at 518

#161118  1:21:58 server id 1  end_log_pos 549 CRC32 0xb0068443 Xid = 13

COMMIT/*!*/;

DELIMITER ;

# End of log file

ROLLBACK /* added by mysqlbinlog */;

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

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

[root@localhost mydata]#

总结:

binlog_format = MIXED     ,默认为STATEMENT

expire_logs_days = 7 ,默认为0

max_binlog_size = 100M  ,默认是1G

binlog_cache_size = 32M   ,默认值


本文出自 “不进则退” 博客,请务必保留此出处http://laozhu.blog.51cto.com/755494/1874337

mysqbinlog解析二进制日志测试随笔