ユーザ用ツール

サイト用ツール


02_freebsd:04_database:01_mysql:20_wait_timeout_thread_cache_size

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
02_freebsd:04_database:01_mysql:20_wait_timeout_thread_cache_size [2015/12/17 06:26] matsui02_freebsd:04_database:01_mysql:20_wait_timeout_thread_cache_size [2017/04/03 05:28] (現在) matsui
行 1: 行 1:
 +====== 20 wait_timeoutとthread_cache_size ======
  
 +アプリケーション側でエラーとなり、MySQLへのコネクションをが残っていて
 +どんどんconnectionが残り続け、結果connectionが溢れてしまう事がある。
 +
 +===== wait_timeout =====
 +
 +デフォルトでは、MySQL側でconnectionを8時間保持し続けます。
 +
 +<code console>
 +mysql> show global variables like 'wait_timeout';
 ++---------------+-------+
 +| Variable_name | Value |
 ++---------------+-------+
 +| wait_timeout  | 28800 |
 ++---------------+-------+
 +1 row in set (0.00 sec)
 +
 +</code>
 +
 +<code |my.cnf>
 +wait_timeout = 360
 +</code>
 +
 +===== thread_cache_size =====
 +
 +wait_timeout を減らすと、その分スレッドをthreadを生成してThreads_createdが増えていくし、
 +connectしたりするオーバーヘッドも大きくなります。
 +そこでthread_cache_sizeを増やしてやると、オーバヘッドがなくなります。
 +thread_cache_sizeは、max_connectionsと同じくらいで良いと思う??
 +
 +<code>
 +mysql> show global variables like 'thread_cache_size';
 ++-------------------+-------+
 +| Variable_name     | Value |
 ++-------------------+-------+
 +| thread_cache_size | 0     
 ++-------------------+-------+
 +1 row in set (0.02 sec)
 +
 +mysql>  show global status like 'Thread_%' ;
 ++-------------------+---------+
 +| Variable_name     | Value   |
 ++-------------------+---------+
 +| Threads_cached    | 0       |
 +| Threads_connected | 79      |
 +| Threads_created   | 6398514 |
 +| Threads_running   | 21      |
 ++-------------------+---------+
 +4 rows in set (0.01 sec)
 +
 +mysql> show global variables like 'max_connect%';
 ++--------------------+-------+
 +| Variable_name      | Value |
 ++--------------------+-------+
 +| max_connect_errors | 10    | 
 +| max_connections    | 100   
 ++--------------------+-------+
 +2 rows in set (0.00 sec)
 +
 +</code>
 +
 +
 +|Threads_cached|キャッシュされているスレッド数(スレッドは使いまわされる)|
 +|Threads_connected|現在の接続数|
 +|Threads_created|接続を処理するために生成されたスレッド数 \\ (この値が増えまくるなら、cached が足りていない)|
 +|Threads_running|スリープ状態になっていないスレッドの数|
 +
 +
 +<code|my.cnf>
 +thread_cache_size = 100  
 +</code>
 +
 +===== オンラインで反映 =====
 +
 +wait_timeoutとthread_cache_sizeは、MySQLを再起動せずに反映させる事ができます。
 +<code>
 +mysql>SET GLOBAL wait_timeout = 360;
 +mysql>SET GLOBAL thread_cache_size = 100;
 +</code>
 +
 +
 +===== thread_cache_sizeの考察 =====
 +
 +[[http://anothermysqldba.blogspot.jp/2013/09/mysql-optimization-tip-threadcachesize.html]]
 +
 +         show status like 'Threads_created';
 +             set @Threads_created=< result from query above>;
 +        show status like 'Connections';
 +             set @Connections=< result from query above>; 
 +        select 100 - (( @Threads_created / @Connections ) * 100) as "Threads_created % of Connections"\G
 +
 +
 +
 +
 +So if your execute the process above what is your percentage ? You want this to be as close to 100 as possible. So for example the server I recently encountered had a % under 10%.  So how do you fix this and raise your %?
 +
 +{{tag>MySQL}}
02_freebsd/04_database/01_mysql/20_wait_timeout_thread_cache_size.txt · 最終更新: 2017/04/03 05:28 by matsui