mysql status

1, 查看MySQL服务器配置信息 

mysql> show variables;  

2, 查看MySQL服务器运行的各种状态值 
mysql> show global status; 

3, 慢查询 :

  1. mysql> show variables like 'slow%';
  2.  
    +---------------------+----------------------------+
  3.  
    | Variable_name | Value |
  4.  
    +---------------------+----------------------------+
  5.  
    | slow_launch_time | 2 |
  6.  
    | slow_query_log | ON |
  7.  
    | slow_query_log_file | /u02/mysql/master-slow.log |
  8.  
    +---------------------+----------------------------
  9.  
    mysql> show global status like 'slow%';
  10.  
    +---------------------+-------+
  11.  
    | Variable_name | Value |
  12.  
    +---------------------+-------+
  13.  
    | Slow_launch_threads | 0 |
  14.  
    | Slow_queries | 1098 |
  15.  
    +---------------------+-------+



4、连接数:

  1.  
    mysql> show variables like 'max_connections';
  2.  
    +----------------------+-------+
  3.  
    | Variable_name | Value |
  4.  
    +----------------------+-------+
  5.  
    | max_connect_errors | 1000 |
  6.  
    | max_connections | 1500 |
  7.  
    | max_user_connections | 1000 |
  8.  
    mysql> show global status like '%connect%';
  9.  
    +-----------------------------------------------+-------+
  10.  
    | Variable_name | Value |
  11.  
    +-----------------------------------------------+-------+
  12.  
    | Aborted_connects | 1 |
  13.  
    | Connection_errors_accept | 0 |
  14.  
    | Connection_errors_internal | 0 |
  15.  
    | Connection_errors_max_connections | 0 |
  16.  
    | Connection_errors_peer_address | 0 |
  17.  
    | Connection_errors_select | 0 |
  18.  
    | Connection_errors_tcpwrap | 0 |
  19.  
    | Connections | 298 |
  20.  
    | Max_used_connections | 780 |
  21.  
    | Performance_schema_session_connect_attrs_lost | 0 |
  22.  
    | Ssl_client_connects | 0 |
  23.  
    | Ssl_connect_renegotiates | 0 |
  24.  
    | Ssl_finished_connects | 0 |
  25.  
    | Threads_connected | 1 |
  26.  
    +-----------------------------------------------+-------+



 Max_used_connections/  max_connections=78%(理想值85%)

超过85%增加 max_connections值。

5,open table 的情况 :

  1.  
    mysql> show variables like 'table%open%';
  2.  
    +----------------------------+-------+
  3.  
    | Variable_name | Value |
  4.  
    +----------------------------+-------+
  5.  
    | table_open_cache | 4096 |
  6.  
    | table_open_cache_instances | 1 |
  7.  
    +----------------------------+-------+
  8.  
    2 rows in set (0.00 sec)
  9.  
    mysql> show global status like 'open%tables%';
  10.  
    +---------------+-------+
  11.  
    | Variable_name | Value |
  12.  
    +---------------+-------+
  13.  
    | Open_tables | 3630 |
  14.  
    | Opened_tables | 6700 |
  15.  
    +---------------+-------+
  16.  
    2 rows in set (0.00 sec)



Open_tables 表示打开表的数量,Opened_tables表示打开过的表数量,如果Opened_tables数量过大,说明配置中 table_cache(5.1.3之后这个值叫做table_open_cache)值可能太小,我们查询一下服务器table_cache值 

6、 进程使用情况 

  1.  
    mysql> show global status like 'Thread%';
  2.  
    +-------------------+-------+
  3.  
    | Variable_name | Value |
  4.  
    +-------------------+-------+
  5.  
    | Threads_cached | 32 |
  6.  
    | Threads_connected | 342 |
  7.  
    | Threads_created | 3567 |
  8.  
    | Threads_running | 278 |
  9.  
    +-------------------+-------+
  10.  
    4 rows in set (0.01 sec)
  11.  
    mysql> show variables like 'thread_cache_size';
  12.  
    +-------------------+-------+
  13.  
    | Variable_name | Value |
  14.  
    +-------------------+-------+
  15.  
    | thread_cache_size | 32 |
  16.  
    +-------------------+-------+



