未命名设计(1).jpg
我直接在标题写出这个错误原因,是因为我希望让读者搜索到一个“快速”的解决方法,
话不多说,我们直接来看怎么处理这个问题。
很多开发人员都会遇见”MySQL: ERROR 1040: Too many connections”的异常情况,造成这种情况的一种原因是访问量过高,MySQL服务器抗不住,这个时候就要考虑增加从服务器分散读压力;另一种原因就是MySQL配置文件中max_connections值过小。
首先,我们来查看mysql的最大连接数:
mysql> show variables like '%max_connections%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set (0.00 sec)
其次,查看服务器响应的最大连接数:
mysql> show global status like 'Max_used_connections';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| Max_used_connections | 2 |
+----------------------+-------+
1 row in set (0.00 sec)
在这里我们可以看到服务器响应的最大连接数为2,远远低于mysql服务器允许的最大连接数值。我们来算一下比例,2/150 ~=1.55%,这么小的比例,很明显给服务器在面对大批量的请求连接时会出现崩溃状态,失去响应。
对于mysql服务器最大连接数值的设置范围比较理想的是:服务器响应的最大连接数值占服务器上限连接数值的比例值在10%以上,如果在10%以下,说明mysql服务器最大连接上限值设置过高。
Max_used_connections / max_connections * 100% = 2/151 *100% ≈ 1%
我们来修改一下
mysql> show variables like '%max_connections%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set (0.19 sec)
mysql> show global status like 'Max_used_connections';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| Max_used_connections | 44 |
+----------------------+-------+
1 row in set (0.17 sec)
这里的最大连接数占上限连接数的30%左右。
上面我们知道怎么查看mysql服务器的最大连接数值,并且知道了如何判断该值是否合理,下面我们就来介绍一下如何设置这个最大连接数值。
方法1:
mysql> set GLOBAL max_connections=256;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%max_connections%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 256 |
+-----------------+-------+
1 row in set (0.00 sec)
方法2:
修改mysql配置文件my.cnf,在[mysqld]段中添加或修改max_connections值:
max_connections=128
重启mysql服务即可。
附。docker启动MySql的yml文件
version: '3'
services:
mysql:
image: mysql
ports:
- 3306:3306
volumes:
- /data/worker_data/db/:/var/lib/mysql
- /data/apps/scheduler/deploy/prod/my.cnf:/etc/mysql/my.cnf
- /data/apps/scheduler/deploy/prod/sql:/data/sql
environment:
- MYSQL_DATABASE=scheduler_test
- MYSQL_ROOT_PASSWORD=root
networks:
- spiders_net
networks:
spiders_net:
external: true
下面看看cnf文件
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
skip-name-resolve
max_connections=1000
max_user_connections=500
wait_timeout=200
# Custom config should go here
!includedir /etc/mysql/conf.d/