MySQL binlog的格式解析

我搜集到了一些资料,对理解代码比较有帮助。

在头文件中binlog_event.h中,有描述

class Log_event_header

class Log_event_footer

参见【Mysteries of the Binary Log Presentation.pdf】

代码见【mysql-5.7.6-m16_src.zip】

MySQL binlog头4个字节:BINLOG_HEADER = b'xfex62x69x6e'

然后我们就可以一个Event,一个Event的读取了(格式基本就是一段一段的了。)

用Python解析Event header

struct.unpack("=IBIIIH", header), 得到的分别是timestamp, typecode, server_id, event_len, next_pos, flasg

查看当前binlog的设置

show variables like '%log%';

set global binlog_format='row'   ; 'statement'  or   'mixed'

binlog格式解析相关的文章和代码:

http://www.tuicool.com/articles/6RvUnqV

http://www.oschina.net/code/snippet_915111_16360

http://www.xcoder.cn/html/Database/mysql/2013/0526/9191.html

http://blog.csdn.net/wyzxg/article/details/7412777

【解析binlog格式,网上能找到有用的文章并不是很多,代码就更少了,我这份代码可以供大家参考。】

https://github.com/healerkx/PySQLKits/tree/master/scripts/mysqlbinlog

细节

1. Decimal在binlog中的的存储格式:

我写到field_descriptor.py里面的 decimal_descriptor, parse()函数, 时间久了,以后我再更新它的解析细节描述。

更多看我Rust的实现版本吧,相对于Python的版本fix了一些问题,占用资源肯定又少了。

https://github.com/healerkx/mysqlbinlog-rs

原文地址:https://www.cnblogs.com/healerkx/p/4692209.html