python实现连接MySQL、Redis并获取数据
最近在弄zabbix监控MySQL和Redis事宜,发现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()
首先安装redis
pip install redis
1 #!/usr/bin/env python 2 import redis 3 redis_dict={} 4 conn =redis.Redis(host='192.168.3.5',port=6379,password="123456") 5 redis_dict=conn.info()#输出redis的info信息,以字典的形式输出 6 print(redis_dict) 7 print('+++++++++++++++++++') 8 print(conn.client_list()) #输出连接redis的客户端信息,以字典形式输出