MySQL: ERROR 1040: Too many connections

未命名設(shè)計(1).jpg

我直接在標(biāo)題寫出這個錯誤原因,是因為我希望讓讀者搜索到一個“快速”的解決方法,

話不多說,我們直接來看怎么處理這個問題。

很多開發(fā)人員都會遇見”MySQL: ERROR 1040: Too many connections”的異常情況,造成這種情況的一種原因是訪問量過高,MySQL服務(wù)器抗不住,這個時候就要考慮增加從服務(wù)器分散讀壓力;另一種原因就是MySQL配置文件中max_connections值過小。

首先,我們來查看mysql的最大連接數(shù):

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

其次,查看服務(wù)器響應(yīng)的最大連接數(shù):

mysql> show global status like 'Max_used_connections';
+----------------------+-------+
| Variable_name    | Value |
+----------------------+-------+
| Max_used_connections | 2   |
+----------------------+-------+
1 row in set (0.00 sec)

在這里我們可以看到服務(wù)器響應(yīng)的最大連接數(shù)為2,遠(yuǎn)遠(yuǎn)低于mysql服務(wù)器允許的最大連接數(shù)值。我們來算一下比例,2/150 ~=1.55%,這么小的比例,很明顯給服務(wù)器在面對大批量的請求連接時會出現(xiàn)崩潰狀態(tài),失去響應(yīng)。

對于mysql服務(wù)器最大連接數(shù)值的設(shè)置范圍比較理想的是:服務(wù)器響應(yīng)的最大連接數(shù)值占服務(wù)器上限連接數(shù)值的比例值在10%以上,如果在10%以下,說明mysql服務(wù)器最大連接上限值設(shè)置過高。

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)

這里的最大連接數(shù)占上限連接數(shù)的30%左右。
上面我們知道怎么查看mysql服務(wù)器的最大連接數(shù)值,并且知道了如何判斷該值是否合理,下面我們就來介紹一下如何設(shè)置這個最大連接數(shù)值。

方法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服務(wù)即可。

附。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/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容