系統(tǒng)運(yùn)維
1.1、安裝依賴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
查看pymysql是否安裝成功:
python

import pymysql
pymysql.VERSION
(0, 8, 1, None)
exit()
看到版本信息即為安裝完成!
1.2、安裝依賴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、Python腳本
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.start_time,t.query_time,t.user_host,t.rows_sent,t.rows_examined,t.sql_text from mysql.slow_log t where t.start_time>=\'%s\' and t.start_time<\'%s\' and t.query_time >=\'%s\'" %(start_time,end_time,query_time)
cur.execute(query)
r = cur.fetchall()
for a 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%s\\t%s\\t%d\\t%d\\t%s" %(start_time,query_time,user_host,rows_sent,rows_examinetd,sql_text)
db.close()
1.3.1、執(zhí)行Python腳本:IP 用戶 密碼 超過2秒 查詢的時間
python /opt/shell/templates/mysql_slowlog.py 10.0.0.12 root 7$JlI@cgZc5$rW7N 2 2019-12-119
當(dāng)前文章:Python查詢MySQL慢日志
文章來源:http://chinadenli.net/article2/cjspic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、網(wǎng)站建設(shè)、網(wǎng)站排名、標(biāo)簽優(yōu)化、微信小程序、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)