CentOS 7中MySQL连接数被限制为214个的解决方法
发现问题
最近在项目中遇到一个问题,由于连接数过多,提示 “too many connections” ,需要增加连接数。
我在 /etc/my.cnf中修改了:
max_connections = 2000
但是, 实际连接数一直被限制在 214:
mysql> show variables like "max_connections"; +-----------------+-------+ | variable_name | value | +-----------------+-------+ | max_connections | 214 | +-----------------+-------+ 1 row in set
思考
如果我设置连接小于214时,比如 200,那么实际连接数就是 200,也就是说,我的配置文件是没有问题的。
查 mysql 官方文档,里面说了:
the maximum number of connections mysql can support depends on the quality of the thread library on a given platform, the amount of ram available, how much ram is used for each connection, the workload from each connection, and the desired response time. linux or solaris should be able to support at 500 to 1000 simultaneous connections routinely and as many as 10,000 connections if you have many gigabytes of ram available and the workload from each is low or the response time target undemanding. windows is limited to (open tables × 2 + open connections) < 2048 due to the posix compatibility layer used on that platform.
increasing open-files-limit may be necessary. also see section 2.5, “installing mysql on linux”, for how to raise the operating system limit on how many handles can be used by mysql.
大概意思是 mysql 能够支持的最大连接数量受限于操作系统,必要时可以增大 。换言之,连接数与文件打开数有关。
解决方法
[root@sqzr ~]# ulimit -n 1024
可知,操作系统最大文件描述符限制为 1024。
更改 mysql 在 linux 的最大文件描述符限制,编辑 /usr/lib/systemd/system/mysqld.service
文件,在文件最后添加:
limitnofile=65535 limitnproc=65535
保存后,执行下面命令,使配置生效
$ systemctl daemon-reload $ systemctl restart mysqld.service
实际连接数到 2000 了,解决
mysql> show variables like "max_connections"; +-----------------+-------+ | variable_name | value | +-----------------+-------+ | max_connections | 2000 | +-----------------+-------+ 1 row in set
参考
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。