iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >MySQL 8.0新特性 — 管理端口的使用简介
  • 922
分享到

MySQL 8.0新特性 — 管理端口的使用简介

MySQL8.0新特性MySQL管理端口 2022-05-27 06:05:36 922人浏览 安东尼
摘要

前言 下面这个报错,相信大多数童鞋都遇见过;那么碰到这个问题,我们应该怎么办呢?在Mysql 5.7及之前版本,出现“too many connection”报错,超级用户root也无法登录上去,除了重启实例,没有

前言

下面这个报错,相信大多数童鞋都遇见过;那么碰到这个问题,我们应该怎么办呢?在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性能、连接池配置等等),以彻底规避此类问题。

以上就是MySQL 8.0新特性 — 管理端口的使用简介的详细内容,更多关于MySQL 8.0新特性 — 管理端口的资料请关注自学编程网其它相关文章!

您可能感兴趣的文档:

--结束END--

本文标题: MySQL 8.0新特性 — 管理端口的使用简介

本文链接: https://www.lsjlt.com/news/10227.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • oracle怎么显示表的字段
    如何显示 oracle 表的字段 在 Oracle 数据库中,可以使用 DESC 命令显示表的字段。 语法: DESC table_name 参数: table_name:要显示字段的表...
    99+
    2024-05-14
    oracle
  • oracle怎么看所有的表
    在 oracle 数据库中查看所有表的步骤:连接到数据库运行查询:select table_name from user_tables; 如何使用 Oracle 查看所有表 ...
    99+
    2024-05-14
    oracle
  • oracle怎么显示行数
    如何使用 oracle 显示行数 在 Oracle 数据库中,有两种主要方法可以显示行数: 1. 使用 COUNT 函数 SELECT COUNT(*) FROM table_n...
    99+
    2024-05-14
    oracle
  • oracle怎么显示百分比
    oracle中显示百分比的方法有:使用百分号“%”;使用to_char()函数;使用format()函数(oracle 18c及更高版本);创建自定义函数。 Oracle 显...
    99+
    2024-05-14
    oracle
  • oracle怎么删除列
    oracle 中删除列的方法有两种:1)使用 alter table table_name drop column column_name 语句;2)使用 drop colum...
    99+
    2024-05-14
    oracle
  • sql怎么查看表的索引
    通过查询系统表,可以获取表的索引信息,包括索引名称、是否唯一、索引类型、索引列和行数。常用系统表有:mysql 的 information_schema.statistics、postg...
    99+
    2024-05-14
    mysql oracle
  • sql怎么查看索引
    您可以使用 sql 通过以下方法查看索引:show indexes 语句:显示表中定义的索引列表及其信息。explain 语句:显示查询计划,其中包含用于执行查询的索引。informat...
    99+
    2024-05-14
  • sql怎么查看存储过程
    如何查看 sql 存储过程的源代码:使用 show create procedure 语句直接获取创建脚本。查询 information_schema.routines 表的 routi...
    99+
    2024-05-14
  • sql怎么查看视图表
    要查看视图表,可以使用以下步骤:使用 select 语句获取视图中的数据。使用 desc 语句查看视图的架构。使用 explain 语句分析视图的执行计划。使用 dbms 提供...
    99+
    2024-05-14
    oracle python
  • sql怎么查看创建的视图
    可以通过sql查询查看已创建的视图,具体步骤包括:连接到数据库并执行查询select * from information_schema.views;查询结果将显示视图的名称、...
    99+
    2024-05-14
    mysql
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作