(轉)根據mysql的status信息優化mysql

1, 查看MySQL服務器配置信息
mysql> show variables;
2, 查看MySQL服務器運行的各種狀態值
mysql> show global status;
3, 慢查詢
mysql> show variables like '%slow%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| log_slow_queries | OFF |
| slow_launch_time | 2 |
+------------------+-------+
mysql> show global status like '%slow%';
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| Slow_launch_threads | 0 |
| Slow_queries | 279 |
+---------------------+-------+
mysql> show variables like '%slow%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| log_slow_queries | OFF |
| slow_launch_time | 2 |
+------------------+-------+
mysql> show global status like '%slow%';
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| Slow_launch_threads | 0 |
| Slow_queries | 279 |
+---------------------+-------+
配置中關閉了記錄慢查詢(最好是打開,方便優化),超過2秒即爲慢查詢,一共有279條慢查詢
4, 連接數

mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 500 |
+-----------------+-------+

mysql> show global status like 'max_used_connections';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| Max_used_connections | 498 |
+----------------------+-------+
mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 500 |
+-----------------+-------+
mysql> show global status like 'max_used_connections';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| Max_used_connections | 498 |
+----------------------+-------+
設置的最大連接數是500,而響應的連接數是498
max_used_connections / max_connections * 100% = 99.6% (理想值 ≈ 85%)
5, key_buffer_size
key_buffer_size是對MyISAM表性能影響最大的一個參數, 不過數據庫中多爲Innodb

mysql> show variables like 'key_buffer_size';
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| key_buffer_size | 67108864 |
+-----------------+----------+

mysql> show global status like 'key_read%';
+-------------------+----------+
| Variable_name | Value |
+-------------------+----------+
| Key_read_requests | 25629497 |
| Key_reads | 66071 |
+-------------------+----------+
mysql> show variables like 'key_buffer_size';
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| key_buffer_size | 67108864 |
+-----------------+----------+
mysql> show global status like 'key_read%';
+-------------------+----------+
| Variable_name | Value |
+-------------------+----------+
| Key_read_requests | 25629497 |
| Key_reads | 66071 |
+-------------------+----------+
一共有25629497個索引讀取請求,有66071個請求在內存中沒有找到直接從硬盤讀取索引,計算索引未命中緩存的概率:
key_cache_miss_rate = Key_reads / Key_read_requests * 100% =0.27%
需要適當加大key_buffer_size

mysql> show global status like 'key_blocks_u%';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Key_blocks_unused | 10285 |
| Key_blocks_used | 47705 |
+-------------------+-------+
mysql> show global status like 'key_blocks_u%';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Key_blocks_unused | 10285 |
| Key_blocks_used | 47705 |
+-------------------+-------+
Key_blocks_unused表示未使用的緩存簇(blocks)數,Key_blocks_used表示曾經用到的最大的blocks數
Key_blocks_used / (Key_blocks_unused + Key_blocks_used) * 100% ≈ 18% (理想值 ≈ 80%)
6, 臨時表

mysql> show global status like 'created_tmp%';
+-------------------------+---------+
| Variable_name | Value |
+-------------------------+---------+
| Created_tmp_disk_tables | 4184337 |
| Created_tmp_files | 4124 |
| Created_tmp_tables | 4215028 |
+-------------------------+---------+
mysql> show global status like 'created_tmp%';
+-------------------------+---------+
| Variable_name | Value |
+-------------------------+---------+
| Created_tmp_disk_tables | 4184337 |
| Created_tmp_files | 4124 |
| Created_tmp_tables | 4215028 |
+-------------------------+---------+
每次創建臨時表,Created_tmp_tables增加,如果是在磁盤上創建臨時表,Created_tmp_disk_tables也增加,Created_tmp_files表示MySQL服務創建的臨時文件文件數:
Created_tmp_disk_tables / Created_tmp_tables * 100% = 99% (理想值<= 25%)

mysql> show variables where Variable_name in ('tmp_table_size', 'max_heap_table_size');
+---------------------+-----------+
| Variable_name | Value |
+---------------------+-----------+
| max_heap_table_size | 134217728 |
| tmp_table_size | 134217728 |
+---------------------+-----------+
mysql> show variables where Variable_name in ('tmp_table_size', 'max_heap_table_size');
+---------------------+-----------+
| Variable_name | Value |
+---------------------+-----------+
| max_heap_table_size | 134217728 |
| tmp_table_size | 134217728 |
+---------------------+-----------+
需要增加tmp_table_size
7,open table 的情況

