#!/usr/local/python3./bin/python3.
#!coding:utf-
####################################
#目地:用于诊断mysql性能问题
#作者:蒋乐兴
#时间:--
#create user moniter@'127.0.0.1' identified by 'moniter@2048';
#
#################################### import mysql.connector as connector
import argparse
import psutil
import json
import sys
import os show_global_status_56="select variable_name,variable_value from information_schema.global_status where variable_name= %s"
show_global_variables_56="select variable_name,variable_value from information_schema.global_variables where variable_name= %s"
show_global_status_57="select variable_name,variable_value from performance_schema.global_status where variable_name= %s"
show_global_variables_57="select variable_name,variable_value from performance_schema.global_variables where variable_name= %s" class AnalyseBase(object):
def __init__(self,cursor,args):
self.cursor=cursor
self.args=args
self.result={} def Analyse(self):
"执行分析函数"
pass
def Print(self):
print(json.dumps(analyst.result,sort_keys=True,indent=,ensure_ascii=False)) class AnalyseInnodb(AnalyseBase):
def innodb_log_waits(self):
"status:innodb_log_waits innodb 等待刷新redo log 的次,如果它不是0,说明innodb_log_buffer_size 过小"
self.cursor.execute(args.show_global_status,('innodb_log_waits',))
name,value=self.cursor.fetchone()
comment=None
if int(value)==:
comment='正常'
else:
comment='innodb_log_waits > 0 应该适当增加innodb_log_buffer_size的大小'
self.result['innodb_log_waits']={'name':'innodb_log_waits','value':value,'comment':comment} def innodb_flush_log_at_trx_commit(self):
("variables:innodb_flush_log_at_trx_commit 0:事务提交时并不把redo log 写入日志文件,而是等待主线程每秒的刷新。"
"1:commit 时同步的方式刷新redo log 到日志文件"
"2:commit 时异步的方式刷新redo log 到日志文件")
self.cursor.execute(args.show_global_variables,('innodb_flush_log_at_trx_commit',))
name,value=self.cursor.fetchone()
comment=None
if int(value)==:
comment='正常、由于每个事务完成后都要同步的刷新日志,所以性能不是最好'
else:
comment='注意、有安全隐患;0:事务提交时并不把redo log 写入日志文件,而是等待主线程每秒的刷新;2:commit 时异步的方式刷新redo log 到日志文件。'
self.result['innodb_flush_log_at_trx_commit']={'name':'innodb_flush_log_at_trx_commit','value':value,'comment':comment} def innodb_buffer_pool_size(self):
self.cursor.execute(args.show_global_variables,('innodb_buffer_pool_size',))
name,value=self.cursor.fetchone()
memory_object=psutil.virtual_memory();
total_memory=memory_object.total
rate=float(value)/float(total_memory)
comment=None
if rate <=0.75:
comment="注意、innodb_buffer_pool_size 过小;total_memory:{0}{1} / innodb_buffer_pool_size:{2}{3} = {4}%"
elif rate<=0.85:
comment="正常、innodb_buffer_pool_size 合适;total_memory:{0}{1} / innodb_buffer_pool_size:{2}{3} = {4}%"
else:
comment="注意、innodb_buffer_pool_size 过大;total_memory:{0}{1} / innodb_buffer_pool_size:{2}{3} = {4}%"
sign=args.memoryunit['sign']
unit=int(args.memoryunit['unit'])
value=int(value)
comment=comment.format(value/unit,sign,total_memory/unit,sign,rate*)
self.result['innodb_buffer_pool_size']={'name':'innodb_buffer_pool_size','value':"{0}{1}".format(value/unit,sign),'comment':comment} def innodb_file_per_table(self):
"variables:innodb_file_per_table 不做成单独表空间的话管理不方便"
self.cursor.execute(args.show_global_variables,('innodb_file_per_table',))
name,value=self.cursor.fetchone()
comment=None
if comment=='ON':
comment='正常'
else:
comment='注意、建议开启innodb_file_per_table,以方式管理innodb表空间文件'
self.result['innodb_file_per_table']={'name':'innodb_file_per_table','value':value,'comment':comment} def innodb_io_capacity(self):
"1:在合并插入缓冲时,合并插入缓冲数量为innodb_io_capacity的5%; 2:在从缓冲区刷新脏页时,刷新脏页的数量为innodb_io_capacity页。"
self.cursor.execute(args.show_global_variables,('innodb_io_capacity',))
name,value=self.cursor.fetchone()
comment=("注意、无法确认最优值,请核对磁盘IO能力。在合并插入缓冲时,合并插入缓冲数量为innodb_io_capacity的5%;"
"在从缓冲区刷新脏页时,刷新脏页的数量为innodb_io_capacity页。")
self.result['innodb_io_capacity']={'name':'innodb_io_capacity','value':value,'comment':comment} def innodb_max_dirty_pages_pct(self):
"innodb 在每秒刷新缓冲池时会去判断这个值,如果大于innodb_max_dirty_pages_pct,才会去刷新100个脏页"
self.cursor.execute(args.show_global_variables,('innodb_max_dirty_pages_pct',))
name,value=self.cursor.fetchone()
comment=None
if int(value) <=:
comment=("注意、innodb_max_dirty_pages_pct 过小;这会增加磁盘的IO负载,请适当增加,推荐值75~80")
elif int(value) <=:
comment='正常'
else:
comment='注意、innodb_max_dirty_pages_pct 过大;脏面数量过大,这会影响服务宕机后,重启的用时'
self.result['innodb_max_dirty_pages_pct']={'name':'innodb_max_dirty_pages_pct','value':value,'comment':comment} def Analyse(self):
self.innodb_log_waits()
self.innodb_file_per_table()
self.innodb_flush_log_at_trx_commit()
self.innodb_io_capacity()
self.innodb_max_dirty_pages_pct()
self.innodb_buffer_pool_size() if __name__=="__main__":
parser=argparse.ArgumentParser()
parser.add_argument('--host',default='127.0.0.1',help='ip address of mysql server.....')
parser.add_argument('--port',default=,type=int,help='port number of mysql server....')
parser.add_argument('--user',default='moniter',help='mysql user name................')
parser.add_argument('--password',default='moniter@2048',help='password of mysql user.........')
parser.add_argument('--mysqlversion',default=5.6,choices=['5.6','5.7'],help='version of mysql server........')
parser.add_argument('--memoryunit',default='MB',choices=['G','GB','M','MB','K','KB'])
parser.add_argument('target',default='innodb',choices=['innodb','binlog','all'],help='the part of mysql that you want to tuning')
args=parser.parse_args()
#隔离不同版本mysql数据库的差异
if args.mysqlversion==5.6:
args.show_global_status=show_global_status_56
args.show_global_variables=show_global_variables_56
elif args.mysqlversion==5.7:
args.show_global_status=show_global_status_57
args.show_global_variables=show_global_variables_57
#调整内存单位
unit=*
if args.memoryunit in('G','GB'):
unit=**
elif args.memoryunit in ('M','MB'):
unit=*
elif args.memoryunit in ('K','KB'):
unit=
args.memoryunit={'sign':args.memoryunit,'unit':unit}
cnx=None
cursor=None
connection_config={
'host':args.host,
'port':args.port,
'user':args.user,
'password':args.password
}
try:
cnx=connector.connect(**connection_config)
cursor=cnx.cursor()
analyst=AnalyseInnodb(cursor,args)
analyst.Analyse()
analyst.Print()
except Exception as err:
print(err)
finally:
if cnx != None:
cnx.close()
cursor.close()

