共計 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 行業資訊頻道,感謝各位的閱讀!