출처: https://qiita.com/hmarf/items/c2350ce717222c3a288c
MySQL에서 too many connections 에러가 나온다면 확인해야 할 것
too many connection 에러가 나온다면 MySQLのmax connections, processlist, wait_timeout 을 확인한다.
# max_connections 수 확인
mysql> show variables like "%max_connections%";
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 150 |
+-----------------+-------+
1 row in set (0.00 sec)
# 접속되어 있는 프로세스를 확인
mysql> show processlist;
+----+------+------------------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+------------------+------+---------+------+----------+------------------+
| 37 | root | localhost | NULL | Sleep | 6090 | | NULL |
| 38 | user | xxx.xx.x.x | NULL | Sleep | 347 | | NULL |
| 39 | user | xxx.xx.x.x | NULL | Sleep | 347 | | NULL |
| 55 | root | localhost | NULL | Query | 0 | starting | show processlist |
+----+------+------------------+------+---------+------+----------+------------------+
4 rows in set (0.01 sec)
# time 이 긴 것을 삭제
mysql> kill 37;
Query OK, 0 rows affected (0.00 sec)
# wait timeout 을 확인
mysql> show global variables like 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout | 28800 |
+---------------+-------+
1 row in set (0.01 sec)
대처 방법
max connection 수를 올린다 & wait timeout 을 제한한다
먼저 max connection, wait timeout 을 변경한다.
그러나 안이하게 Connection 수를 올리면 메모리 부족을 발생시킨다.
아래의 식으로 메모리 계산을 할 수 있다
메모리 사용량 = 글로벌 버퍼 + (스레드 버퍼 * 연결 수)
실행 중의 서버 내에서 변경하는 경우
mysql> set global max_connections = 1000;
mysql> set global wait_timeout = 1800;
설정 파일로 변경하는 경우
[mysqld]
max_connections = 1000
wait_timeout = 1800
amazon RDS 에서는 max connection을 올릴 수 없었던 것 같다. AWS에서는 사용하고 있는 인스턴스 메모리에서 최적의 max connection 를 자동으로 계산하는 것 같다.
일시적으로 올라가지만 점차 감소한다.
client에서 connection 수를 제한한다
아래의 Go 언어를 예를 든다.
DB.SetMaxOpenConns(100)
위의 1행 만으로 끝난다.