Mysql sql slow query monitoring script code example

Mysql sql slow query monitoring script code example

1. Modify my.cnf

#The overall effect is that both the table and the log file are written when the global is turned on, but for general_log only the table is written, and for slow_query_log, both the table and the log file are recorded.
general_log=1#Open the MySQL SQL execution log slow_query_log=1#Open the MySQL slow SQL log #After setting, it will affect general_log and slow_query_log,
log_output=table,File#Log output will be written to the table and the log file. In order to facilitate the program to count, it is best to write to the table#General_log_file is not configured here, so general_log will only write to the table#In mysql5.1.29 and above, set the following to open mysql to record the executed sql in the file#general_log_file=/log/general.log

#5.1.29 and before:
#log=/var/lib/mysql/sql_row.log
long_query_time=1#Set MySQL's slow query to a query that takes more than 1 second slow_query_log_file=/log/slow.log

2. Modify the format of MySQL's log table (in the MySQL library)

#By default, general_log is in csv format. Changing it to MyISAM format will increase query efficiency.

set global general_log = off;

alter table general_log engine = MyISAM;

set global general_log = on;

#The default slow_query_log is in csv format. Changing it to MyISAM format will increase query efficiency a lot

set global slow_query_log = off; equal to 0 has the same effect

alter table slow_log engine = MyISAM;

set global slow_query_log = on; equal to 1 has the same effect

3. Because MySQL's log tables: general_log and slow_query_log are not allowed to be modified, it is necessary to create a new table that is easy to delete and modify (this log table is too large and data from n days ago needs to be cleaned up regularly)

Create the slow_log_dba table

