Python葵花宝典-mysql日志分析

本文介绍了一个Python脚本,用于解析MySQL慢查询日志并将其数据插入到一个专门设计的数据库表中。该表包含IP地址、查询时间、锁时间、发送行数、检查行数、SQL执行时间及慢查询SQL语句等字段,通过建立索引优化查询效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/usr/bin/python
# -*- coding:utf-8 -*-
 
import re
import sys
import time
import MySQLdb
 
def create_table():
    db=MySQLdb.connect(host="localhost", user="root", passwd="mysql", db="slow_log")
    cursor=db.cursor()
    cursor.execute("DROP TABLE IF EXISTS `mysql_slow_log`;")
    sql="""CREATE TABLE `mysql_slow_log` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `IP_HOST` varchar(150) NOT NULL,
      `Query_time` float(11,6) NOT NULL,
      `Lock_time` char(11) NOT NULL,
      `Rows_sent` int(11) NOT NULL,
      `Rows_examined` int(11) NOT NULL,
      `sql_time` datetime NOT NULL,
      `slow_sql` LongText NOT NULL,
      PRIMARY KEY (`id`),
      KEY `Query_time` (`Query_time`),
      KEY `Rows_examined` (`Rows_examined`),
      KEY `sql_time` (`sql_time`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"""
    cursor.execute(sql)
    db.close()
def insert_table():
    log_file=open('mysql-slow.log')
    db=MySQLdb.connect("localhost","root","mysql","slow_log")
    cursor=db.cursor()
    content=''
    for line in log_file.readlines():
        line=line.strip('\n')
        content=content+line
    re_mysql = re.findall('#.*?User@Host: (.*?)#.*?Query_time: (.*?) Lock_time: (.*?) Rows_sent: (.*?) Rows_examined: (.*?)SET.*?timestamp=(.*?);(.*?);', content, re.I);
    for record in re_mysql:
           IP_HOST=record[0].strip()
           Query_time=record[1].strip()
           Lock_time=record[2].strip()
           Rows_sent=record[3].strip()
           Rows_examined=record[4].strip()
           timestamp=int(record[5])
           timeArray=time.localtime(timestamp)
           sql_time=time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
           slow_sql=record[6].strip()
           set_charset="set names utf8"
           sql = """INSERT INTO mysql_slow_log(IP_HOST,Query_time,Lock_time,Rows_sent,Rows_examined,sql_time,slow_sql)
                VALUES ('"""+IP_HOST+"""',"""+Query_time+""",'"""+Lock_time+"""',"""+Rows_sent+""","""+Rows_examined+""",'"""+sql_time+"""',\""""+slow_sql+"""\;\")""";
           try:
               cursor.execute(set_charset)
               cursor.execute(sql)
               print sql
               db.commit()
           except:
               db.rollback()
    log_file.close()
    db.close()
def main():
    create_table()
    insert_table()
main()

 

转载于:https://www.cnblogs.com/overarching/p/9956655.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值