小编给大家分享一下如何提取MySQL binlog中指定表的操作记录,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
一段简单的Python脚本,需要本地装了合适版本的mysqlbinlog工具。
支持Python 2.7+
点击(此处)折叠或打开
#_*_ 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:
# 处理首行
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)
# 处理末行
if row[0:3] != '###' and priv_str == '###':
goal_flag = 0
# 处理目标操作
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()
看完了这篇文章,相信你对“如何提取MySQL binlog中指定表的操作记录”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。