以下脚本只对binlog进行了初步的解析,使用者可以根据解析的结果,在ReadBinlog.app部分进行二次开发
#!/usr/bin/env python3
# _*_ coding:utf8 _*_
from threading import Thread
from queue import Queue
from time import strptime,mktime,localtime,strftime
from pymysqlreplication import BinLogStreamReader #pip3 install mysql-replication==0.21
from pymysqlreplication.row_event import DeleteRowsEvent,UpdateRowsEvent,WriteRowsEvent
##从主库读取binlog,然后将event解析,将解析结果放队列中
class BinlogStream(Thread):
def __init__(self,
queue_logs, #队列
master_host,master_port,master_user,master_pswd, #主库配置
start_time, stop_time, #开始结束时间,格式:YYYY-mm-dd HH:MM:SS
log_file,log_pos=0, #开始binlog文件位置
only_schemas = None, only_tables = None #只监听指定的库和表,格式:['db1','db2']
):
self._Queue = queue_logs
self.connect = {'host': master_host, 'port': master_port, 'user': master_user,'passwd': master_pswd}
self.log_file,self.log_pos = log_file,log_pos
self.start_time = int(mktime(strptime(start_time, "%Y-%m-%d %H:%M:%S")))
self.stop_time = int(mktime(strptime(stop_time, "%Y-%m-%d %H:%M:%S")