首页 > 数据库技术 > 详细

python实现连接MySQL并获取数据

时间:2019-07-18 16:00:11      阅读:102      评论:0      收藏:0      [点我收藏+]

最近在弄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()

 

python实现连接MySQL并获取数据

原文:https://www.cnblogs.com/kindnull/p/11207814.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!