如果我们在MySQL服务器配置文件中设置了thread_cache_size,当客户端断开之后,服务器处理此客户的线程将会缓存起来以响应下一个客户而不是销毁(前提是缓存数未达上限)。Threads_created表示创建过的线程数,如果发现Threads_created值过大的话,表明 MySQL服务器一直在创建线程,这也是比较耗资源,可以适当增加配置文件中thread_cache_size值,查询服务器 thread_cache_size配置: 

7、查询缓存(query cache)

  1.  
    mysql> show global status like 'qcache%';
  2.  
    +-------------------------+----------+
  3.  
    | Variable_name | Value |
  4.  
    +-------------------------+----------+
  5.  
    | Qcache_free_blocks | 2226 |
  6.  
    | Qcache_free_memory | 10794944 |
  7.  
    | Qcache_hits | 5385458 |
  8.  
    | Qcache_inserts | 1806301 |
  9.  
    | Qcache_lowmem_prunes | 433101 |
  10.  
    | Qcache_not_cached | 4429464 |
  11.  
    | Qcache_queries_in_cache | 7168 |
  12.  
    | Qcache_total_blocks | 16820 |
  13.  
    +-------------------------+----------+

Qcache_free_blocks:缓存中相邻内存块的个数。数目大说明可能有碎片。FLUSH QUERY CACHE会对缓存中的碎片进行整理,从而得到一个空闲块。 
Qcache_free_memory:缓存中的空闲内存。 
Qcache_hits:每次查询在缓存中命中时就增大 
Qcache_inserts:每次插入一个查询时就增大。命中次数除以插入次数就是不中比率。 
Qcache_lowmem_prunes:缓存出现内存不足并且必须要进行清理以便为更多查询提供空间的次数。这个数字最好长时间来看;如果这个数字在不断增长,就表示可能碎片非常严重,或者内存很少。(上面的free_blocks和free_memory可以告诉您属于哪种情况) 
Qcache_not_cached:不适合进行缓存的查询的数量,通常是由于这些查询不是 SELECT 语句或者用了now()之类的函数。 
Qcache_queries_in_cache:当前缓存的查询(和响应)的数量。 
Qcache_total_blocks:缓存中块的数量。 


我们再查询一下服务器关于query_cache的配置: 

  1.  
    mysql> show variables like 'query_cache%';
  2.  
    +------------------------------+----------+
  3.  
    | Variable_name | Value |
  4.  
    +------------------------------+----------+
  5.  
    | query_cache_limit | 33554432 |
  6.  
    | query_cache_min_res_unit | 4096 |
  7.  
    | query_cache_size | 33554432 |
  8.  
    | query_cache_type | ON |
  9.  
    | query_cache_wlock_invalidate | OFF |
  10.  
    +------------------------------+----------+



各字段的解释: 
query_cache_limit:超过此大小的查询将不缓存 
query_cache_min_res_unit:缓存块的最小大小 
query_cache_size:查询缓存大小 
query_cache_type:缓存类型,决定缓存什么样的查询,示例中表示不缓存 select sql_no_cache 查询 
query_cache_wlock_invalidate:当有其他客户端正在对MyISAM表进行写操作时,如果查询在query cache中,是否返回cache结果还是等写操作完成再读表获取结果。 
query_cache_min_res_unit的配置是一柄”双刃剑”,默认是4KB,设置值大对大数据查询有好处,但如果你的查询都是小数据查询,就容易造成内存碎片和浪费。 
查询缓存碎片率 = Qcache_free_blocks / Qcache_total_blocks * 100% 
如果查询缓存碎片率超过20%,可以用FLUSH QUERY CACHE整理缓存碎片,或者试试减小query_cache_min_res_unit,如果你的查询都是小数据量的话。 
查询缓存利用率 = (query_cache_size – Qcache_free_memory) / query_cache_size * 100% 
查询缓存利用率在25%以下的话说明query_cache_size设置的过大,可适当减小;查询缓存利用率在80%以上而且Qcache_lowmem_prunes > 50的话说明query_cache_size可能有点小,要不就是碎片太多。 
查询缓存命中率 = (Qcache_hits – Qcache_inserts) / Qcache_hits * 100% 
示例服务器 查询缓存碎片率 = 20.46%,查询缓存利用率 = 62.26%,查询缓存命中率 = 1.94%,命中率很差,可能写操作比较频繁吧,而且可能有些碎片。 

