暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

MySQL不重启调整参数的操作

164

点击标题下「蓝色微信名」可快速关注

技术社群的这篇文章《如何巧妙解决 Too many connections 报错?》给我们讲解了如何通过不重启调整参数的操作,我们日常测试和运维过程中,可能会碰到的一个场景,值得学习借鉴。

背景

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

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

报错信息

ERROR 1040 (HY000): Too many connections

报错原因

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

解决办法

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

处理痛点

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

GDB 工具

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

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

处理过程

下面,我们就通过一个实验场景,来模拟 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 inset (0.00 sec)

mysql> use jiangshifeng;
Database changed
mysql> show tables;
Empty set (0.00 sec)

3. 设置该实例的最大连接数为 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)

4. 安装 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 ~]

5. 使用 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

6. 此时,我们再次用客户端工具连接 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 ~]#

7. 通过 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]#

8. 再次登录验证。
[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'forhelp. 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 inset (0.10 sec)

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

总结

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


如果您认为这篇文章有些帮助,还请不吝点下文章末尾的"点赞"和"在看",或者直接转发朋友圈,



近期更新的文章:
MySQL的HINT功能
TiDB社区的专栏征文大赛启动
MySQL免费培训和认证的机会
Node.js的环境安装
5月24日IFClub上海站,大咖云集、干货拉满!

热文鉴赏:
揭开"仿宋"和"仿宋_GB2312"的神秘面纱
Linux的"aarch"是多了个"a"?
中国队“自己的”世界杯
你不知道的C罗-Siu庆祝动作
大阪环球影城避坑指南和功略
推荐一篇Oracle RAC Cache Fusion的经典论文
"红警"游戏开源代码带给我们的震撼

文章分类和索引:
公众号1700篇文章分类和索引

文章转载自bisal的个人杂货铺,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论