MySQL binlog log analysis tool written in Python

Source: Internet
Author: User

Because the addition, deletion, and modification of the MySQL database suddenly increase, the operations on the tables that need to be queried are very frequent. I wrote a small tool to analyze the bin-log and find the tables for addition, deletion, modification, and query, and sorted in descending order according to the number of operations, the following is the code:

# For mysql5.5 binlog
Import OS, sys
# Python binlog. py binglog-0001 '2017-07-01 00:00:00 ''2017-07-02 00:00:00'
Def log_w (type, text ):
Logfile = "cmds.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 ):
Update = ''. join (a. split () [: 2])
Mylist. append (update)
If ('insert into' in ):
Update = ''. join (a. split () [: 3]). replace (" ","")
Mylist. append (update)
If ('delete from' in ):
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]

The execution result is as follows:

How to safely delete binlog logs under MySQL

MySQL -- binlog log data recovery

How does MySQL Delete binlog and restore Data?

Introduction and Analysis of Three MySQL binlog formats

MySQL uses binlog Incremental backup + restore instance

MySQL deletes binlog logs and restores data.

Implementation and performance of MySQL binlog pre-allocation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.