python 正则解析mysql慢日志

Posted _雪辉_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 正则解析mysql慢日志相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python3
import re
slow_message = '''
# Time: 2021-04-23T11:45:34.547941+08:00
# User@Host: sysbench[sysbench] @  [127.0.0.1]  Id:   215
# Query_time: 0.000138  Lock_time: 0.000039 Rows_sent: 0  Rows_examined: 0
SET timestamp=1619149534;
INSERT INTO sbtest1 (id, k, c, pad) VALUES (49811, 49912, '10220485634-91999725679-19196059245-27016586998-15755374217-01375486173-63134319850-09341020024-22612796483-62654126985', '76715857161-08232266466-02982013956-69920229100-95612205979');
'''
slowlog_patern = '#\\s+Time:\\s+(?P<Time>.+)\\n' \\
              '#\\s+User@Host:\\s+(?P<user>\\w+)\\[\\w+\\]\\s+@\\s+' \\
              '(?P<proxyHost>(\\w|\\[|\\]|\\.|\\s)+)\\s+Id:\\s+(?P<threadID>\\d+)\\n' \\
              '#\\s+Query_time:\\s+(?P<queryTime>\\w+\\.?\\w+)\\s+' \\
              'Lock_time:\\s+(?P<lockTime>\\w+\\.?\\w+)\\s+Rows_sent:\\s+(?P<rowsSent>\\d+)\\s+' \\
              'Rows_examined:\\s+(?P<rowsExamined>\\d+)\\n' \\
              '(use\\s+(?P<use>.*);\\n)?' \\
              'SET\\s+timestamp=(?P<sqlTimestamp>\\d+);\\n' \\
              '(?P<sqlText>(.|\\n)*)'
slow_log_regex = re.compile(slowlog_patern)
match = slow_log_regex.search(slow_message)
if match:
    sql_dict = match.groupdict()
    print(sql_dict)
[root@zijie ~]# python3 analyze_slowlog.py
{'Time': '2021-04-23T11:45:34.547941+08:00', 'user': 'sysbench', 'proxyHost': '[127.0.0.1] ', 'threadID': '215', 'queryTime': '0.000138', 'lockTime': '0.000039', 'rowsSent': '0', 'rowsExamined': '0', 'use': None, 'sqlTimestamp': '1619149534', 'sqlText': "INSERT INTO sbtest1 (id, k, c, pad) VALUES (49811, 49912, '10220485634-91999725679-19196059245-27016586998-15755374217-01375486173-63134319850-09341020024-22612796483-62654126985', '76715857161-08232266466-02982013956-69920229100-95612205979');\\n"}

以上是关于python 正则解析mysql慢日志的主要内容,如果未能解决你的问题,请参考以下文章

Logstash分析MySQL慢查询日志

mysql慢查询解析-linux命令

MySQL慢日志全解析

MySQL慢日志全解析

logstash解析mysql慢日志

MySQL慢日志查询全解析:从参数配置到分析工具