mysql> show global status like 'open%tables%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Open_tables | 1024 |
| Opened_tables | 1465 |
+---------------+-------+
mysql> show global status like 'open%tables%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Open_tables | 1024 |
| Opened_tables | 1465 |
+---------------+-------+
Open_tables 表示打開表的數量,Opened_tables表示打開過的表數量,如果Opened_tables數量過大,說明配置中 table_cache(5.1.3之後這個值叫做table_open_cache)值可能太小,我們查詢一下服務器table_cache值

mysql> show variables like 'table_cache';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| table_cache | 1024 |
+---------------+-------+
mysql> show variables like 'table_cache';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| table_cache | 1024 |
+---------------+-------+
Open_tables / Opened_tables * 100% =69% 理想值 (>= 85%)
Open_tables / table_cache * 100% = 100% 理想值 (<= 95%)
8, 進程使用情況

mysql> show global status like 'Thread%';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_cached | 31 |
| Threads_connected | 239 |
| Threads_created | 2914 |
| Threads_running | 4 |
+-------------------+-------+
mysql> show global status like 'Thread%';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_cached | 31 |
| Threads_connected | 239 |
| Threads_created | 2914 |
| Threads_running | 4 |
+-------------------+-------+
如果我們在MySQL服務器配置文件中設置了thread_cache_size,當客戶端斷開之後,服務器處理此客戶的線程將會緩存起來以響應下一個客戶而不是銷燬(前提是緩存數未達上限)。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 |
+-------------------+-------+
mysql> show variables like 'thread_cache_size';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| thread_cache_size | 32 |
+-------------------+-------+
9, 查詢緩存(query cache)

mysql> show global status like 'qcache%';
+-------------------------+----------+
| Variable_name | Value |
+-------------------------+----------+
| Qcache_free_blocks | 2226 |
| Qcache_free_memory | 10794944 |
| Qcache_hits | 5385458 |
| Qcache_inserts | 1806301 |
| Qcache_lowmem_prunes | 433101 |
| Qcache_not_cached | 4429464 |
| Qcache_queries_in_cache | 7168 |
| Qcache_total_blocks | 16820 |
+-------------------------+----------+
mysql> show global status like 'qcache%';
+-------------------------+----------+
| Variable_name | Value |
+-------------------------+----------+
| Qcache_free_blocks | 2226 |
| Qcache_free_memory | 10794944 |
| Qcache_hits | 5385458 |
| Qcache_inserts | 1806301 |
| Qcache_lowmem_prunes | 433101 |
| Qcache_not_cached | 4429464 |
| Qcache_queries_in_cache | 7168 |
| Qcache_total_blocks | 16820 |
+-------------------------+----------+
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的配置:

mysql> show variables like 'query_cache%';
+------------------------------+----------+
| Variable_name | Value |
+------------------------------+----------+
| query_cache_limit | 33554432 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 33554432 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
+------------------------------+----------+
mysql> show variables like 'query_cache%';
+------------------------------+----------+
| Variable_name | Value |
+------------------------------+----------+
| query_cache_limit | 33554432 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 33554432 |
| query_cache_type | ON |
| 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結果還是等寫操作完成再讀表獲取結果。
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

