1. 程式人生 > >解決MySQL:1040 "too many connectons" (show variables like '%max_connections%';結果為:214)

解決MySQL:1040 "too many connectons" (show variables like '%max_connections%';結果為:214)

最近MySQL總是報:1040 too many connections 的錯誤,怎麼折騰配置檔案,調大連線數,增加:open_files_limit = 65535等等。還是會報錯誤,進入資料庫查詢:

mysql> show variables like '%max_connections%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 214   |
+-----------------+-------+
1 row in set (0.01 sec)

發現最大連線數總是變成了214。猜測應該不是配置檔案的問題。

由於我這裡有Ubuntu和centos2種系統環境,現在將我的操作整理下:

CentOS 7.5:

vim /usr/lib/systemd/system/mysqld.service

Ubuntu16:

sudo vim /lib/systemd/system/mysql.service

開啟檔案後服務配置檔案後,配置檔案底部加入或替換以下兩行設定:

LimitNOFILE=65535

LimitNPROC=65535

vim /lib/systemd/system/mysql.service 
# MySQL systemd service file

[Unit]
Description=MySQL Community Server
After=network.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql
PermissionsStartOnly=true
ExecStartPre=/usr/share/mysql/mysql-systemd-start pre
ExecStart=/usr/sbin/mysqld
ExecStartPost=/usr/share/mysql/mysql-systemd-start post
TimeoutSec=600
Restart=on-failure
RuntimeDirectory=mysqld
RuntimeDirectoryMode=755
LimitNOFILE=65535
LimitNPROC=65535

儲存並退出,再讓系統重新載入新配置:

CentOS 7:

systemctl daemon-reload

systemctl restart mysqld.service

Ubuntu:

sudo systemctl daemon-reload

sudo service mysql restart

完成後應該就能解決問題了。

三、驗證

mysql> show variables like '%max_connections%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 1000  |
+-----------------+-------+
1 row in set (0.00 sec)