最近在弄zabbix监控MySQL事宜,发现shell脚本无法解决字符串转换为整数操作,于是想到了Python,这里就用Python3环境
首先安装pymysql
pip install pymysql
1 #!/usr/bin/env python 2 # -*- coding: utf8 -*- 3 import pymysql 4 mysql_connect = pymysql.connect(host='192.168.3.5',user='root',passwd='123456',port=3306,db='mysql',charset = 'utf8') 5 cur = mysql_connect.cursor() 6 cur.execute("show global status") 7 res = list(cur.fetchall()) 8 #print(res) 9 mysql_dict={} 10 for i in res: 11 if list(i)[1].strip() == '': 12 # print('列表第二个元素为空') 13 pass 14 else: 15 tmp_dict={list(i)[0]:list(i)[1]} 16 # print('分割线------------------------') 17 mysql_dict.update(tmp_dict) 18 print(mysql_dict["Connections"]) 19 cur.close() 20 mysql_connect.close()