mysql binlog 过滤表_python 分析mysql binlog 并记录特定表的delete记录

本文介绍了如何使用Python解析MySQL的binlog,并过滤出特定表的DELETE记录。通过设置binlog相关参数,然后使用pythonbinlog模块读取binlog文件,查找包含DELETE关键字的SQL语句,并结合Table_map信息,将特定表的DELETE操作时间及详情写入文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

测试:python 3.5 、mysql 5.7

mysql 参数:binlog_rows_query_log_events =  on ;binlog_format = row

#coding:utf-8

importos

del_sql_file= r"/export/bak/del_file.txt"binlog_scr= r"ls /export/data/mysql/data/mysql-bin.0*"binlog_list=os.popen(binlog_scr).readlines()#循环读取所有binlog

for log_line inbinlog_list:if len(log_line) >0:

binlog_file=log_line.strip()print(binlog_file)#解析binlog

comn = r"/export/servers/mysql/bin/mysqlbinlog -vvv {}".format(binlog_file)

binlog_sql=os.popen(comn).readlines()

del_sql= ""new_sql_flag=Falsefor line inbinlog_sql:

lower_line=line.lower()#记录含有delete的原生sql

if "delete" in lower_line and lower_line.count("#") == 1 and "server id" not inlower_line:

new_sql_flag=True

del_sql=lineelif "# at" inlower_line:

new_sql_flag=Falseelifnew_sql_flag:#原生sql有多行时,将多行进行合并

del_sql = "{} {}".format(del_sql, line)if line.find("Table_map:") >0:#获取时间戳

start_index = 1end_index= line.index("server id", start_index)

current_time=line[start_index:end_index].strip()#获取库名、表名

tb_start_index = line.index("Table_map", start_index) + 11tb_end_index= line.index("mapped", tb_start_index)

table_info= line[tb_start_index:tb_end_index].strip().replace("`", "")#将特定表的delete 信息记录到文件中

if "rt_out_orders_m" in table_info and len(del_sql) >0:

with open(del_sql_file,"a+", encoding="utf-8") as f:

f.write("{} {}\n".format(current_time, table_info))

f.write(del_sql)#遇到 Table_map 关键字则将del_sql清空,重新记录

del_sql = ""

测试结果:

200320 23:01:02 ob_task.rt_out_orders_m

# DELETE FROM ob_task.rt_out_orders_m WHERE RECEIPT_NO = 'EBS4418047049205' limit 10

200320 23:01:02 ob_task.rt_out_orders_m

# DELETE FROM ob_task.rt_out_orders_m WHERE RECEIPT_NO = 'EBS4418047040735' limit 10

200320 23:01:02 ob_task.rt_out_orders_m

# DELETE FROM ob_task.rt_out_orders_m WHERE RECEIPT_NO = 'EBS4418047051006' limit 10

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值