LoginSignup
3
3

More than 5 years have passed since last update.

Sequelでmysql2アダプタを使っていて「too many connections」とエラーが出た時の対処

Last updated at Posted at 2015-03-27

「Too many connection」エラーが出た

Sequel::DatabaseConnectionError - Mysql2::Error: Too many connections:

mysqlの接続数確認。

$ mysqladmin -u root -proot extended-status | grep -E "Max|Threads"
| Max_used_connections                          | 152         |
| Threads_cached                                | 0           |
| Threads_connected                             | 10          |
| Threads_created                               | 505         |
| Threads_running                               | 1           |

$ mysqladmin processlist -u root -p
+-----+------+-----------+-------------+---------+------+-------+------------------+----------+
| Id  | User | Host      | db          | Command | Time | State | Info             | Progress |
+-----+------+-----------+-------------+---------+------+-------+------------------+----------+
| 19  | user | localhost | db1         | Sleep   | 7856 |       |                  | 0.000    |
| 497 | user | localhost | db1         | Sleep   | 182  |       |                  | 0.000    |
| 498 | user | localhost | db1         | Sleep   | 182  |       |                  | 0.000    |
| 499 | user | localhost | db1         | Sleep   | 164  |       |                  | 0.000    |
| 501 | user | localhost | db1         | Sleep   | 155  |       |                  | 0.000    |
| 502 | user | localhost | db1         | Sleep   | 155  |       |                  | 0.000    |
| 504 | user | localhost | db1         | Sleep   | 155  |       |                  | 0.000    |
| 507 | root | localhost |             | Query   | 0    | init  | show processlist | 0.000    |
+-----+------+-----------+-------------+---------+------+-------+------------------+----------+

以前使ったプロセスがずっと残ってる……

sequelでmysql2アダプタで接続した時のデフォルトのtimeout値は1ヶ月

マニュアルによると、timeoutオプションのデフォルト値は1monthとなっている。

対処法:接続の際にtimeoutオプションを設定する

require 'sequel'
require 'mysql2'
db=Sequel.mysql2(user:"user",password:"pw",database:"db1",charaset:"utf8",timeout:"60")

timeoutオプションを設定する。
引数は秒数。
上記の設定だと、Sleep状態のプロセスは60秒で終了する。

但し、過去に生成したプロセスには効果が無いので、一旦Mysql(Mariadb)サーバを再起動すること。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3