Mysql, Maria DB

[Python] mysql lock monitoring. (실행 중인 스레드 모니터링)

Pydole 2018. 5. 5. 22:01

파이썬 pymysql 모듈을 이용하여 실행 중인 스레드 모니터링을 할 수 있다.

 

 

- mysql 명령어 : SHOW PROCESSLIST

 

(DOCS : https://dev.mysql.com/doc/refman/5.5/en/show-processlist.html)

 

 

import pymysql

strFormat = '%-10s\t%-10s\t%-20s\t%-20s\t%-30s\t%-10s\t%-30s\t%-30s\t%-10s\n'
strOut = strFormat % ('Id', 'User', 'Host', 'db', 'Command', 'Time', 'State','Info','Progress')
strOut += '----------\t----------\t---------------------\t---------------------\t------------------------------\t' \
'----------\t------------------------------\t------------------------------\t----------\n'

conn = pymysql.connect(host='', user='', password=''
,db='', charset='utf8')

c = conn.cursor()
sql = 'SHOW PROCESSLIST'
c.execute(sql)
rows = c.fetchall()

for i in rows:
     strOut += strFormat % (i[0],i[1],i[2],i[3],i[4],i[5],i[6],i[7],i[8])
     print(strOut)

c.close()
conn.close()

 

Id        	User      	Host                	db                  	Command                       	Time      	State                         	Info                          	Progress  
----------	----------	---------------------	---------------------	------------------------------	----------	------------------------------	------------------------------	----------
1         	system user	                    	None                	Daemon                        	None      	InnoDB purge worker           	None                          	0.0       
2         	system user	                    	None                	Daemon                        	None      	InnoDB purge coordinator      	None                          	0.0       
3         	system user	                    	None                	Daemon                        	None      	InnoDB purge worker           	None                          	0.0       
4         	system user	                    	None                	Daemon                        	None      	InnoDB purge worker           	None                          	0.0       
5         	system user	                    	None                	Daemon                        	None      	InnoDB shutdown handler       	None                          	0.0       
10        	root      	localhost:47084     	information_schema  	Sleep                         	583       	                              	None                          	0.0       
24        	root      	localhost:47924     	mysql               	Query                         	0         	init                          	SHOW PROCESSLIST              	0.0