mysql 性能分析套件的更多相关文章

  1. MySQL性能分析及explain的使用

    MySQL性能分析及explain用法的知识 1.使用explain语句去查看分析结果 如explain select * from test1 where id=1;会出现:id  selectty ...

  2. MySQL性能分析及explain的使用说明

    1.使用explain语句去查看分析结果 如explain select * from test1 where id=1;会出现:id selecttype table type possible_k ...

  3. mysql性能分析show profile/show profiles

    MySQL性能分析show profiles show profile 和 show profiles 语句可以展示当前会话(退出session后,profiling重置为0) 中执行语句的资源使用情 ...

  4. MySQL性能分析和优化-part 1

    MySQL性能优化 平时我们在使用MySQL的时候,怎么评估系统的运行状态,怎么快速定位系统瓶颈,又如何快速解决问题呢? 本文总结了多年来MySQL优化的经验,系统介绍MySQL优化的方法. OS性能 ...

  5. MySQL性能分析, mysql explain执行计划详解

    MySQL性能分析 MySQL性能分析及explain用法的知识是本文我们主要要介绍的内容,接下来就让我们通过一些实际的例子来介绍这一过程,希望能够对您有所帮助. 1.使用explain语句去查看分析 ...

  6. mysql性能分析工具

    一.EXPALIN 在SQL语句之前加上EXPLAIN关键字就可以获取这条SQL语句执行的计划 那么返回的这些字段是什么呢? 我们先关心一下比较重要的几个字段: 1. select_type 查询类型 ...

  7. MySQL性能分析(转)

    第一步:检查系统的状态 通过操作系统的一些工具检查系统的状态,比如CPU.内存.交换.磁盘的利用率.IO.网络,根据经验或与系统正常时的状态相比对,有时系统表面上看起来看空闲,这也可能不是一个正常的状 ...

  8. mysql性能分析-------profiling和explain

    1. profiling之性能分析 MySQL5.0.37版本以上支持了Profiling – 官方手册.此工具可用来查询 SQL 会执行多少时间,System lock和Table lock 花多少 ...

  9. MySQL性能分析、及调优工具使用详解

    本文汇总了MySQL DBA日常工作中用到的些工具,方便初学者,也便于自己查阅. 先介绍下基础设施(CPU.IO.网络等)检查的工具: vmstat.sar(sysstat工具包).mpstat.op ...

