MySQL系列:show Global STATUS 用法

目录

作用

优化方法

全部变量

扩展


作用

可以列出MySQL服务器运行各种状态值

 

优化方法

连接数

//mysql的最大连接数
show global status like 'max_connections';

+-----------------+-------+  
| Variable_name   | Value |  
+-----------------+-------+  
| max_connections | 500   |  
+-----------------+-------+  

常见的问题"MYSQL: ERROR 1040: Too many connections"的异常情况,造成这种情况的一种原因是用户访问量过高,
MySQL服务器抗不住也有可能是最大连接数设置的过小,需要注意

//服务器响应的最大连接数
show global status like 'max_used_connections';  
+----------------------+-------+  
| Variable_name        | Value |  
+----------------------+-------+  
| Max_used_connections | 450   |  
+----------------------+-------+  

设置的最大连接数是500,而响应的连接数是498,max_used_connections / max_connections * 100% = 90% (理想值 ≈ 85%)。
当前响应数占比太小也不好,说明利用率不高。 比较理想的是在10%以上,不长期徘徊在10%以下,说明最大链接数量设置过高

索引缓冲区的大小 key_buffer_size(MyISAM)

show global status like 'key_read%';     
+-------------------+----------+     
| Variable_name     | Value    |     
+-------------------+----------+     
| Key_read_requests | 600 |     
| Key_reads         | 60    |     
+-------------------+----------+    

通过检查状态值Key_read_requests和Key_reads,可以知道key_buffer_size设置是否合理。
比例key_reads /key_read_requests应该尽可能的低,至少是1:100,1:1000更好
key_buffer_size只对MyISAM表起作用
  
mysql> show variables like 'key_buffer_size';  
+-----------------+----------+  
| Variable_name   | Value    |  
+-----------------+----------+  
| key_buffer_size | 16777216 |  
+-----------------+----------+  
  
一共有600个索引读取请求,有60个请求在内存中没有找到直接从硬盘读取索引,计算索引未命中缓存的概率:   
key_cache_miss_rate = Key_reads / Key_read_requests * 100% =0.27%   
需要适当加大key_buffer_size   

open table 的情况

如果查询非常满,并且通过show processlist 发现state一栏中比较多的查询正在opening table,那么可以使用接下来的命令优化

mysql> show global status like 'open%tables%';     
+---------------+-------+     
| Variable_name | Value |     
+---------------+-------+     
| Open_tables   | 299   |     
| Opened_tables | 33000 |     
+---------------+-------+    
 
Open_tables 表示打开表的数量,Opened_tables表示打开过的表数量,如果Opened_tables数量过大,
说明配置中 table_cache值可能太小,我们查询一下服务器table_open_cache值   

mysql> show variables like '%open_%';  
+---------------+-------+ ------+------+    
| Variable_name                | Value |  
+---------------+-------+------+ ------+    
|innodb_open_files             | 3000  |  
|open_files_limit              | 65535   |  
|table_open_cache              | 200   |  
| table_open_cache_instances   | 16    |  
+---------------+-------+------+------+      
  
比较理想的值:
Open_tables / Opened_tables >= 0.85
Open_tables / table_open_cache <= 0.95

table_open_cache_instances:表缓存实例数,为通过减小会话间争用提高扩展性,表缓存会分区为table_open_cache/table_open_cache_instances大小的较小的缓存实例。DML语句会话只需要锁定所在缓存实例,
这样多个会话访问表缓存时就可提升性能(DDL语句仍会锁定整个缓存)。默认该值为1,当16核以上可设置为8或16。

进程使用情况

mysql> show global status like 'Thread%';  
+-------------------+-------+  
| Variable_name     | Value |  
+-------------------+-------+  
| Threads_cached    | 31    |  
| Threads_connected | 239   |  
| Threads_created   | 2914  |  
| Threads_running   | 4     |  
+-------------------+-------+  
Threads_connected 表示当前连接数。Threads_running是代表当前并发数

Threads_running有如下可能
1. 某个DML线程阻塞到了其他select 进程,导致堆积。
2. 缓存失效,某个缓存对象在某个时间点统一失效,导致各台web服务器同时并发进行数据库访问。

Threads_created表示创建过的线程数,如果发现Threads_created值过大的话,表明 MySQL服务器一直在创建线程,这也是比较耗资源,
可以适当增加配置文件中thread_cache_size值,查询服务器 thread_cache_size配置:   
  
