Get the App
SLTechnology News&Howtos  ›  Database  › 

How to extract the operation record of a specified table in MySQL binlog

Shulou Source: shulou.com Published: 2022-05-31 15:57:07 09月27日 Update

Editor to share with you how to extract the operation records of the specified table in MySQL binlog. I hope you will get something after reading this article. Let's discuss it together.

A simple Python script that requires an appropriate version of the mysqlbinlog tool installed locally.

Support for Python 2.7 +

Click (here) to collapse or open

# _ * _ coding:utf-8 _ * _

Import sys

Import os

Import io

Binlogfile = sys.argv [1]

Database_name = sys.argv [2]

Table_name = sys.argv [3]

Def format_binlog ():

Os.system ('mysqlbinlog-- base64-output=decode-rows-v' + binlogfile+' >'+ binlogfile+'.txt')

Def pickupbinlog ():

F = io.open (binlogfile+'.txt','r')

Fw = io.open (database_name+'_'+table_name+'.txt','a')

Priv_str =''

Priv_line =''

Goal_flag = 0

For row in f:

# process the first line

If row [0:3] ='# 'and priv_str! =' #':

If database_name in row and table_name in row:

Goal_flag = 1

Fw.write (priv_line)

Fw.write (row)

# handle the last line

If row [0:3]! ='# 'and priv_str =' #':

Goal_flag = 0

# handle target operations

If row [0:3] ='# 'and priv_str = =' # 'and goal_flag = = 1:

Fw.write (row)

Priv_str = row [0:3]

Priv_line = row

F.close ()

Fw.close ()

If _ _ name__ = ='_ _ main__':

# python2.7 pickupbinlog.py mysql-bin.001051 dbname tablename

# python3 pickupbinlog.py mysql-bin.001051 dbname tablename

Format_binlog ()

Pickupbinlog ()

After reading this article, I believe you have a certain understanding of "how to extract the operation records of the specified table in MySQL binlog". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for your reading!

Tags: Processing middle finger article appropriate done tools more versions goals knowledge scripts industry information information channels channels utf-8 support Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MySQL Huawei Docker Microsoft