故障分析 | MySQL 管理端口登录异常排查及正确使用技巧


letme
letme 2023-07-04 18:41:31 52771
分类专栏: 资讯

1背景描述

MySQL 8.0.14 版本中引入了 admin_port 参数,用于提供一个管理端口来处理 too many connections 报错。最近一套 MySQL 8.0 实例出现 too many connections 报错,尝试通过管理端口登录,但是仍然提示该报错。跟业务部门协商之后,调大了连接数,重启数据库恢复业务。为什么配置了 admin_port 却没有生效呢,带着疑问做了如下测试。

2场景复现

管理端口相关参数

--创建一个单独的 listener 线程来监听 admin 的连接请求
create_admin_listener_thread    = 1           

--监听地址
admin_address = localhost   

--监听端口,默认为 33062,也可以自定义端口
admin_port = 33062        
 
--配置好参数,重启数据库生效
systemctl restart mysqld_3306
 
--测试 root 账号是否可以通过 33062 端口登录
[root@mysql ~]# mysql -uroot -p -S /data/mysql/data/3306/mysqld.sock -P33062 -e 'select version()'
Enter password:
+-----------+
| version() |
+-----------+
| 8.0.33    |
+-----------+

模拟故障现象

调小 max_connections 参数,模拟出现 too many connections 报错。

--更改 max_connections 参数为 1
mysql> set global max_connections = 1;
 
--模拟连接数被打满
[root@mysql ~]# mysql -uroot -p -S /data/mysql/data/3306/mysqld.sock -e 'select version()'
Enter password:
ERROR 1040 (HY000): Too many connections
 
--root 账号使用 33062 端口登录依然报错
[root@mysql ~]# mysql -uroot -p -S /data/mysql/data/3306/mysqld.sock -P33062 -e 'select version()'
Enter password:
ERROR 1040 (HY000): Too many connections

3故障分析

疑问

为啥连接数没打满的情况下,root 账号可以通过 33062 端口登录?

[root@mysql ~]# mysql -uroot -p -S /data/mysql/data/3306/mysqld.sock -P33062
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.33 MySQL Community Server - GPL
 
Copyright (c) 2000, 2023, 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> \s
--------------
mysql  Ver 8.0.33 for Linux on x86_64 (MySQL Community Server - GPL)
 
Connection id:          16
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         8.0.33 MySQL Community Server - GPL
Protocol version:       10
Connection:             Localhost via UNIX socket            --使用的socket连接
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    utf8mb4
Conn.  characterset:    utf8mb4
UNIX socket:            /data/mysql/data/3306/mysqld.sock
Binary data as:         Hexadecimal
Uptime:                 1 hour 6 min 54 sec
 
Threads: 3  Questions: 25  Slow queries: 0  Opens: 142  Flush tables: 3  Open tables: 74  Queries per second avg: 0.006

socket 连接会忽略指定的端口,即便是指定一个不存在的端口也是可以登录的,也就是说 socket 连接并没有通过管理端口登录,所以在连接数打满的情况下,使用 socket 登录依然会报错。

[root@mysql ~]# netstat -nlp |grep 33063
[root@mysql ~]# mysql -uroot -p -S /data/mysql/data/3306/mysqld.sock -P33063 -e 'select version()'
Enter password:
+-----------+
| version() |
+-----------+
| 8.0.33    |
+-----------+

登录地址

netstat 查看 33062 端口是监听在 127.0.0.1,并不是参数里边配置的 localhost

[root@mysql ~]# netstat -nlp |grep 33062
tcp        0      0 127.0.0.1:33062         0.0.0.0:*               LISTEN      2204/mysqld

查看 MySQL 官方文档发现 admin_address 支持设置为 IPv4IPv6 或者 hostname。如果该值是主机名,则服务器将该名称解析为 IP 地址并绑定到该地址。如果一个主机名可以解析多个 IP 地址,如果有 IPv4 地址,服务器使用第一个 IPv4 地址,否则使用第一个 IPv6 地址,所以这里把 localhost 解析为了 127.0.0.1

If admin_address is specified, its value must satisfy these requirements:

  • The value must be a single IPv4 address, IPv6 address, or host name.
  • The value cannot specify a wildcard address format (*, 0.0.0.0, or ::).
  • As of MySQL 8.0.22, the value may include a network namespace specifier.