CREATE TABLE `slow_log_dba` (
 `start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 `user_host` mediumtext NOT NULL,
 `query_time` time NOT NULL,
 `lock_time` time NOT NULL,
 `rows_sent` int(11) NOT NULL,
 `rows_examined` int(11) NOT NULL,
 `db` varchar(512) NOT NULL,
 `last_insert_id` int(11) NOT NULL,
 `insert_id` int(11) NOT NULL,
 `server_id` int(10) unsigned NOT NULL,
 `sql_text` mediumtext NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Slow log for dba';

Create the general_log_dba table

CREATE TABLE `general_log_dba` (
 `event_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 `user_host` mediumtext NOT NULL,
 `thread_id` int(11) NOT NULL,
 `server_id` int(10) unsigned NOT NULL,
 `command_type` varchar(64) NOT NULL,
 `argument` mediumtext NOT NULL,
 KEY `user_host` (`user_host`(200)),
 KEY `event_time` (`event_time`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='general log for dba op';

4. Because the program ultimately uses the general_log_dba and slow_log_dba tables, it is necessary to regularly copy the data of general_log and slow_query_log to general_log_dba and slow_log_dba

Because the report is generated once a day, this action only needs to be performed once a day.

#The script saves 10 days of data and copies the data of general_log and slow_query_log to general_log_dba and slow_log_dba every day

#Run mysqllogtable.sh once a day as a scheduled task

#!/bin/sh

NDaysAgo=$(date -d '-10 days' "+%F %H:%M:%S")

/usr/local/mysql/bin/mysql -uXXXX -p'xxxxxxxx' -D'mysql' -e "insert general_log_dba select * from general_log;

truncate general_log;

delete from general_log_dba where event_time < \"$NDaysAgo\";

insert slow_log_dba select * from slow_log;

truncate slow_log;

delete from slow_log_dba where start_time < \"$NDaysAgo\""

5. Write a python script to count the daily SQL operations and slow queries of MySQL every day (some of the scripts are abstract methods, please handle them at your own discretion)

Script to count MySQL daily execution records

# -*- coding: utf-8 -*-
__author__ = 'river'
import MySQLdb as mysql
import re
from datetime import datetime, timedelta
import smtplib
from email.mime.text import MIMEText
def sendHtmlMail(mailcontent,myip):
  try:
    yestoday=(datetime.now()-timedelta(days=1)).strftime("%Y-%m-%d")
    sender = '[email protected]'
    receiver = ['[email protected]']
    subject = myip+'mysql operation report '+yestoday
    smtpserver = 'smtp.exmail.xx.com'
    username = '[email protected]'
    password = 'xxxxx'
    msg = MIMEText(mailcontent,'html','utf-8')#'Hello','text','utf-8'
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = '[email protected]'
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver)
    smtp.login(username, password)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()
  except Exception, e:
    print e,'send mail error'
if __name__ == '__main__':
  result=None
  htmlfile='mysqlLogMon.html'
  myiplist=['192.168.10.10','192.168.10.19']
  yestoday=(datetime.now()-timedelta(days=1)).strftime("%Y-%m-%d 00:00:00")
  today = datetime.now().strftime("%Y-%m-%d 00:00:00")
  for myip in myiplist:
    sql="select user_host,argument from general_log_dba where event_time >='%s' and event_time <='%s'" %(yestoday,today)
    try:
      dbcon = mysql.connect(host=myip, user='xxxxx', passwd='xxxxx', db='mysql', port=3306, charset='utf8')
      cur = dbcon.cursor()
      print "step 1,"+myip+','+datetime.now().strftime("%Y-%m-%d %H:%M:%S")
      cur.execute(sql)
      result = cur.fetchall()
      cur.close()
      dbcon.close()
    except Exception, e:
      print e,'conn mysql error'
    user_host_set=set()
    print "step 2,"+myip+','+datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    allhash={}
    if result:
      for user_host,argument in result:
        argument_delcom=re.compile(r'(\/\*(\s|.)*?\*\/)').sub("",argument).strip().replace(u"\x00",'').lower()
        if re.compile(r'^access.*').match(argument_delcom) or re.compile(r'^.*@.*on.*').match(argument_delcom) or re.compile(r'^grant.*').match(argument_delcom):
          tmpargument = argument_delcom.strip()
        else:
          tmpargument=argument_delcom.split(' ')[0].strip()
          if len(tmpargument)>30:
            #Some sql is u'select\n\t\t\t\t\tcount(m.enquirymainid)', you can use print repr(tmpargument)
            tmpargument=argument_delcom.split('\n')[0].strip()
        #If it is all comments, then this item will not be counted if not tmpargument or tmpargument.strip()=='' or tmpargument.strip()==' ':
          continue
        if allhash.has_key(user_host):
          allhash[user_host][tmpargument]=allhash[user_host].get(tmpargument,0)+1
        else:
          allhash[user_host]={tmpargument:1}
      print "step 3,"+myip+','+datetime.now().strftime("%Y-%m-%d %H:%M:%S")
      headhtml='''
      
      '''
      print "step 4,"+myip+','+datetime.now().strftime("%Y-%m-%d %H:%M:%S")
      with open(htmlfile,'w') as htmlfileobj:
        htmlfileobj.write(headhtml)
        htmlfileobj.flush()
      print "step 5,"+myip+','+datetime.now().strftime("%Y-%m-%d %H:%M:%S")
      with open(htmlfile,'a') as htmlfileobj:
        for hostkey in allhash.keys():
          listtmp=sorted(allhash[hostkey].iteritems(),key=lambda labkey:labkey[1],reverse=True)
          rowspan = len(allhash[hostkey])
          #htmlfileobj.write()
          tmpline = '' % (rowspan, hostkey.encode ('utf-8'))
          htmlfileobj.write(tmpline)
          countn=0
          for runsql,count in listtmp:
            if countn==0:
              tmpline='' %(runsql.encode('utf-8'),count)
            else:
              tmpline='' %(runsql.encode('utf-8'),count)
            countn+=1
            htmlfileobj.write(tmpline)
        tmpline='''
The number of times the user executed SQL was %s %s %s
%s %s
'''
        htmlfileobj.write(tmpline)
      with open(htmlfile,'r') as htmlfileobj:
        mailcontent = htmlfileobj.read()
      sendHtmlMail(mailcontent,myip)
    else:
      print 'sql result is None, exit ing'
    print "step 6,"+myip+','+datetime.now().strftime("%Y-%m-%d %H:%M:%S")

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • MySQL slow query pitfalls
  • MYSQL slow query and log example explanation
  • The role and opening of MySQL slow query log
  • MYSQL slow query and log settings and testing
  • Enabling and configuring MySQL slow query log
  • Example of MySQL slow query
  • How to locate MySQL slow queries
  • MySQL slow query method and example
  • Detailed explanation of the reason why the MySQL 5.7 slow query log time is 8 hours behind the system time
  • Mysql slow query optimization method and optimization principle
  • How to optimize MySQL performance through MySQL slow query
  • Solve the problem of MySQL Threads_running surge and slow query

<<:  Analyze the role of rel="nofollow" in HTML and the use of rel attribute

>>:  Detailed explanation of docker-machine usage

Recommend

Mariadb remote login configuration and problem solving

Preface: The installation process will not be des...

Detailed tutorial on installing MYSQL under WINDOWS

1. Download the installation package -Choose the ...

Summary of MySQL5 green version installation under Windows (recommended)

1 Download MySQL Download address: http://downloa...

Detailed explanation of webpage screenshot function in Vue

Recently, there is a requirement for uploading pi...

Solution to Docker disk space cleaning

Some time ago, I encountered the problem that the...

Detailed graphic explanation of sqlmap injection

Table of contents 1. We found that this website m...

Use non-root users to execute script operations in docker containers

After the application is containerized, when the ...

How to design and create adaptive web pages

With the popularization of 3G, more and more peop...

Vue implements form data validation example code

Add rules to the el-form form: Define rules in da...

Detailed explanation of basic operation commands for Linux network settings

Table of contents View network configuration View...

A brief discussion on several specifications of JS front-end modularization

Table of contents Preface The value of front-end ...

Mysql get table comment field operation

I won't say much nonsense, let's just loo...

Installation and configuration tutorial of MySQL 8.0.16 under Win10

1. Unzip MySQL 8.0.16 The dada folder and my.ini ...