!/usr/bin/python#-*- coding:utf-8 -*-
importsysimportdatetimefrom pymysqlreplication importBinLogStreamReaderfrom pymysqlreplication.row_event importDeleteRowsEvent, UpdateRowsEvent, WriteRowsEventfrom pymysqlreplication.event importRotateEvent,QueryEvent
reload(sys)
sys.setdefaultencoding(‘utf8‘)
conn_setting={"host": "127.0.0.1","port": 3306,"user": "xx","passwd": "xx"}
stream=BinLogStreamReader(
connection_settings=conn_setting,
server_id=9999,
log_file="mysql-bin.000009",
log_pos=4,
resume_stream=True,
blocking=True
)for binlogevent instream:ifisinstance(binlogevent, RotateEvent):
current_master_log_file=binlogevent.next_binlogprint "Next binlog file: %s" %(current_master_log_file)if isinstance(binlogevent, WriteRowsEvent) or isinstance(binlogevent, DeleteRowsEvent) or isinstance(binlogevent, UpdateRowsEvent) orisinstance(binlogevent, QueryEvent):if binlogevent.packet.server_id == 123:
current_datetime=datetime.datetime.fromtimestamp(binlogevent.packet.timestamp)print "数据写入时间: %s" %(current_datetime)
start_binlog_file=current_master_log_file
start_binlog_pos=binlogevent.packet.log_posprint "开始的binlog文件: %s" %(start_binlog_file)print "开始的binlog pos点: %s" %(start_binlog_pos)
exit(1)
输出结果如下:
Next binlog file: mysql-bin.000001数据写入时间:2020-05-11 11:56:09开始的binlog文件: mysql-bin.000001开始的binlog pos点:146147
我们自己动手来解析binlog看看输出的pos点是否准确:
可以看见是准确无误的。
总结:
线上一般会开启gtid或者使用其他高可用组件进行切换。python-mysql-replication是个好东西,建议同学们都学习使用一下。
巧用python-mysql-replication寻找pos点
标签:span 就是 resume stream passwd locking sql bsp writer
本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉
本文系统来源:https://www.cnblogs.com/gomysql/p/12867967.html