Too many connections
refer to : https://dev.mysql.com/doc/refman/8.4/en/too-many-connections.html
If clients encounter Too many connections errors when attempting to connect to the mysqld server, all available connections are in use by other clients.
1.使用SHOW PROCESSLIST
,来查看,当前有哪些连接
5 | event_scheduler | localhost | Daemon | 315119 | Waiting on empty queue | ||
---|---|---|---|---|---|---|---|
6237 | root | 218.94.128.34:41882 | pcm | Sleep | 3568 | ||
6238 | root | 218.94.128.34:36816 | office_db | Sleep | 3541 | ||
6240 | root | 218.94.128.34:60470 | office_db | Sleep | 3434 | ||
6241 | root | 218.94.128.34:47912 | pcm | Sleep | 3384 | ||
6242 | root | 218.94.128.34:59582 | office_db | Sleep | 3370 | ||
6243 | root | 218.94.128.34:59598 | pcm | Sleep | 3366 | ||
6245 | root | 218.94.128.34:40862 | office_db | Sleep | 3333 | ||
6246 | root | 218.94.128.34:46788 | office_db | Sleep | 3327 | ||
6298 | root | 114.221.154.200:42042 | pcm | Sleep | 65 | ||
6299 | root | 114.221.154.200:42043 | pcm | Query | 0 | init | SHOW PROCESSLIST |
6300 | root | 218.94.128.34:41470 | pcm | Sleep | 32 | ||
2.使用show variables like 'max_connections';
,来查看,配置中,允许的最大连接数
max_connections | 1000 |
---|---|
可以看到,最大连接数是1000
3.使用show variables like 'wait_timeout';
和show variables like 'interactive_timeout';
,来查看,超时时间
4.在/etc/my.cnf
中,来设置上面3个配置项的值,如下:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
interactive_timeout=3600
wait_timeout=3600
max_connections=1000
设置interactive_timeout为3600秒,wait_timeout为3600秒,max_connections为1000
SHOW PROCESSLIST & information_schema.processlist
上面,我们使用了SHOW PROCESSLIST
命令,查看了当前有哪些连接。
如果我们想在这个基础上,统计出连接数 ,或者 是统计有来自于某个ip有哪些连接数,就可以使用information_schema.processlist这个表,来做条件查询。
select count(*) from information_schema.processlist;
select count(*) from information_schema.processlist where Host like '218.94%';
select count(*),DB from information_schema.processlist where Host like '218.94%' group by DB;
select * from information_schema.processlist where Host like '218.94%' ;