mysql> show variables like 'thread_cache_size';     
+-------------------+-------+     
| Variable_name     | Value |     
+-------------------+-------+     
| thread_cache_size | 32    |     
+-------------------+-------+    

 连接池配置建议: thread_cache_size =  8 + (max_connections / 100)

锁情况

 //表锁
mysql> show global status like 'table_locks%';  
+-----------------------+---------+  
| Variable_name         | Value   |  
+-----------------------+---------+  
| Table_locks_immediate | 42579   |  
| Table_locks_waited    | 200     |  
+-----------------------+---------+  
Table_locks_immediate 表示立即释放表锁数,Table_locks_waited表示需要等待的表锁数。
如果 Table_locks_immediate / Table_locks_waited > 5000,最好采用InnoDB引擎,因为InnoDB是行锁而MyISAM是表锁,对于高并发写入的应用InnoDB效果会好些. 
如果InnoDB中,该值较高,并且有性能问题,应首先优化查询

//行锁
mysql> show status like 'innodb_row_lock%';

+-------------------------------+-------+
| Variable_name                 | Value |
+-------------------------------+-------+
| InnoDB_row_lock_current_waits | 0     |
| InnoDB_row_lock_time          | 0     |
| InnoDB_row_lock_time_avg      | 0     |
| InnoDB_row_lock_time_max      | 0     |
| InnoDB_row_lock_waits         | 0     |
+-------------------------------+-------+
如果发现锁争用比较严重,如InnoDB_row_lock_waits和InnoDB_row_lock_time_avg的值比较高,
还可以通过设置InnoDB Monitors来进一步观察发生锁冲突的表、数据行等,并分析锁争用的原因。
//创建监视器
CREATE TABLE innodb_monitor(a INT) ENGINE=INNODB;
//查询状态
show engine innodb status
//关闭监视器
DROP TABLE innodb_monitor;

InnoDB缓存(缓存数据和索引)

mysql> show global variables like 'innodb_buffer_pool_size';
+-------------------------+-----------+
| Variable_name           | Value     |
+-------------------------+-----------+
| innodb_buffer_pool_size | 536870912 |
+-------------------------+-----------+

那么如何设置该参数大小呢?首先查看运行时buffer pool相关数据指标:

mysql> show global status like 'Innodb_buffer_pool_pages_data';
+-------------------------------+-------+
| Variable_name                 | Value |
+-------------------------------+-------+
| Innodb_buffer_pool_pages_data | 9696  |
+-------------------------------+-------+

mysql> show global status like 'Innodb_buffer_pool_pages_total';
+--------------------------------+-------+
| Variable_name                  | Value |
+--------------------------------+-------+
| Innodb_buffer_pool_pages_total | 32764 |
+--------------------------------+-------+

mysql> show global status like 'Innodb_page_size';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| Innodb_page_size | 16384 |
+------------------+-------+

上述三项指标的含义如下:

Innodb_buffer_pool_pages_data
The number of pages in the InnoDB buffer pool containing data. The number includes both dirty and clean pages.

Innodb_buffer_pool_pages_total
The total size of the InnoDB buffer pool, in pages.

Innodb_page_size
InnoDB page size (default 16KB). Many values are counted in pages; the page size enables them to beeasily converted to bytes

计算Innodb_buffer_pool_pages_data/Innodb_buffer_pool_pages_total*100%
当结果 > 95% 则增加 innodb_buffer_pool_size, 建议使用物理内存的 75%
当结果 < 95% 则减少 innodb_buffer_pool_size, 
建议设置大小为: Innodb_buffer_pool_pages_data* Innodb_page_size * 1.05 / (1024*1024*1024)
命令如:SET GLOBAL innodb_buffer_pool_size= 22222223:单位kb

查询缓存

mysql> show global status like 'qcache%';
+-------------------------+-----------+
| Variable_name           | Value     |
+-------------------------+-----------+
| Qcache_free_blocks      | 3         |
| Qcache_free_memory      | 3128864   |
| Qcache_hits             | 224134    |
| Qcache_inserts          | 382       |
| Qcache_lowmem_prunes    | 0         |
| Qcache_not_cached       | 6741      |
| Qcache_queries_in_cache | 40        |
| Qcache_total_blocks     | 95        |
+-------------------------+-----------+

Qcache_free_blocks:缓存中相邻内存块的个数。数目大说明可能有碎片
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,则对query cache 命中率进行监控也是必须的,query cache命中率计算如下:
query_cache_hits =(Qcache_hits/(Qcache_hits+Qcache_inserts))* 100%;