随机推荐

  1. Linux远程拷贝scp命令

    今天要从admin服务器将测试上修正content和image_count后的数据库更新到dz服务器. 首先需要备份数据库,使用mysqldump命令 整表全部备份:    mysqldump -u ...

  2. HTTP 错误 500.21 - Internal Server Error 处理程序“PageHandlerFactory-ISAPI-4.0_32bit”在其模块

    问题: 系统是win7.今天把我做过的项目发布后,在IIS上运行时一直出现一个错误,HTTP 错误500.21-Internal Server Error.处理程序“PageHandlerFactor ...

  3. (转)fastcgi协议的简单实现

    FastCgi不仅可以用于webserver与PHP的交互,也可用于任何两个应用之间的交互,PHPer用的比较多的应该就是用于两个子系统之间的交互. 比如A系统和B系统分部独立的部署在两台机器上,其之 ...

  4. codility上的问题 (21) Upsilon 2012

    这是我目前最喜欢的codiltiy上的问题之一.问题描述是:给定一个整数数组A,所有的数均不相同.假设下标从0开始,找到一个数组B, 满足A[B[0]] > A[B[1]] > A[B[2 ...

  5. sourceforge软件下载方式

    访问http://www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge/ 根据软件名称在文件列表中查找

  6. 【转】P2P之UDP穿透NAT的原理与实现(附源代码)

    作者:shootingstars (有容乃大,无欲则刚)  日期:2004-5-25 出处:P2P中国(PPcn.net) P2P 之 UDP穿透NAT的原理与实现(附源代码)原创:shootings ...

  7. [每日一题] OCP1z0-047 :2013-08-17 EXTERNAL TABLE――加载数据 ............................56

    正确答案:C 一.对答案解释: A.       TYPE:有两个选可供选择: 1.        ORACLE_LOADER:传统方式,与SQLLDR一样,参数从多,应用较多. 2.         ...

  8. Dynamics CRM2013 missing prvReadComplexControl privilege

    左右ComplexControl 权限设置,SDK例如,在以下的说明,仅供内部使用的实体,但是你可以没有找到这个叫配置安全角色ComplexControl的东西的. 在msdn上面查下就会发现这么一段 ...

  9. Android应用程序键盘(Keyboard)消息处理机制分析

    在Android系统中,键盘按键事件是由WindowManagerService服务来管理的,然后再以消息的形 式来分发给应用程序处理,不过和普通消息不一样,它是由硬件中断触发的:在上一篇文章< ...

  10. Oracle 中的Pivoting Insert用法

    1.标准Insert --单表单行插入   语法:   INSERT INTO table [(column1,column2,...)] VALUE (value1,value2,...)      ...