共计 1026 个字符,预计需要花费 3 分钟才能阅读完成。
丸趣 TV 小编给大家分享一下如何提取 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 中指定表的操作记录”有了一定的了解,如果想了解更多相关知识,欢迎关注丸趣 TV 行业资讯频道,感谢各位的阅读!