Test: python 3.5, mysql 5.7
mysql 参数:binlog_rows_query_log_events = on ;binlog_format = row
# coding:utf-8 import os 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 in binlog_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 = False for line in binlog_sql: lower_line = line.lower() # 记录含有delete的原生sql if "delete" in lower_line and lower_line.count("#") == 1 and "server id" not in lower_line: new_sql_flag =True del_sql = Line elif " # AT " in lower_line: new_sql_flag = False elif new_sql_flag: # native sql multiple rows, the plurality of rows are combined del_sql = " {} {} " .format (del_sql, Line) IF line.find ( " Table_map: " )> 0: # Get timestamp start_index. 1 = end_index = line.index ( " Server ID ", Start_index) CURRENT_TIME = Line [start_index: end_index] .strip () # get the library name, table name tb_start_index = line.index ( " Table_map " , start_index) + 11 tb_end_index = line.index ( " mapped " , tb_start_index) table_info = Line [tb_start_index: tb_end_index]. .strip () the replace ( " ` " , " " ) # records delete information about a particular table to a file 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) # met to Table_map keywords will del_sql empty, re-record del_sql = ""
Test Results:
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