如何巧妙解决 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. 另外,该参数也不是设置得越大越好,需要评估物理资源并配合压测情况得出一个合适的值,否则程序异常死锁导致不断申请新连接产生的连接数异常打满时,可能直接把数据库搞挂了。
内容概要:本文《2025年全球AI Coding市场洞察研究报告》由亿欧智库发布,深入分析了AI编程工具的市场现状和发展趋势。报告指出,AI编程工具在2024年进入爆发式增长阶段,成为软件开发领域的重要趋势。AI编程工具不仅简化了代码生成、调试到项目构建等环节,还推动编程方式从人工编码向“人机协同”模式转变。报告详细评估了主流AI编程工具的表现,探讨了其商业模式、市场潜力及未来发展方向。特别提到AI Agent技术的发展,使得AI编程工具从辅助型向自主型跃迁,提升了任务执行的智能化和全面性。报告还分析了AI编程工具在不同行业和用户群体中的应用,强调了其在提高开发效率、减少重复工作和错误修复方面的显著效果。最后,报告预测2025年AI编程工具将在精准化和垂直化上进一步深化,推动软件开发行业进入“人机共融”的新阶段。 适合人群:具备一定编程基础,尤其是对AI编程工具有兴趣的研发人员、企业开发团队及非技术人员。 使用场景及目标:①了解AI编程工具的市场现状和发展趋势;②评估主流AI编程工具的性能和应用场景;③探索AI编程工具在不同行业中的具体应用,如互联网、金融、游戏等;④掌握AI编程工具的商业模式和盈利空间,为企业决策提供参考。 其他说明:报告基于亿欧智库的专业研究和市场调研,提供了详尽的数据支持和前瞻性洞察。报告不仅适用于技术从业者,也适合企业管理者和政策制定者,帮助他们在技术和商业决策中更好地理解AI编程工具的价值和潜力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值