Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python queries MySQL slow logs

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1.1.The installation depends on pymysql

Cd / usr/local/src/

Wget https://files.pythonhosted.org/packages/44/39/6bcb83cae0095a31b6be4511707fdf2009d3e29903a55a0494d3a9a2fac0/PyMySQL-0.8.1.tar.gz

Tar xf PyMySQL-0.8.1.tar.gz

Cd PyMySQL-0.8.1

Python setup.py install

Check to see if pymysql is installed successfully:

Python

Import pymysql

Pymysql.VERSION

(0,8,1, None)

Exit ()

The installation is complete when you see the version information!

1.2.The installation depends on MysqlDb

Cd / usr/local/src/

Wget https://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz/download

Tar xf download

Cd MySQL-python-1.2.3

Python setup.py build

Python setup.py install

1.3.The Python script

More / opt/shell/templates/mysql_slowlog.py

#! / usr/bin/python

Import sys

Import MySQLdb

Import time

Import datetime

Import MySQLdb.cursors

Mysql_ip = sys.argv [1]

Mysql_user = sys.argv [2]

Mysql_password = sys.argv [3]

Query_time = "00:0" + sys.argv [4] + ": 00"

Start_time = sys.argv [5] + "00:00:00"

End_time = sys.argv [5] + "23:59:59"

Db= MySQLdb.connect (host=mysql_ip, user=mysql_user, passwd=mysql_password, db= "mysql", port=3306)

Cur = db.cursor ()

Query = "select t.startcalendar timestation t.querytimeclamination .userroomhostrecovert.rowswatches sentencet.rowswatches examinedPowert.sqlroomtext from mysql.slow_log t where t.start_time > ='% s' and t.startcalendar timekeeper timestamps% s'"% (start_time,end_time,query_time)

Cur.execute (query)

R = cur.fetchall ()

For an in r:

Start_time = a [0]

Query_time = a [1]

User_host = a [2]

Rows_sent = a [3]

Rows_examinetd = a [4]

Sql_text = a [5]

Print "\ n"

Print "% s\ t% d\ t% d\ t% s"% (start_time,query_time,user_host,rows_sent,rows_examinetd,sql_text)

Db.close ()

1.3.1. Execute the Python script: the time it takes to query the IP user's password for more than 2 seconds

Python / opt/shell/templates/mysql_slowlog.py 10.0.0.12 root 7$ JlI@cgZc5 $rW7N 2 2019-12-119

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report