MySQL 8.0新特性 — 管理端口的使用简介
作者:brightdeng@DBA 发布时间:2024-01-28 21:53:46
目录
前言
连接管理
额外连接
管理端口
总结
前言
下面这个报错,相信大多数童鞋都遇见过;那么碰到这个问题,我们应该怎么办呢?在MySQL 5.7及之前版本,出现“too many connection”报错,超级用户root也无法登录上去,除了重启实例,没有其他更好的解决办法;不过在MySQL 8.0版本中,是对连接管理做了一些优化,下面我们就来看一下。
ERROR 1040 (HY000): Too many connections
连接管理
在MySQL 8.0版本中,对连接管理这一块,是先后做了两个比较大的改变:一个是允许额外连接,另一个是专用的管理端口。
额外连接
在MySQL 8.0版本中,在当前连接数达到最大连接数时,服务端允许1个额外连接,可以让具有CONNECTION_ADMIN权限的用户连接进来,下面简单测试一下。
(1)为了方便测试,先调整最大连接数
mysql> set global max_connections=3;
Query OK, 0 rows affected (0.00 sec)
(2)多开几个会话,以达到最大连接数
mysql> show processlist;
+----+-----------------+-----------------+------+---------+--------+------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------------+------+---------+--------+------------------------+------------------+
| 15 | event_scheduler | localhost | NULL | Daemon | 154190 | Waiting on empty queue | NULL |
| 54 | root | localhost | NULL | Query | 0 | starting | show processlist |
| 55 | test | 127.0.0.1:59120 | NULL | Sleep | 19 | | NULL |
| 56 | test | 127.0.0.1:59136 | NULL | Sleep | 9 | | NULL |
+----+-----------------+-----------------+------+---------+--------+------------------------+------------------+
4 rows in set (0.00 sec)
mysql> show global status like 'threads_connected';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_connected | 3 |
+-------------------+-------+
4 rows in set (0.01 sec)
(3)普通用户test尝试连接,报错too many connections
$ mysql -utest -p -h127.0.0.1 -P10080
Enter password:
ERROR 1040 (08004): Too many connections
(4)超级用户root尝试连接成功
$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 60
Server version: 8.0.20 MySQL Community Server - GPL
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
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>
(5)再次查看当前连接数,为max_connections+1
+----+-----------------+-----------------+------+---------+--------+------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------------+------+---------+--------+------------------------+------------------+
| 15 | event_scheduler | localhost | NULL | Daemon | 155064 | Waiting on empty queue | NULL |
| 54 | root | localhost | NULL | Query | 0 | starting | show processlist |
| 55 | test | 127.0.0.1:59120 | NULL | Sleep | 893 | | NULL |
| 56 | test | 127.0.0.1:59136 | NULL | Sleep | 883 | | NULL |
| 60 | root | localhost | NULL | Sleep | 141 | | NULL |
+----+-----------------+-----------------+------+---------+--------+------------------------+------------------+
5 rows in set (0.00 sec)
mysql> show global status like 'threads_connected';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_connected | 4 |
+-------------------+-------+
4 rows in set (0.00 sec)
(6)超级用户root再次尝试连接,也报错too many connections
$ mysql -uroot -p
Enter password:
ERROR 1040 (HY000): Too many connections
通过上面测试可知,在MySQL 8.0中,允许的连接数为max_connections+1,其中这1个额外连接,只允许具有CONNECTION_ADMIN权限的用户使用。通过这1个额外连接,DBA可以使用超级用户root连接,进行kill会话等管理操作,以避免直接重启实例,降低成本,提高效率。
管理端口
额外连接,在一定程度上,提供了出现too many connection问题时的临时解决手段,但额外数量只有1个,难免会有一些意外,出现类似"连接被抢用"、“终端异常掉线”等情况。因此,在MySQL 8.0.14版本中,又推出了一个非常重要的新特性——管理端口;它允许具有SERVICE_CONNECTION_ADMIN权限的用户,通过特定的IP和PORT连接上来,且没有连接数限制。
(1)先介绍下相关参数
admin_address:监听IP地址
admin_port:监听端口
create_admin_listener_thread:是否创建一个单独的线程来监听管理连接
(2)通过配置上述参数,即可启用管理端口
mysql> show global variables like 'admin%';
+---------------+-----------+
| Variable_name | Value |
+---------------+-----------+
| admin_address | 127.0.0.1 |
| admin_port | 33062 |
+---------------+-----------+
2 rows in set (0.00 sec)
# netstat -lntp | grep 33062
tcp 0 0 127.0.0.1:33062 0.0.0.0:* LISTEN 20042/mysqld
(3)接下来进行测试
mysql> show processlist;
+----+-----------------+-----------------+------+---------+--------+------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------------+------+---------+--------+------------------------+------------------+
| 15 | event_scheduler | localhost | NULL | Daemon | 168750 | Waiting on empty queue | NULL |
| 54 | root | localhost | NULL | Query | 0 | starting | show processlist |
| 55 | test | 127.0.0.1:59120 | NULL | Sleep | 14579 | | NULL |
| 56 | test | 127.0.0.1:59136 | NULL | Sleep | 14569 | | NULL |
+----+-----------------+-----------------+------+---------+--------+------------------------+------------------+
4 rows in set (0.00 sec)
mysql> show global status like 'threads_connected';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_connected | 3 |
+-------------------+-------+
1 row in set (0.00 sec)
(4)普通用户test尝试连接,报错too many connections
$ mysql -utest -p -h127.0.0.1 -P10080
Enter password:
ERROR 1040 (08004): Too many connections
(5)超级用户root尝试通过管理端口连接成功
$ mysql -uroot -p -h127.0.0.1 -P33062
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 62
Server version: 8.0.20 MySQL Community Server - GPL
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
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>
(6)继续多开几个会话,使用超级用户root,通过管理端口连接成功,不受最大连接数max_connections限制
mysql> show processlist;
+----+-----------------+-----------------+------+---------+--------+------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------------+------+---------+--------+------------------------+------------------+
| 15 | event_scheduler | localhost | NULL | Daemon | 169035 | Waiting on empty queue | NULL |
| 54 | root | localhost | NULL | Query | 0 | starting | show processlist |
| 55 | test | 127.0.0.1:59120 | NULL | Sleep | 14864 | | NULL |
| 56 | test | 127.0.0.1:59136 | NULL | Sleep | 14854 | | NULL |
| 62 | root | 127.0.0.1:47660 | NULL | Sleep | 151 | | NULL |
| 63 | root | 127.0.0.1:47760 | NULL | Sleep | 52 | | NULL |
| 64 | root | 127.0.0.1:47768 | NULL | Sleep | 43 | | NULL |
| 65 | root | 127.0.0.1:47780 | NULL | Sleep | 35 | | NULL |
| 66 | root | 127.0.0.1:47790 | NULL | Sleep | 24 | | NULL |
| 67 | root | 127.0.0.1:47800 | NULL | Sleep | 16 | | NULL |
| 68 | root | 127.0.0.1:47808 | NULL | Sleep | 8 | | NULL |
+----+-----------------+-----------------+------+---------+--------+------------------------+------------------+
11 rows in set (0.00 sec)
mysql> show global status like 'threads_connected';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_connected | 10 |
+-------------------+-------+
1 row in set (0.00 sec)
可以说,有了管理端口这个新功能,DBA再也不用担心too many connections的问题。
总结
在MySQL 8.0版本中,为了应对too many connections的场景,先后推出了额外连接和管理端口两个新功能,可以让DBA方便、快速地解决问题;不过,这始终是一个临时应急手段,最根本的原因还是要排查应用端的配置(并发限流、SQL性能、连接池配置等等),以彻底规避此类问题。
来源:https://cloud.tencent.com/developer/inventory/2098/article/1708462


