这里给大家分享一些MySQL的常用性能指标,可以对此增加一些自定义指标到数据库的监控里,如zabbix或者prometheus,来更好的检测数据库的状态。
 我的MySQSL版本是5.7.19。因为是自己的测试环境,所以截图的一些指标很低,仅为大家展示查看参数的显示情况。
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.19    |
+-----------+
1 row in set (0.00 sec)
(1) QPS(每秒Query量)
QPS = Questions(or Queries) / seconds
mysql > show global status like 'Question%'; 

(2) TPS(每秒事务量)
TPS = (Com_commit + Com_rollback) / seconds
mysql > show global status like'Com_commit'; mysql > show global status like'Com_rollback'; 

(3)key Buffer 命中率
key_buffer_read_hits = (1-key_reads /key_read_requests) * 100%
 key_buffer_write_hits = (1-key_writes /key_write_requests) * 100%
mysql>show global status like 'key%'; 

(4)InnoDB Buffer命中率
innodb_buffer_read_hits = (1 -innodb_buffer_pool_reads / innodb_buffer_pool_read_requests) * 100%
mysql> show status like'innodb_buffer_pool_read%'; 

(5)Query Cache命中率
Query_cache_hits = (Qcahce_hits /(Qcache_hits + Qcache_inserts )) * 100%;
mysql> show status like 'Qcache%'; 

(6)Table Cache状态量
mysql> show global status like 'open%'; 
比较 open_tables 与opend_tables 值

(7)Thread Cache 命中率
mysql> show global status like'Thread%'; mysql> show global status like'Connections'; 
Thread_cache_hits = (1 - Threads_created /connections ) * 100%

(8)锁状态
mysql> show global status like '%lock%'; 
Table_locks_waited/Table_locks_immediate=0.0% 如果这个比值比较大的话,说明表锁造成的阻塞比较严重,Innodb_row_lock_waits 和Innodb_row_lock_time_avg太大,说明锁争用比较严重,有可能是间隙锁造成的,可以查询information_schema数据库中相关表查看锁情况,或者通过设置InnoDB Monitors来进一步观察锁冲突的表、数据行等,分析原因。

(9)复制延时
mysql > show slave status; 
查看延时
 当前SQL线程运行的延时=Read_Master_Log_Pos-Exec_Master_Log_Pos
红框:
     Master_Log_File: mysql-bin.003590    //当前IO线程正在读取的主服务器二进制文件的名称
     Read_Master_Log_Pos: 331275450      //当前IO线程正在读取的二进制日志的位置
     Relay_Log_File: relaylog.009253     //SQL线程当前正在读取和执行的中继日志文件的名称
     Relay_Log_Pos: 331275665           //当前中继日志里,SQL线程已读取和执行的位置
     Relay_Master_Log_File: mysql-bin.003590     //由SQL线程执行的包含多数近期事件的主服务器二进制日志文件的名称
黄框:
     Slave_IO_Running: Yes         //IO线程是否被启动并成功的连接到主服务器上
     Slave_SQL_Running: Yes        //SQL线程是否被启动
蓝框:
     Last_Errno: 0                //是记录系统的最后一次错误代码
绿框:
     Exec_Master_Log_Pos: 331275450      //是SQL线程执行中继日志中相对于主库bin log的位点。Read_Master_Log_Pos减去Exec_Master_Log_Pos可以表示当前SQL线程运行的延时
     Relay_Log_Space: 331275954        //所有原有的中继日志结合起来的总大小

(10) Tmp Table 状况(临时表状况)
mysql > show status like 'Created_tmp%'; 
Created_tmp_disk_tables/Created_tmp_tables比值最好不要超过10%,如果Created_tmp_tables值比较大,可能是排序句子过多等原因。

(11) Binlog Cache 使用状况
mysql > show status like'Binlog_cache%'; 
如果Binlog_cache_disk_use值不为0 ,可能需要调大 binlog_cache_size大小

(12) Innodb_log_waits 量
mysql > show status like'innodb_log_waits'; 
Innodb_log_waits是等待日志缓冲刷出的次数,如果值不等于0的话,并且持续增长,表明 innodb log buffer 因为空间不足而等待,可以考虑增大innodb_log_buffer_size。

(13)open file and table
mysql> show global status like 'Open_files'; mysql> show global status like 'Open_tables'; 

(14) 慢查询
开启慢查询:
编辑/etc/my.cnf,在[mysqld]域中添加:
         slow_query_log= 1   # 开启慢查询
         slow_query_log_file=/data/mysql/slow.log   # 慢查询日志路径
         long_query_time= 1        # 慢查询的时长
查看慢查询数量:
[mysql@mogdb-kernel-0005 ~]$ mysqladmin -u root -pmysql status 

(15)通用查询日志
查看通用查询日志状态:
mysql> show global variables like 'general_log'; 

 开启通用查询日志:
mysql> set global general_log=on; 
注意开启通用查询日志会消耗服务器性能,一般只有在排查问题时才会短暂打开。
![[附源码]Python计算机毕业设计SSM基于的考研信息共享平台(程序+LW)](https://img-blog.csdnimg.cn/db0f4d73fad344888ac1dd92ef3ee7b6.png)


















