MySQL method of getting slow log information by time

  • 2020-11-03 22:37:51
  • OfStack

1 case: database exception handled today. The number of connections increased sharply.
I want to analyze 1 slow log, but 1 slow log is several G, and it is a short log format, it is relatively difficult to find that time point. So I wrote a script to extract point logs from slow logs by time. Script:

https://github.com/wubx/mysql-binlog-statistic/blob/master/bin/cutlogbytime

Usage:

cutlogbytime
# Used to intercept from slow logs 1 Time - to - time log for easy analysis
./cutlogbytime /path/slowlogfile starttime endtime  > dstfile.log

Time needs to be timestamped


mysql> select unix_timestamp('2013-04-05');
+------------------------------+
| unix_timestamp('2013-04-05') |
+------------------------------+
| 1365091200 |
+------------------------------+
1 row in set (0.00 sec)

mysql> select unix_timestamp('2013-04-06');
+------------------------------+
| unix_timestamp('2013-04-06') |
+------------------------------+
| 1365177600 |
+------------------------------+
1 row in set (0.00 sec)


./cutlogbytime /path/slowlogfile 1365091200 1365177600 > 20130405_slow.log

This will make the log files 1 point smaller and easier to analyze. And you can take the log at a certain point in a day based on the day file.


Related articles: