Python寫的分析MySQL binlog日誌工具

來源:互聯網
上載者:User

因為MySQL資料庫增刪改突然暴增,需要查詢是那些表的操作特別頻繁,寫了一個用來分析bin-log的小工具,找出增刪改查的表,並按照操作次數降序排列,以下是代碼:

#for mysql5.5 binlog
import os,sys
#python binlog.py binglog-0001 '2013-07-01 00:00:00' '2013-07-02 00:00:00'
def log_w(type,text):
    logfile = "%s.txt" % (type,text)
    #now = time.strftime("%Y-%m-%d %H:%M:%S")
    tt = str(text) + "\n"
    f = open(logfile,'a+')
    f.write(tt)
    f.close()
logname = sys.argv[1]
start_time = sys.argv[2]
end_time = sys.argv[3]
comn = "/usr/bin/mysqlbinlog --start-datetime='%s' --stop-datetime='%s' %s" % (start_time,end_time,logname)
aa=os.popen(comn).readlines()
mylist=[]
for a in aa:
    if ('UPDATE' in a):
            update = ' '.join(a.split()[:2])
            mylist.append(update)
    if ('INSERT INTO' in a):
            update = ' '.join(a.split()[:3]).replace("INTO ","")
            mylist.append(update)
    if ('DELETE from' in a):
            update = ' '.join(a.split()[:3]).replace("from ","")
            mylist.append(update)
mylist.sort()
bb = list(set(mylist))
bb.sort()
cc = []
for item in bb:
        cc.append([mylist.count(item),(item)])
cc.sort()
cc.reverse()
for i in cc:
        print str(i[0])+'\t'+i[1]

執行結果如下:

如何安全刪除MySQL下的binlog日誌

MySQL--binlog日誌恢複資料

MySQL刪除binlog日誌及日誌恢複資料的方法

MySQL binlog三種格式介紹及分析

MySQL 利用binlog增量備份+還原執行個體

MySQL刪除binlog日誌及日誌恢複資料

MySQL binlog預分配的實現和效能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.