====== 20 wait_timeoutとthread_cache_size ======
アプリケーション側でエラーとなり、MySQLへのコネクションをが残っていて
どんどんconnectionが残り続け、結果connectionが溢れてしまう事がある。
===== wait_timeout =====
デフォルトでは、MySQL側でconnectionを8時間保持し続けます。
mysql> show global variables like 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout | 28800 |
+---------------+-------+
1 row in set (0.00 sec)
wait_timeout = 360
===== thread_cache_size =====
wait_timeout を減らすと、その分スレッドをthreadを生成してThreads_createdが増えていくし、
connectしたりするオーバーヘッドも大きくなります。
そこでthread_cache_sizeを増やしてやると、オーバヘッドがなくなります。
thread_cache_sizeは、max_connectionsと同じくらいで良いと思う??
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)
|Threads_cached|キャッシュされているスレッド数(スレッドは使いまわされる)|
|Threads_connected|現在の接続数|
|Threads_created|接続を処理するために生成されたスレッド数 \\ (この値が増えまくるなら、cached が足りていない)|
|Threads_running|スリープ状態になっていないスレッドの数|
thread_cache_size = 100
===== オンラインで反映 =====
wait_timeoutとthread_cache_sizeは、MySQLを再起動せずに反映させる事ができます。
mysql>SET GLOBAL wait_timeout = 360;
mysql>SET GLOBAL thread_cache_size = 100;
===== 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}}