您的位置:首页 > 数据库 > MySQL

“Too many connections”引起MySQL崩溃并启动失败

2018-08-15 14:22 821 查看

问题描述:

在部署一套新环境的时候,rancher-server上有14个镜像包一起升级,主要是微服务的写入和查询程序,基本上都是需要去连接MySQL的程序。可能是由于大并发连接数据库,最后起来的几个服务就报错了,docker容器启动失败,报的是MySQL的错误,打印了很长一串sql语句,最后有一个“too many connections”
然后登录MySQL查看当前的最大连接数设置,发现MySQL已经无法登录:

[root@host12 ~]# mysql -u root -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1040 (HY000): Too many connections

此时同样需要连接到这个数据库的rancher-server界面也已经无法访问,尝试重启MySQL,重启失败:

[root@host12 ~]# systemctl restart mysqld
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.

执行systemctl status mysqld.service查看报错,发现执行stop这一步成功了,但是start失败,MySQL没有起得来:

[root@host12 ~]# systemctl status mysqld.service
â— mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2018-08-13 17:27:59 CST; 20s ago
Docs: man:systemd-sysv-generator(8)
Process: 30955 ExecStop=/etc/rc.d/init.d/mysqld stop (code=exited, status=0/SUCCESS)
Process: 30992 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=1/FAILURE)

Aug 13 17:27:58 host12 systemd[1]: Starting LSB: start and stop MySQL...
Aug 13 17:27:59 host12 mysqld[30992]: Starting MySQL. ERROR! The server quit without updating PID file (/data/mysqldata/host12.pid).
Aug 13 17:27:59 host12 systemd[1]: mysqld.service: control process exited, code=exited status=1
Aug 13 17:27:59 host12 systemd[1]: Failed to start LSB: start and stop MySQL.
Aug 13 17:27:59 host12 systemd[1]: Unit mysqld.service entered failed state.
Aug 13 17:27:59 host12 systemd[1]: mysqld.service failed.
[root@host12 ~]#

解决过程

systemctl status mysqld.service看到的信息没有提到连接数过多的问题,但是结合rancher-server上docker容器的报错,应该是当前连接数太多导致的。
因为通常MySQL最大连接数的默认值是100,最大可以达到16384。在部署好MySQL之后没有手动修改最大连接数,目前还是默认的状态。
修改配置文件,在/etc/my.cnf文件加入了max_connections=10000这一行(因为不确定到底目前多少连接数,所以一次性改的比较大试试效果)。

[root@host12 mariadb]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysqldata
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
max_connections=10000     #加了这一行
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

[root@12 mariadb]#

再启动MySQL,成功。
然后rancher-server访问正常,上面的服务也起都正常启动。
在启动之后,根据现有环境的配置,将最大连接数改到了2000:

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

mysql>

后记:MySQL查看和修改最大连接数的方法:
1、查看最大连接数
show variables like '%max_connections%';

2、修改最大连接数
方法一:修改配置文件(永久生效)。
在/etc/my.conf文件加入 max_connections=2000 ,然后重启MySQL服务即可.

方法二:命令行修改(临时生效)
命令行登录MySQL后。设置新的MySQL最大连接数为2000:
MySQL> set global max_connections=2000;
方法二在命令行设置的最大连接数只在mysql当前服务进程有效,一旦mysql重启,又会恢复到初始状态。
因为mysql启动后的初始化工作是从其配置文件中读取数据的,而这种方式没有对其配置文件做更改。

如果不能立即重启MySQL,就两种方法一起用。命令行修改后,再一起修改配置文件,下次MySQL重启后,也能生效了

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  MySQL Too many