Administrator
发布于 2024-06-11 / 20 阅读
0
0

Mysql FAQ

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,来查看,当前有哪些连接

5event_schedulerlocalhostDaemon315119Waiting on empty queue
6237root218.94.128.34:41882pcmSleep3568
6238root218.94.128.34:36816office_dbSleep3541
6240root218.94.128.34:60470office_dbSleep3434
6241root218.94.128.34:47912pcmSleep3384
6242root218.94.128.34:59582office_dbSleep3370
6243root218.94.128.34:59598pcmSleep3366
6245root218.94.128.34:40862office_dbSleep3333
6246root218.94.128.34:46788office_dbSleep3327
6298root114.221.154.200:42042pcmSleep65
6299root114.221.154.200:42043pcmQuery0initSHOW PROCESSLIST
6300root218.94.128.34:41470pcmSleep32

2.使用show variables like 'max_connections';,来查看,配置中,允许的最大连接数

max_connections1000

可以看到,最大连接数是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%' ;

评论