8、文件打开数(open_files) 

  1.  
    mysql> show global status like 'open_files';
  2.  
    +---------------+-------+
  3.  
    | Variable_name | Value |
  4.  
    +---------------+-------+
  5.  
    | Open_files | 821 |
  6.  
    +---------------+-------+
  7.  
     
  8.  
    mysql> show variables like 'open_files_limit';
  9.  
    +------------------+-------+
  10.  
    | Variable_name | Value |
  11.  
    +------------------+-------+
  12.  
    | open_files_limit | 65535 |
  13.  
    +------------------+-------+



比较合适的设置:Open_files / open_files_limit * 100% <= 75% 

9、 表锁情况 

  1.  
    mysql> show global status like 'table_locks%';
  2.  
    +-----------------------+---------+
  3.  
    | Variable_name | Value |
  4.  
    +-----------------------+---------+
  5.  
    | Table_locks_immediate | 4257944 |
  6.  
    | Table_locks_waited | 25182 |
  7.  
    +-----------------------+---------+





Table_locks_immediate 表示立即释放表锁数,Table_locks_waited表示需要等待的表锁数,如果 Table_locks_immediate / Table_locks_waited > 5000,最好采用InnoDB引擎,因为InnoDB是行锁而MyISAM是表锁,对于高并发写入的应用InnoDB效果会好些. 


10、 表扫描情况 

  1.  
    mysql> show global status like 'handler_read%';
  2.  
    +-----------------------+-----------+
  3.  
    | Variable_name | Value |
  4.  
    +-----------------------+-----------+
  5.  
    | Handler_read_first | 108763 |
  6.  
    | Handler_read_key | 92813521 |
  7.  
    | Handler_read_next | 486650793 |
  8.  
    | Handler_read_prev | 688726 |
  9.  
    | Handler_read_rnd | 9321362 |
  10.  
    | Handler_read_rnd_next | 153086384 |
  11.  
    +-----------------------+-----------+
  12.  
     
  13.  
     
  14.  
    mysql> show global status like 'com_select';
  15.  
    +---------------+---------+
  16.  
    | Variable_name | Value |
  17.  
    +---------------+---------+
  18.  
    | Com_select | 2693147 |
  19.  
    +---------------+---------+





计算表扫描率: 
表扫描率 = Handler_read_rnd_next / Com_select 
如果表扫描率超过4000,说明进行了太多表扫描,很有可能索引没有建好,增加read_buffer_size值会有一些好处,但最好不要超过8MB。

11、排序使用情况 


  1.  
    mysql> show global status like 'sort%';
  2.  
    +-------------------+----------+
  3.  
    | Variable_name | Value |
  4.  
    +-------------------+----------+
  5.  
    | Sort_merge_passes | 2136 |
  6.  
    | Sort_range | 81888 |
  7.  
    | Sort_rows | 35918141 |
  8.  
    | Sort_scan | 55269 |
  9.  
    +-------------------+----------+





Sort_merge_passes 包括两步。MySQL 首先会尝试在内存中做排序,使用的内存大小由系统变量 Sort_buffer_size 决定,如果它的大小不够把所有的记录都读到内存中,MySQL 就会把每次在内存中排序的结果存到临时文件中,等 MySQL 找到所有记录之后,再把临时文件中的记录做一次排序。这再次排序就会增加 Sort_merge_passes。实际上,MySQL 会用另一个临时文件来存再次排序的结果,所以通常会看到 Sort_merge_passes 增加的数值是建临时文件数的两倍。因为用到了临时文件,所以速度可能会比较慢,增加 Sort_buffer_size 会减少 Sort_merge_passes 和 创建临时文件的次数。但盲目的增加 Sort_buffer_size 并不一定能提高速度.
另外,增加read_rnd_buffer_size(3.2.3是record_rnd_buffer_size)的值对排序的操作也有一点的好处.

原文地址:https://www.cnblogs.com/aaron-agu/p/13953896.html