1. 程式人生 > >“Too many connections”引起MySQL崩潰並啟動失敗

“Too many connections”引起MySQL崩潰並啟動失敗

using socket tail lob loaded emd 登錄 報錯 conf

問題描述:

在部署一套新環境的時候,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
a— 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=1000 ,然後重啟MySQL服務即可.

方法二:命令行修改(臨時生效)
命令行登錄MySQL後。設置新的MySQL最大連接數為1000:
MySQL> set global max_connections=200;
方法二在命令行設置的最大連接數只在mysql當前服務進程有效,一旦mysql重啟,又會恢復到初始狀態。
因為mysql啟動後的初始化工作是從其配置文件中讀取數據的,而這種方式沒有對其配置文件做更改。

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

“Too many connections”引起MySQL崩潰並啟動失敗