MySQL 5.6大查询和大事务监控脚本(Python 2)
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,可以配置在Zabbix里面,作为监控的模版#!/usr/bin/env python#import MySQLdb,MySQLdb.cursorsimport sys,timefrom datetim
千家信息网最后更新 2026年11月02日MySQL 5.6大查询和大事务监控脚本(Python 2)
可以配置在Zabbix里面,作为监控的模版
#!/usr/bin/env python#import MySQLdb,MySQLdb.cursorsimport sys,timefrom datetime import datetimeinnodb_lock_output_file = '/tmp/innodb_lock_output.log'# socket_dir = '/var/lib/mysql/mysql.sock'time_step = 1db_host = '127.0.0.1'db_port = 23306db_user = 'zabbix'db_pass = 'l8ka65'f = open(innodb_lock_output_file,'a')current_time_stamp = int(time.time()) - time_stepcurrent_time = time.ctime()result = ''# print sys.argvif len(sys.argv) <> 2: print "Usage: %s current_lock | current_running" % sys.argv[0] exit()db = MySQLdb.connect(host=db_host, user=db_user, passwd=db_pass, charset='utf8', port = db_port # unix_socket=socket_dir )conn = db.cursor(MySQLdb.cursors.DictCursor)db.select_db('information_schema')now_time_sql = 'select now() as now_time;'conn.execute(now_time_sql)current_time = conn.fetchall()[0]['now_time']result += str(current_time)result += '\n'lock_sql = ''' SELECT * FROM INNODB_TRX where TIMESTAMPDIFF(SECOND, trx_started, now()) > 1 ORDER BY trx_started LIMIT 1 '''running_sql = '''select user,host,db,time,State,info from PROCESSLIST where TIME > 30 and COMMAND <> 'Sleep' and COMMAND <> 'Binlog Dump' and user <> 'system user' and lower(info) not like '%alter%table%' order by TIME DESC LIMIT 1 '''if sys.argv[1] == 'current_lock': conn.execute(lock_sql) query_result = conn.fetchall() locks = conn.rowcount if locks > 0: cur_time = datetime.now() print (cur_time - query_result[0]['trx_started']).seconds else: print 0 # print result for item in query_result: for each in item: # print each result += str(each) result += '\t' result += ':==>>>>\t' result += str(item[each]) result += '\n' result += '\n' result += '\n' # print result if locks > 0: f.write(result)elif sys.argv[1] == 'current_running': conn.execute(running_sql) query_result = conn.fetchall() thread_count = conn.rowcount if thread_count > 0 : f.write(result) for item in conn.fetchall(): f.write(str(item) + '\n') f.write('\n\n\n\n') print query_result[0]['time'] else: print 0else: print "Usage: %s current_lock | current_running" % sys.argv[0]conn.close()db.close()f.close()执行脚本
# python innodb_lock_monitor.py current_running# python innodb_lock_monitor.py current_lock
慢查询语句会记录在文本文件中
]# tail -300 /tmp/innodb_lock_output.logblocking_trx_state :==>>>> RUNNINGrequesting_SQL :==>>>> delete who_cart,who_cart_ext from who_cart left join who_cart_ext on who_cart.rec_id = who_cart_ext.cart_id where who_cart.rec_id=1469638027
脚本
查询
监控
文件
文本
模版
语句
配置
事务
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
耶加雪菲
互联网黑科技
上海喔噻互联网科技有限公司
湖北九亚互联网络科技有限公司
天津市摩尔互联网科技
广东猪兼强互联网科技有限公司
疫情对互联网科技公司影响
中国 科技 互联网
美国互联网科技
亿策互联网科技
狮子座互联网科技有限公司
新浪科技互联网
福州新华互联网科技学校 知乎
受益于互联网科技的高速发展
亳州好赞互联网科技有限公司
互联网科技的全部知识
世界互联网领先科技成果获奖
为客找房互联网科技公司怎么样
山东子淼互联网科技有限公司
利用互联网等科技手段
互联网是第几次科技革命的成果
互联网科技网
工业互联网科技新基建
互联网 视角下的科技创新
重庆科技学院互联网
山东吉时兔互联网科技有限公司
乌鲁木齐新华智原互联网科技学校
保险科技服务互联网营销体系
江苏互联网智能科技软件
科技和互联网行业的特点