このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン | |||
02_freebsd:04_database:01_mysql:20_wait_timeout_thread_cache_size [2015/12/17 06:26] – matsui | 02_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 ' | ||
+ | +---------------+-------+ | ||
+ | | Variable_name | Value | | ||
+ | +---------------+-------+ | ||
+ | | wait_timeout | ||
+ | +---------------+-------+ | ||
+ | 1 row in set (0.00 sec) | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code |my.cnf> | ||
+ | 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 ' | ||
+ | +-------------------+-------+ | ||
+ | | Variable_name | ||
+ | +-------------------+-------+ | ||
+ | | thread_cache_size | 0 | ||
+ | +-------------------+-------+ | ||
+ | 1 row in set (0.02 sec) | ||
+ | |||
+ | mysql> | ||
+ | +-------------------+---------+ | ||
+ | | Variable_name | ||
+ | +-------------------+---------+ | ||
+ | | Threads_cached | ||
+ | | Threads_connected | 79 | | ||
+ | | Threads_created | ||
+ | | Threads_running | ||
+ | +-------------------+---------+ | ||
+ | 4 rows in set (0.01 sec) | ||
+ | |||
+ | mysql> show global variables like ' | ||
+ | +--------------------+-------+ | ||
+ | | Variable_name | ||
+ | +--------------------+-------+ | ||
+ | | max_connect_errors | 10 | | ||
+ | | max_connections | ||
+ | +--------------------+-------+ | ||
+ | 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> | ||
+ | mysql> | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== thread_cache_sizeの考察 ===== | ||
+ | |||
+ | [[http:// | ||
+ | |||
+ | show status like ' | ||
+ | set @Threads_created=< | ||
+ | show status like ' | ||
+ | set @Connections=< | ||
+ | select 100 - (( @Threads_created / @Connections ) * 100) as " | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | 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> |