MySQL令人头疼的Aborted告警案例分析

[[196907]]

实战

Part1:写在最前

在MySQL的error log中,我们会经常性看到一些各类的Aborted connection错误,本文中会针对这类错误进行一个初步分析,并了解一个问题产生后的基本排查思路和方法。掌握这种方法是至关重要的,而不是出现问题了,去猜,去试。数据库出现问题的时候需要DBA在短时间内快速解决问题,因此一个好与坏的DBA,区别也在于此。

Part2:种类

  1. [Warning] Aborted connection 305628 to db: 'db' user'dbuser' host: 'hostname' (Got an error reading communication packets) 
  2. [Warning] Aborted connection 81 to db:'unconnected' user'root' host: '127.0.0.1' (Got timeout reading communication 
  3. packets) 
  4. [Warning] Aborted connection 109 to db:'helei1' user'sys_admin' host: '192.168.1.1' (Got an error writing communication packets) 
  5. [Warning] Access denied for user 'root'@'127.0.0.1' (using password: YES) 
  6. [Warning] Got an error writing communication packets  

Part3:重点参数分析

wait_timeout

Command-Line Format--wait-timeout=#
System VariableNamewait_timeout
Variable ScopeGlobal, Session
Dynamic VariableYes
Permitted Values (Windows)Typeinteger
Default28800
Min Value1
Max Value2147483
Permitted Values (Other)Typeinteger
Default28800
Min Value1
Max Value31536000

这个参数指的是数据库系统在关闭它之前,服务器等待非交互式连接上的活动的秒数。

interactive_timeout

Command-Line Format--interactive-timeout=#
System VariableNameinteractive_timeout
Variable ScopeGlobal, Session
Dynamic VariableYes
Permitted ValuesTypeinteger
Default28800
Min Value1

这个参数指的是在关闭交互式连接之前,服务器等待活动的秒数

Warning:警告这两个参数建议一起调节,能够避免一些坑。

本文的两个参数值采用的是默认值

  1. mysql> show global variables like '%timeout%'
  2. +----------------------------+----------+ 
  3. | Variable_name              | Value    | 
  4. +----------------------------+----------+ 
  5. | connect_timeout            | 10       | 
  6. | delayed_insert_timeout     | 300      | 
  7. | innodb_lock_wait_timeout   | 50       | 
  8. | innodb_rollback_on_timeout | OFF      | 
  9. |interactive_timeout        | 28800    | 
  10. | lock_wait_timeout          | 31536000 | 
  11. | net_read_timeout           | 30       | 
  12. | net_write_timeout          | 60       | 
  13. | slave_net_timeout          | 3600     | 
  14. |wait_timeout               | 28800    | 
  15. +----------------------------+----------+ 
  16. 10 rows in set (0.01 sec)  

另外在数据库中,我们重点关注下这两个参数,看看什么情况下Aborted_clients会提升,什么情况下Aborted_connects 会提升

  1. mysql>show global status like 'aborted%'
  2. +------------------+-------+ 
  3. |Variable_name    | Value | 
  4. +------------------+-------+ 
  5. |Aborted_clients  | 19    | 
  6. |Aborted_connects | 0     | 
  7. +------------------+-------+ 
  8. rows inset (0.00 sec)  

Part4:案例1

这里我故意输入错误的密码5次,来看下数据库的error log和Aborted的哪个参数记载了这一问题

  1. [root@HE3~]# mysql -uroot -pwrongpass -h127.0.0.1 
  2. ERROR 1045 (28000): Access denied for user 'root'@'127.0.0.1' (using password: YES) 
  3. [root@HE3~]# mysql -uroot -pwrongpass -h127.0.0.1 
  4. ERROR 1045 (28000): Access denied for user 'root'@'127.0.0.1' (using password: YES) 
  5. [root@HE3~]# mysql -uroot -pwrongpass -h127.0.0.1 
  6. ERROR 1045 (28000): Access denied for user 'root'@'127.0.0.1' (using password: YES) 
  7. [root@HE3~]# mysql -uroot -pwrongpass -h127.0.0.1 
  8. ERROR 1045 (28000): Access denied for user 'root'@'127.0.0.1' (using password: YES) 
  9. [root@HE3~]# mysql -uroot -pwrongpass -h127.0.0.1 
  10. ERROR 1045 (28000): Access denied for user 'root'@'127.0.0.1' (using password: YES)  

可以看出,这里的Aborted_connects 记录了密码错误的这一问题

  1. mysql>show global status like 'aborted%'
  2. +------------------+-------+ 
  3. |Variable_name    | Value | 
  4. +------------------+-------+ 
  5. |Aborted_clients  | 19    | 
  6. |Aborted_connects | 5     | 
  7. +------------------+-------+ 
  8. rows inset (0.00 sec)  

error log中,也记载了这类密码输错的信息

  1. [Warning] Access denied for user'root'@'127.0.0.1' (using password: YES) 
  2. [Warning] Access denied for user 'root'@'127.0.0.1' (using password:YES) 
  3. [Warning] Access denied for user 'root'@'127.0.0.1' (using password:YES) 
  4. [Warning] Access denied for user 'root'@'127.0.0.1' (using password:YES) 
  5. [Warning] Access denied for user 'root'@'127.0.0.1' (using password:YES)  

Part5:案例2