怎麼配置MySQL服務器,但考慮到服務器硬件配置的不同,具體應用的差別,那些文章的做法只能作爲初步設置參考,我們需要根據自己的情況進行配置優化,好的做法是MySQL服務器穩定運行了一段時間後運行,根據服務器的”狀態”進行優化。

   mysql> show global status;
  可以列出MySQL服務器運行各種狀態值,另外,查詢MySQL服務器配置信息語句:

   mysql> show variables;
  一、慢查詢

   mysql> show variables like '%slow%';
  +------------------+-------+

  | Variable_name | Value |

  +------------------+-------+

  | log_slow_queries | ON |

  | slow_launch_time | 2 |

  +------------------+-------+

  mysql> show global status like '%slow%';

  +---------------------+-------+

  | Variable_name | Value |

  +---------------------+-------+

  | Slow_launch_threads | 0 |

  | Slow_queries | 4148 |

  +---------------------+-------+

  配置中打開了記錄慢查詢,執行時間超過2秒的即爲慢查詢,系統顯示有4148個慢查詢,你可以分析慢查詢日誌,找出有問題的SQL語句,慢查詢時間不宜設置過長,否則意義不大,最好在5秒以內,如果你需要微秒級別的慢查詢,可以考慮給MySQL打補丁:http://www.percona.com /docs/wiki/release:start,記得找對應的版本。

  打開慢查詢日誌可能會對系統性能有一點點影響,如果你的MySQL是主-從結構,可以考慮打開其中一臺從服務器的慢查詢日誌,這樣既可以監控慢查詢,對系統性能影響又小。

  二、連接數

  經常會遇見”MySQL: ERROR 1040: Too many connections”的情況,一種是訪問量確實很高,MySQL服務器抗不住,這個時候就要考慮增加從服務器分散讀壓力,另外一種情況是MySQL配置文件中max_connections值過小:

   mysql> show variables like 'max_connections';
  +-----------------+-------+

  | Variable_name | Value |

  +-----------------+-------+

  | max_connections | 256 |

  +-----------------+-------+

  這臺MySQL服務器最大連接數是256,然後查詢一下服務器響應的最大連接數:

  mysql> show global status like 'Max_used_connections';

  MySQL服務器過去的最大連接數是245,沒有達到服務器連接數上限256,應該沒有出現1040錯誤,比較理想的設置是:

  Max_used_connections / max_connections * 100% ≈ 85%

  最大連接數占上限連接數的85%左右,如果發現比例在10%以下,MySQL服務器連接數上限設置的過高了。

  三、Key_buffer_size

  key_buffer_size是對MyISAM表性能影響最大的一個參數,下面一臺以MyISAM爲主要存儲引擎服務器的配置:

   mysql> show variables like 'key_buffer_size';
  +-----------------+------------+

  | Variable_name | Value |

  +-----------------+------------+

  | key_buffer_size | 536870912 |

  +-----------------+------------+

  分配了512MB內存給key_buffer_size,我們再看一下key_buffer_size的使用情況:

   mysql> show global status like 'key_read%';
  +------------------------+-------------+

  | Variable_name | Value |

  +------------------------+-------------+

  | Key_read_requests | 27813678764 |

  | Key_reads | 6798830 |

  +------------------------+-------------+

  一共有27813678764個索引讀取請求,有6798830個請求在內存中沒有找到直接從硬盤讀取索引,計算索引未命中緩存的概率:

  key_cache_miss_rate = Key_reads / Key_read_requests * 100%

  比如上面的數據,key_cache_miss_rate爲0.0244%,4000個索引讀取請求才有一個直接讀硬盤,已經很BT 了,key_cache_miss_rate在0.1%以下都很好(每1000個請求有一個直接讀硬盤),如果key_cache_miss_rate在 0.01%以下的話,key_buffer_size分配的過多,可以適當減少。

  MySQL服務器還提供了key_blocks_*參數:

   mysql> show global status like 'key_blocks_u%';
  +------------------------+-------------+

  | Variable_name | Value |

  +------------------------+-------------+

  | Key_blocks_unused | 0 |

  | Key_blocks_used | 413543 |

  +------------------------+-------------+

  Key_blocks_unused表示未使用的緩存簇(blocks)數,Key_blocks_used表示曾經用到的最大的blocks數,比如這臺服務器,所有的緩存都用到了,要麼增加key_buffer_size,要麼就是過渡索引了,把緩存佔滿了。比較理想的設置:

   Key_blocks_used / (Key_blocks_unused + Key_blocks_used) * 100% ≈ 80%
  四、臨時表

   mysql> show global status like 'created_tmp%';
  +-------------------------+---------+

  | Variable_name | Value |

  +-------------------------+---------+

  | Created_tmp_disk_tables | 21197 |

  | Created_tmp_files | 58 |

  | Created_tmp_tables | 1771587 |

  +-------------------------+---------+

  每次創建臨時表,Created_tmp_tables增加,如果是在磁盤上創建臨時表,Created_tmp_disk_tables也增加,Created_tmp_files表示MySQL服務創建的臨時文件文件數,比較理想的配置是: Created_tmp_disk_tables / Created_tmp_tables * 100% <= 25%
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章