An IP address can be specified as an IPv4 or IPv6 address. If the value is a host name, the server resolves the name to an IP address and binds to that address. If a host name resolves to multiple IP addresses, the server uses the first IPv4 address if there are any, or the first IPv6 address otherwise.

指定 admin_address 为主机名,测试效果。

--修改 admin_address 值为主机名 mysql
  
vim /data/mysql/etc/3306/my.cnf
admin_address                               = mysql
  
--hosts 配置
[root@mysql ~]# grep -i mysql /etc/hosts
192.168.100.82 mysql
  
--重启数据库
systemctl restart mysql_3306
  
--查看管理端口监听的地址,监听地址变更为主机名 mysql 对应的IP地址
[root@mysql ~]# netstat -nlp |grep 33062
tcp        0      0 192.168.100.82:33062    0.0.0.0:*               LISTEN      1790/mysqld   

再次尝试

尝试使用 127.0.0.1 地址登录。

--root 账号无法通过 127.0.0.1 地址登录,因为没有授权 root 账号从 127.0.0.1 地址登录
[root@mysql ~]# mysql -uroot -p -h127.0.0.1 -P33062 -e 'select version()'                                  
Enter password:
ERROR 1130 (HY000): Host '127.0.0.1' is not allowed to connect to this MySQL server
 
--默认 root 账号只允许从 localhost 登录
mysql> select user,host from mysql.user where user='root';
+------+-----------+
| user | host      |
+------+-----------+
| root | localhost |
+------+-----------+

4故障解决

设置 admin_address 为 127.0.0.1,并添加管理账号。

--创建一个单独的 listener 线程来监听 admin 的连接请求
create_admin_listener_thread = 1

--监听地址,建议设置为一个固定的 IP 地址
admin_address = 127.0.0.1    

--监听端口,默认为 33062,也可以自定义端口
admin_port = 33062 
 
--新建管理账号
create user root@'127.0.0.1' identified by 'xxxxxxxxx';
grant all on *.* to root@'127.0.0.1' with grant option;
flush privileges;
 
 
--测试登陆成功
[root@mysql ~]# mysql -uroot -p -h127.0.0.1 -P33062 -e 'select version()'
Enter password:
+-----------+
| version() |
+-----------+
| 8.0.33    |
+-----------+

5管理端口配置总结

  1. 通过 admin_address 设置为固定的 IP 地址,例如 127.0.0.1,避免设置为 hostname 引起的不确定因素。
  2. MySQL 部署好之后,新建可以通过 admin_address 地址登录的管理员账号,例如 root@'127.0.0.1'

6一些优化建议

  1. 最小化权限配置,除管理员之外其他账号一律不允许配置 super 或者 service_connection_admin 权限。
  2. 应用端(Tomcat、JBoss 、Wildfly 等)配置数据源连接池,声明 initialSizemaxActive 属性值,控制连接数的无限增长。
  3. 及时优化 SQL,防止因性能问题引起的并发操作导致数据库连接数打满。

网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。

本文链接:https://www.xckfsq.com/news/show.html?id=23676
赞同 0
评论 0 条
letmeL2
粉丝 0 发表 78 + 关注 私信
上周热门
如何使用 StarRocks 管理和优化数据湖中的数据?  2941
【软件正版化】软件正版化工作要点  2860
统信UOS试玩黑神话:悟空  2819
信刻光盘安全隔离与信息交换系统  2712
镜舟科技与中启乘数科技达成战略合作,共筑数据服务新生态  1246
grub引导程序无法找到指定设备和分区  1213
华为全联接大会2024丨软通动力分论坛精彩议程抢先看!  163
点击报名 | 京东2025校招进校行程预告  162
2024海洋能源产业融合发展论坛暨博览会同期活动-海洋能源与数字化智能化论坛成功举办  160
华为纯血鸿蒙正式版9月底见!但Mate 70的内情还得接着挖...  157
本周热议
我的信创开放社区兼职赚钱历程 40
今天你签到了吗? 27
信创开放社区邀请他人注册的具体步骤如下 15
如何玩转信创开放社区—从小白进阶到专家 15
方德桌面操作系统 14
我有15积分有什么用? 13
用抖音玩法闯信创开放社区——用平台宣传企业产品服务 13
如何让你先人一步获得悬赏问题信息?(创作者必看) 12
2024中国信创产业发展大会暨中国信息科技创新与应用博览会 9
中央国家机关政府采购中心:应当将CPU、操作系统符合安全可靠测评要求纳入采购需求 8

加入交流群

请使用微信扫一扫!