当前位置: 首页 > news >正文

如何巧妙解决 Too many connections 报错?

1. 背景

在日常的 MySQL 运维中,难免会出现参数设置不合理,导致 MySQL 在使用过程中出现各种各样的问题。

今天,我们就来讲解一下 MySQL 运维中一种常见的问题:最大连接数设置不合理,一旦到了业务高峰期就会出现连接数打满的问题。

报错信息

ERROR 1040 (HY000): Too many connections

报错原因

这个问题,无非就是并发过高,导致最大连接数被用完引起的。

解决办法

重新设置最大连接数 max_connections 的值。

处理痛点

  1. 出现了 Too many connections 的报错信息,无法用 mysql 客户端连接进行动态修改。
  2. 修改配置参数后重启,确实可以让 max_connections 的值重新生效,但是生产环境有业务访问,肯定是不能随便重启的。

2. GDB 工具

基于以上种种问题和主要痛点,那么我们就来试试 gdb 工具吧,让我们在不重启的状态下,照样可以修改 MySQL 的参数 !!!

gdb 工具的介绍在这里就不做过多赘述了,其实大多数场景下,都是把它当做调试工具来用。今天我们更多的是用到 gdb 在线修改配置参数的功能。

3. 处理过程

下面,我们就通过一个实验场景,来模拟 mysql 连接数打满,出现 Too many connections 的问题,通过 gdb 工具的解决步骤。

  1. 准备一个数据库实例,数据库版本为 MySQL 5.7.40。
  2. 创建一个空库 jiangshifeng
mysql> create database jiangshifeng;
Query OK, 1 row affected (0.00 sec)mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| jiangshifeng       |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)mysql> use jiangshifeng;
Database changed
mysql> show tables;
Empty set (0.00 sec)
  1. 设置该实例的最大连接数为 10。
mysql> set global max_connections=10;
Query OK, 0 rows affected (0.01 sec)mysql> show variables like '%max_conn%';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 100   |
| max_connections    | 10    |
+--------------------+-------+
2 rows in set (0.00 sec)
  1. 安装 gdb 工具。
yum install -y gdb

安装完成验证

[root@node1 ~]# gdb
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) 
(gdb) q
[root@node1 ~]# 
  1. 使用 sysbench 进行压测,模拟在 jiangshifeng 这个空库创建 11 张表,每个表的数据量为 20000000 条,连接线程数为 15。
sysbench /usr/share/sysbench/oltp_common.lua --mysql-host=127.0.0.1 --mysql-port=3306 --mysql-user=root --mysql-password=123456 --mysql-db=jiangshifeng --db-driver=mysql --tables=11 --table-size=20000000 --report-interval=15 --threads=15  prepare
  1. 此时,我们再次用客户端工具连接 mysql,就会报错(Too many connections)。
[root@node1 ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1040 (HY000): Too many connections
[root@node1 ~]#
  1. 通过 gdb 工具设置 max_connections 参数。

方法一

gdb -p $(pidof mysqld) -ex "set max_connections=50" -batch

方法二

[root@node1 ~]# ps -ef | grep mysql
root       1320      1  0 Mar17 ?        00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/DB/mysql --pid-file=/DB/mysql/node1.pid
mysql      1582   1320  9 Mar17 ?        03:33:44 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/DB/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=node1.err --pid-file=/DB/mysql/node1.pid --socket=/tmp/mysql.sock --port=3306[root@node1 ~]# gdb -p 1582
...
(gdb) p max_connections
$1 = 10
(gdb) 
(gdb) set max_connections=50
(gdb) p max_connections
$4 = 50
(gdb) 
$5 = 50
(gdb) q
A debugging session is active.Inferior 1 [process 1582] will be detached.Quit anyway? (y or n) y
Detaching from program: /usr/local/mysql/bin/mysqld, process 1582
[Inferior 1 (process 1582) detached]
[root@node1 sysbench]#
  1. 再次登录验证。
[root@node1 ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 146
Server version: 5.7.40-log MySQL Community Server (GPL)Copyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
mysql> show variables like '%max_conn%';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 100   |
| max_connections    | 50    |
+--------------------+-------+
2 rows in set (0.10 sec)

此时,MySQL 最大连接数 max_connections 已经成功修改。并且,前面的 sysbench 压测数据还正常运行,可以理解为基本上不影响业务。

4. 总结

  1. 本次虽然只介绍了通过不重启的方式,用 gdb 修改 max_connections 参数。但是其他参数的修改也类似,可以使用同样的方式。
  2. 尽管在实验中看到了修改参数成功了,但是操作始终有未知风险的,不能总是觉得有了这些工具,一定能够万无一失。
  3. 一定要严格遵守开发规范,数据库的问题,往往是人在使用时的不当操作导致的。例如这个最大连接数,我们要评估好业务最大的并发的峰值,设置成比业务峰值大很多,就能尽量避免出现连接数打满的问题了。
  4. 另外,该参数也不是设置得越大越好,需要评估物理资源并配合压测情况得出一个合适的值,否则程序异常死锁导致不断申请新连接产生的连接数异常打满时,可能直接把数据库搞挂了。

相关文章:

  • 切片和边缘计算技术分析报告
  • 软考错题(一)
  • Android 如何理解 Java JNI 中的引用与 Java 对象应用的区别
  • 机器人跑拉松是商业噱头还是技术进步的必然体现
  • LED实验
  • 数据库 postgresql 修改密码 sh
  • 前端面经-VUE3篇(五)--内置组件
  • 「Mac畅玩AIGC与多模态23」开发篇19 - Markdown 富文本输出工作流示例
  • 【面试 · 一】vue大集合
  • Spring AI介绍、应用场景和示例代码
  • 第2章 算法分析基础
  • 76.评论日记
  • MySQL数据库安装+配置(1遍成功)
  • 数据可视化:php+echarts实现数据可视化(包含echart安装引入)
  • 力扣49. 字母异位词分组(哈希表)
  • DAY03:Vue深度解析之响应式系统与计算属性实战指南
  • 化妆品出口日本需要做什么认证?化妆品出口日本流程,化妆品出口日本介绍
  • Spring Boot 为 MongoDB 自动配置了哪些核心 Bean?
  • 广东省省考备考(第四天5.7)—言语(第二节课)
  • android中的背压问题及解决方案
  • 重庆荣昌出圈背后:把网络流量变成经济发展的增量
  • 山东14家城商行中,仅剩枣庄银行年营业收入不足10亿
  • 江西暴雨强对流明显,专家:落雨区高度重叠,地质灾害风险高
  • 国家发改委:美芯片药品等领域关税影响全球科技发展,损害人类共同利益
  • 体坛联播|曼联热刺会师欧联杯决赛,多哈世乒赛首日赛程出炉
  • 首批18位!苏联籍抗日航空英烈信息更新