mysql> show variables like 'query_cache%';
+------------------------------+-----------+
| Variable_name                | Value     |
+------------------------------+-----------+
| query_cache_limit            | 2097152   |
| query_cache_min_res_unit     | 2048      |
| query_cache_size             | 536870912 |
| query_cache_type             | OFF       |
| query_cache_wlock_invalidate | OFF       |
+------------------------------+-----------+

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结果还是等写操作完成再读表获取结果。

If you often have recurring queries for tables that are not updated frequently,
enable the query cache:
query_cache_type = 1
query_cache_size = 10M

全部变量

Aborted_clients    0
Aborted_connects    0
Binlog_cache_disk_use    0
Binlog_cache_use    0
Binlog_stmt_cache_disk_use    0
Binlog_stmt_cache_use    0
Bytes_received    610500
Bytes_sent    2713751
Com_admin_commands    0
Com_assign_to_keycache    0
Com_alter_db    0
Com_alter_db_upgrade    0
Com_alter_event    0
Com_alter_function    0
Com_alter_instance    0
Com_alter_procedure    0
Com_alter_server    0
Com_alter_table    0
Com_alter_tablespace    0
Com_alter_user    0
Com_analyze    0
Com_begin    1081
Com_binlog    0
Com_call_procedure    0
Com_change_db    26
Com_change_master    0
Com_change_repl_filter    0
Com_check    0
Com_checksum    0
Com_commit    1081
Com_create_db    0
Com_create_event    0
Com_create_function    0
Com_create_index    0
Com_create_procedure    0
Com_create_server    0
Com_create_table    0
Com_create_trigger    0
Com_create_udf    0
Com_create_user    0
Com_create_view    0
Com_dealloc_sql    0
Com_delete    204
Com_delete_multi    0
Com_do    0
Com_drop_db    0
Com_drop_event    0
Com_drop_function    0
Com_drop_index    0
Com_drop_procedure    0
Com_drop_server    0
Com_drop_table    0
Com_drop_trigger    0
Com_drop_user    0
Com_drop_view    0
Com_empty_query    0
Com_execute_sql    0
Com_explain_other    0
Com_flush    0
Com_get_diagnostics    0
Com_grant    0
Com_ha_close    0
Com_ha_open    0
Com_ha_read    0
Com_help    0
Com_insert    580
Com_insert_select    0
Com_install_plugin    0
Com_kill    0
Com_load    0
Com_lock_tables    0
Com_optimize    0
Com_preload_keys    0
Com_prepare_sql    0
Com_purge    0
Com_purge_before_date    0
Com_release_savepoint    0
Com_rename_table    0
Com_rename_user    0
Com_repair    0
Com_replace    0
Com_replace_select    0
Com_reset    0
Com_resignal    0
Com_revoke    0
Com_revoke_all    0
Com_rollback    0
Com_rollback_to_savepoint    0
Com_savepoint    0
Com_select    1897
Com_set_option    55
Com_signal    0
Com_show_binlog_events    0
Com_show_binlogs    0
Com_show_charsets    0
Com_show_collations    0
Com_show_create_db    0
Com_show_create_event    0
Com_show_create_func    0
Com_show_create_proc    0
Com_show_create_table    0
Com_show_create_trigger    0
Com_show_databases    1
Com_show_engine_logs    0
Com_show_engine_mutex    0
Com_show_engine_status    0
Com_show_events    0
Com_show_errors    0
Com_show_fields    0
Com_show_function_code    0
Com_show_function_status    1
Com_show_grants    0
Com_show_keys    0
Com_show_master_status    0
Com_show_open_tables    0
Com_show_plugins    0
Com_show_privileges    0
Com_show_procedure_code    0
Com_show_procedure_status    1
Com_show_processlist    2
Com_show_profile    0
Com_show_profiles    0
Com_show_relaylog_events    0
Com_show_slave_hosts    0
Com_show_slave_status    2
Com_show_status    14
Com_show_storage_engines    0
Com_show_table_status    0
Com_show_tables    4
Com_show_triggers    0
Com_show_variables    6
Com_show_warnings    0
Com_show_create_user    0
Com_shutdown    0
Com_slave_start    0
Com_slave_stop    0
Com_group_replication_start    0
Com_group_replication_stop    0
Com_stmt_execute    0
Com_stmt_close    0
Com_stmt_fetch    0
Com_stmt_prepare    0
Com_stmt_reset    0
Com_stmt_send_long_data    0
Com_truncate    0
Com_uninstall_plugin    0
Com_unlock_tables    0
Com_update    21
Com_update_multi    0
Com_xa_commit    0
Com_xa_end    0
Com_xa_prepare    0
Com_xa_recover    0
Com_xa_rollback    0
Com_xa_start    0
Com_stmt_reprepare    0
Connection_errors_accept    0
Connection_errors_internal    0
Connection_errors_max_connections    0
Connection_errors_peer_address    0
Connection_errors_select    0
Connection_errors_tcpwrap    0
Connections    29
Created_tmp_disk_tables    9
Created_tmp_files    6
Created_tmp_tables    81
Delayed_errors    0
Delayed_insert_threads    0
Delayed_writes    0
Flush_commands    1
Handler_commit    3143
Handler_delete    64424
Handler_discover    0
Handler_external_lock    7331
Handler_mrr_init    0
Handler_prepare    0
Handler_read_first    371
Handler_read_key    21204
Handler_read_last    6
Handler_read_next    98179
Handler_read_prev    22
Handler_read_rnd    330
Handler_read_rnd_next    159188
Handler_rollback    0
Handler_savepoint    0
Handler_savepoint_rollback    0
Handler_update    155
Handler_write    11642
Innodb_buffer_pool_dump_status    Dumping of buffer pool not started
Innodb_buffer_pool_load_status    Buffer pool(s) load completed at 200818 22:54:06
Innodb_buffer_pool_resize_status    
Innodb_buffer_pool_pages_data    2230
Innodb_buffer_pool_bytes_data    36536320
Innodb_buffer_pool_pages_dirty    529
Innodb_buffer_pool_bytes_dirty    8667136
Innodb_buffer_pool_pages_flushed    1586
Innodb_buffer_pool_pages_free    5912
Innodb_buffer_pool_pages_misc    49
Innodb_buffer_pool_pages_total    8191
Innodb_buffer_pool_read_ahead_rnd    0
Innodb_buffer_pool_read_ahead    241
Innodb_buffer_pool_read_ahead_evicted    0
Innodb_buffer_pool_read_requests    1407944
Innodb_buffer_pool_reads    1800
Innodb_buffer_pool_wait_free    0
Innodb_buffer_pool_write_requests    429035
Innodb_data_fsyncs    1058
Innodb_data_pending_fsyncs    0
Innodb_data_pending_reads    0
Innodb_data_pending_writes    0
Innodb_data_read    33706496
Innodb_data_reads    2143
Innodb_data_writes    2457
Innodb_data_written    65130496
Innodb_dblwr_pages_written    1519
Innodb_dblwr_writes    107
Innodb_log_waits    0
Innodb_log_write_requests    31977
Innodb_log_writes    668
Innodb_os_log_fsyncs    749
Innodb_os_log_pending_fsyncs    0
Innodb_os_log_pending_writes    0
Innodb_os_log_written    14216704
Innodb_page_size    16384
Innodb_pages_created    178
Innodb_pages_read    2052
Innodb_pages_written    1586
Innodb_row_lock_current_waits    0
Innodb_row_lock_time    0
Innodb_row_lock_time_avg    0
Innodb_row_lock_time_max    0
Innodb_row_lock_waits    0
Innodb_rows_deleted    64427
Innodb_rows_inserted    2603
Innodb_rows_read    244735
Innodb_rows_updated    21
Innodb_num_open_files    82
Innodb_truncated_status_writes    0
Innodb_available_undo_logs    128
Key_blocks_not_flushed    0
Key_blocks_unused    6693
Key_blocks_used    5
Key_read_requests    14
Key_reads    5
Key_write_requests    0
Key_writes    0
Locked_connects    0
Max_execution_time_exceeded    0
Max_execution_time_set    0
Max_execution_time_set_failed    0
Max_used_connections    23
Max_used_connections_time    2020-08-18 22:59:58
Not_flushed_delayed_rows    0
Ongoing_anonymous_transaction_count    0
Open_files    16
Open_streams    0
Open_table_definitions    178
Open_tables    270
Opened_files    653
Opened_table_definitions    178
Opened_tables    277
Performance_schema_accounts_lost    0
Performance_schema_cond_classes_lost    0
Performance_schema_cond_instances_lost    0
Performance_schema_digest_lost    0
Performance_schema_file_classes_lost    0
Performance_schema_file_handles_lost    0
Performance_schema_file_instances_lost    0
Performance_schema_hosts_lost    0
Performance_schema_index_stat_lost    0
Performance_schema_locker_lost    0
Performance_schema_memory_classes_lost    0
Performance_schema_metadata_lock_lost    0
Performance_schema_mutex_classes_lost    0
Performance_schema_mutex_instances_lost    0
Performance_schema_nested_statement_lost    0
Performance_schema_prepared_statements_lost    0
Performance_schema_program_lost    0
Performance_schema_rwlock_classes_lost    0
Performance_schema_rwlock_instances_lost    0
Performance_schema_session_connect_attrs_lost    0
Performance_schema_socket_classes_lost    0
Performance_schema_socket_instances_lost    0
Performance_schema_stage_classes_lost    0
Performance_schema_statement_classes_lost    0
Performance_schema_table_handles_lost    0
Performance_schema_table_instances_lost    0
Performance_schema_table_lock_stat_lost    0
Performance_schema_thread_classes_lost    0
Performance_schema_thread_instances_lost    0
Performance_schema_users_lost    0
Prepared_stmt_count    0
Qcache_free_blocks    1
Qcache_free_memory    1031832
Qcache_hits    0
Qcache_inserts    0
Qcache_lowmem_prunes    0
Qcache_not_cached    1896
Qcache_queries_in_cache    0
Qcache_total_blocks    1
Queries    4985
Questions    4984
Rsa_public_key    -----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAskayeJXe6koPQegwhTtk
YpLp37XTVpbc2U9XmmLOVaoHVq1Ic6munYUc6/QbI5BwzaMEkJpvBILXatRLzaeB
hBUo0J9x9dW2gK6gLCe3PVZVTGSKifDa9yDDy4oJN5p5ewWPDp5FM5TJKuhLD5YW
VIktSpJ6/2d8yqIF9S/X17PKjwHjMoSIFT8l+lQ+zbRiByCb4DziOsyqTU32kmnP
XDT8oPbgQ7qIuNPXN6fX0NgHmWdZL7ghE+ZeHiXKVipQ26SAJoaKNuJJBJpUOyfw
B0WdSU2GDyaqLAF9qZoo3CMGsfaMPdBnx2uuKMXcIBy9bprFbvfTZ841O/4WFCyi
CwIDAQAB
-----END PUBLIC KEY-----