接下来我们看下文章第三节提到的两个重点参数对数据库连接的行为影响

这里我们将这两个参数均配置为10秒

  1. mysql>set global wait_timeout=10; 
  2. Query OK,0 rows affected (0.00 sec) 
  3.    
  4. mysql>set global interactive_timeout=10; 
  5. Query OK,0 rows affected (0.00 sec) 
  6. mysql>show processlist; 
  7. ERROR 2006 (HY000): MySQL server has gone away 
  8. No connection. Trying to reconnect... Connection id: 79 Current database: *** NONE *** 
  9.    
  10. +----+------+-----------------+------+---------+------+-------+------------------+ 
  11. | Id |User | Host            | db   | Command | Time | State | Info             | 
  12. +----+------+-----------------+------+---------+------+-------+------------------+ 
  13. | 79 |root | 127.0.0.1:42016 | NULL | Query  |    0 | NULL  | show processlist | 
  14. +----+------+-----------------+------+---------+------+-------+------------------+ 
  15. 1 row in set (0.00 sec)  

这里三次操作,可以看到clients数上升,这是由于timeout参数控制的,已经连接上数据的连接被杀掉。

  1. mysql>show global status like 'aborted%'
  2. ERROR 2006 (HY000): MySQL server has gone away 
  3. No connection. Trying to reconnect... Connection id:    81 Current database: *** NONE *** 
  4.    
  5. +------------------+-------+ 
  6. |Variable_name    | Value | 
  7. +------------------+-------+ 
  8. |Aborted_clients  | 22    | 
  9. |Aborted_connects | 5     | 
  10. +------------------+-------+ 
  11. rows in set (0.01 sec)  

error log中记载的是

  1. [Warning] Aborted connection 81 to db: 'unconnected' user'root' host: '127.0.0.1' (Got timeout reading communication packets) 
  2. [Warning] Aborted connection 78 to db: 'unconnected' user'root' host: '127.0.0.1' (Got timeout reading communication packets)  
  3. [Warning] Aborted connection 79 to db: 'unconnected' user'root' host: '127.0.0.1' (Got timeout reading communication packets) 

Part6:案例3

在这个案例中我们看下***连接数对数据库连接的行为影响

  1. mysql>show global variables like 'max_conn%'
  2. +--------------------+-------+ 
  3. |Variable_name      | Value | 
  4. +--------------------+-------+ 
  5. |max_connect_errors | 1000  | 
  6. |max_connections    | 1024  | 
  7. +--------------------+-------+ 
  8. rows in set (0.00 sec) 
  9.    
  10.    
  11. mysql>set global max_connections=2; 
  12. Query OK,0 rows affected (0.00 sec)  

这里看到爆出了连接数过多的问题

  1. [root@HE3~]# mysql -uroot -pMANAGER -h127.0.0.1 
  2. ERROR 1040 (HY000): Too many connections  

而错误日志没有任何记录

Part7:案例4

第三方工具navicat select结果没有出来的时候选择停止则出现

clients上涨

  1. mysql>show global status like 'aborted%'
  2. +------------------+-------+ 
  3. |Variable_name    | Value | 
  4. +------------------+-------+ 
  5. |Aborted_clients  | 28    | 
  6. |Aborted_connects | 10    | 
  7. +------------------+-------+ 
  8. rows in set (0.00 sec)  

error log日志记录

  1. 170626 16:26:56 [Warning] Aborted connection 109 to db: 'helei1' user'sys_admin' host: '192.168.1.1' (Got an error writing communication packets) 

Part8:原因总结

  1. 在MySQL中sleep状态数百秒的而且经常重复连接是应用程序在工作后没有关闭连接的症状之一,而是依靠数据库wait_timeout来关闭它们。强烈建议在操作结束时更改应用程序逻辑以正确关闭连接;
  2. 检查以确保max_allowed_packet的值足够高,并且客户端没有收到“数据包太大”消息。 这种情况他会中止连接,而不正确关闭它;
  3. 另一种可能性是TIME_WAIT。建议您确认连接被妥善管理并且是在应用端正常关闭;
  4. 确保事务正确提交(开始和提交),以便一旦应用程序“完成”连接,它将处于“clean”的状态;
  5. 您应该确保客户端应用程序不中止连接。 例如,如果PHP的选项max_execution_time设置为5秒,增加connect_timeout是没用的,因为PHP会杀死脚本。 其他编程语言和环境也有类似的选项;
  6. 连接延迟的另一个原因是DNS问题。 检查是否启用了skip-name-resolve,检查主机根据其IP地址而不是其主机名进行身份验证;
  7. 尝试增加MySQL的net_read_timeout和net_write_timeout值,看看是否减少了错误的数量。

——总结——

通过这4个案例,我们能够了解到,Aborted_clients、和Aborted_connects的区别,以及什么情况下会爆出什么样的错误日志,文章第二节中的几个Aborted错误是常见的错误,这类错误出现的时候脑海里要有一个理论知识,知道什么情况下,会出现什么样的错误,以便快速定位问题。由于笔者的水平有限,编写时间也很仓促,文中难免会出现一些错误或者不准确的地方,不妥之处恳请读者批评指正。 

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/252040.html<

(0)
运维的头像运维
上一篇2025-04-29 06:23
下一篇 2025-04-29 06:24

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注