最近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。猜测应该不是配置文件的问题。
去网上搜索了一圈,参考这篇文章:https://blog.csdn.net/luckykapok918/article/details/79037091
由于我这里有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)