Select_full_join    7
Select_full_range_join    0
Select_range    438
Select_range_check    0
Select_scan    373
Slave_open_temp_tables    0
Slow_launch_threads    0
Slow_queries    0
Sort_merge_passes    0
Sort_range    411
Sort_rows    340
Sort_scan    25
Ssl_accept_renegotiates    0
Ssl_accepts    0
Ssl_callback_cache_hits    0
Ssl_cipher    
Ssl_cipher_list    
Ssl_client_connects    0
Ssl_connect_renegotiates    0
Ssl_ctx_verify_depth    18446744073709551615
Ssl_ctx_verify_mode    5
Ssl_default_timeout    0
Ssl_finished_accepts    0
Ssl_finished_connects    0
Ssl_server_not_after    Mar  1 09:31:50 2030 GMT
Ssl_server_not_before    Mar  3 09:31:50 2020 GMT
Ssl_session_cache_hits    0
Ssl_session_cache_misses    0
Ssl_session_cache_mode    SERVER
Ssl_session_cache_overflows    0
Ssl_session_cache_size    128
Ssl_session_cache_timeouts    0
Ssl_sessions_reused    0
Ssl_used_session_cache_entries    0
Ssl_verify_depth    0
Ssl_verify_mode    0
Ssl_version    
Table_locks_immediate    120
Table_locks_waited    0
Table_open_cache_hits    3387
Table_open_cache_misses    277
Table_open_cache_overflows    0
Tc_log_max_pages_used    0
Tc_log_page_size    0
Tc_log_page_waits    0
Threads_cached    1
Threads_connected    22
Threads_created    23
Threads_running    1
Uptime    404
Uptime_since_flush_status    404
validate_password_dictionary_file_last_parsed    2020-08-18 22:54:06
validate_password_dictionary_file_words_count    0

 

扩展

MySQL系列:show Global VARIABLES 用法

 MySQL系列:show full PROCESSLIST 用法

 

 

你可能感兴趣的:(MySQL)