1. 程式人生 > >JavaWeb問題集錦: CentOS 7中MySQL連線數被限制為214個的解決方法

JavaWeb問題集錦: CentOS 7中MySQL連線數被限制為214個的解決方法

這篇文章主要給大家介紹了關於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 能夠支援的最大連線數量受限於作業系統,必要時可以增大 open-files-limit。換言之,連線數與檔案開啟數有關。

解決方法

[[email protected] ~]# 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

參考

https://dev.mysql.com/doc/refman/5.7/en/too-many-connections.html

https://www.oschina.net/question/853151_241231