猜你喜欢
- 下面的例子演法了怎么样从协程里返回一个值:import asyncioasync def coroutine(): print(
- 本文实例讲述了Flask框架中request、请求钩子、上下文用法。分享给大家供大家参考,具体如下:request就是flask中代表当前请
- 一、re.findall函数介绍它在re.py中有定义:def findall(pattern, string, flags=0): &nb
- 冒泡排序:顾名思义就是(较小的值)像泡泡一样往上冒,(大的值)往下沉。实现原理:依次将相邻两个数值进行比较,较小的数值移到左边,较大的数值移
- 1.1 什么是Mysql多实例?简单的说,Mysql多实例就是在一台服务器上同时开启多个不同的服务端口(如 : 3306/3307/3308
- 柱形图bar()函数绘制柱形图import matplotlib.pyplot as plx = [1,2,3,4,5,6,7]y = [1
- 本文实例为大家分享了python实现K折交叉验证的具体代码,供大家参考,具体内容如下用KNN算法训练iris数据,并使用K折交叉验证方法找出
- 1.包: package PaintBrush; /** * * @author lucifer */ public class Paint
- 迭代是Python最强大的功能之一,是访问集合元素的一种方式。迭代器是一个可以记住遍历的位置的对象。迭代器对象从集合的第一个元素开始访问,直
- 本次分析一下Logger.info的流程1. Logger.info源码: def info(self, msg, *args, **kwa
- Python中可以使用for循环实现累加求和for循环语法:for 变量 in range(x):循环需要执行的代码如下实现1到n求和:de
- Windows下MySQL的安装和删除,供大家参考,具体内容如下安装Mysql1 下载mysql下载地址1;下载地址22 安装教程2.1配置
- 李开复曾经在年前表示,谷歌公司会在今年春节期间,对Google谷歌的搜索结果页进行“从未有过的尝试”修改,以凸显农历春节的喜庆气氛。现在,输
- google 的设计原则中文1.易用性-聚焦在人,方便他们的生活,工作,梦想。2.速度-分秒必争3.简单-简单而强有力4.关联性- 对初学者
- 1、split()含义:split() 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔 num+1 个子字符串
- 前言本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。以下文章来源于Python进击者 ,
- 1.问题现象go编译的时候报错import cycle not allowedcycle意思很简单就是循环的意思。代表的就是一个包被循环的导
- asp如何用Jmail的发送电子邮件?asp源码见下:<% Set mail1
- Jquery中的一些东西学习一下子,补充完善一下,毕竟有些时候没有使用到这个方式很有用,在使用bootstrap table的时候,选择当前
- 1.前言前面学完了SQL Server的基本语法,接下来学习如何在程序中使用sql,毕竟不能在程序中使用的话,实用性就不那么大了。2.最基本