Mysql too many conntions 问题解决以及优化

to many connections
  • 报错信息
Too many connections
  • 原因
连接数超过mysql设置的最大线程数
    1.一般是连接泄露导致
    2.也可能是mysql max_connections 设置过小导致
  • 解决

手动断开连接

step 1  查看最大的链接
    SELECT substring_index(host, ':',1) AS host_name,state,count(*) FROM information_schema.processlist GROUP BY state, host_name order by host_name;
step 2 查看进程
    show processlist ;
    Id          user    host:port      command  status Time info
    15289454,apiserver,10.244.0.21:32918,apiserver,Sleep,29,"",
    15290359,apiserver,10.244.0.21:44534,apiserver,Sleep,29,"",
    15291995,apiserver,10.244.0.21:35212,apiserver,Sleep,29,"",
    15365612,apiserver,10.244.0.162:44682,apiserver,Sleep,5,"",
    22508742,apiserver,10.244.0.196:32842,apiserver,Sleep,33,"",
    22508744,apiserver,10.244.0.189:52898,apiserver,Sleep,33,"",

step 3 kill 
    kill processid

设置最大连接数

查看设置的最大连接数
show variables like "max_connections";

查看连接失效时间
show global variables like 'wait_timeout' ;

查看当前连接数
show status like 'Threads%';

设置最大连接(暂时)
set global max_connections=500;

设置最大连接数(永久)修改mysql配置文件
max_connections=500

排查连接泄露

根据用户筛选
select USER , count(*) from information_schema.processlist group by USER;

根据主机名
SELECT substring_index(host, ':',1) AS host_name,state,count(*) FROM information_schema.processlist GROUP BY state, host_name order by host_name;
  • 优化,合理的根据服务器配置设置连接数
max_connections:最大连接数(151 默认值)

max_user_connections:用户最大连接数
wait_timeout 连接失效时间

而一个连接会消耗大约2M左右的内存
wait_timeout的值如果设置的比较小,连接会不停的创建和销毁,这样会浪费一定的IO资源

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值