您好,登錄后才能下訂單哦!
小編給大家分享一下如何提取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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。