MySQL 修改最大连接数

今天使用ide连接线下MySQL报错Can not connect to MySQL server. Too many connections,报错很明确,与MySQL的连接数满了。想想也是,每起一个服务都会创建MySQL连接池,占用不少的长连接。用ide查看了一下,原来最大连接数才151,看来有必要改大一点了。

上网查了一下,修改方式有两种

1.命令行修改

进入mysql后,set GLOBAL max_connections=1024; 即可立即生效,但是博主没有使用这种方式,因为这种方法治标不治本,一点重启mysql,最大连接数又会变回151

2.修改配置,然后重启

vi /etc/m.cnf加入max_connections=1024,然后重启mysql即可。

重启后,很遗憾,max_connections变成了214,这就很诡异了。我把max_connections又分别设置成500和213,实际的max_connections分别是214和213。也就是说,在这台服务器上,max_connections最大只能是234,猜测是因为操作系统的限制导致max_connections最大只能为213。博主翻了翻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. 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.

以我的英语水准,把上述语句概括起来就是,max_connections依托于操作系统,Linux系统必要时需要增加open-files-limit。万万没想到啊,修改max_connections竟然要修改操作系统最大文件描述符。

vi /usr/lib/systemd/system/mysqld.service加入

LimitNOFILE=50000

重启MySQL

嗯,搞定了~

Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/5156f61bc5979a75490e8764d164d35e.html