#!/bin/python
#coding =utf-8
Import MySQLdb
Import time
Import Sys
Import Mysqldb.cursors
# add character set parameters to prevent Chinese garbled characters
Print "---------TOP OS SLAVE--------"
conn = MySQLdb.connect (host= "XXXX", db= "MySQL", user= "user", passwd= "password", port=338
0,charset= "UTF8")
cursor = Conn.cursor ()
query = "SELECT distinct t.user_host,t.sql_text from Mysql.slow_log t where T.query_time>5 and T.start_time>=subdat E (Curdate (), Date_format (Curdate (), '%w ') +3) and T.start_time<subdate (Curdate (), Date_format (Curdate (), '%w ')-4) ORDER by T.query_time DESC limit 10 "
Cursor.execute (query)
R = Cursor.fetchall ()
For a in R:
User_host =a[0]
Sql_text =a[1]
print "\ n"
Print "%s\t%s"% (User_host,sql_text)
Conn.close ()
Outputs the query results as an array.
This article is from the "Dream to Reality" blog, please be sure to keep this source http://lookingdream.blog.51cto.com/5177800/1917129
Python read MySQL query result value standard output