如何排查RDS for MySQL 中的 “Got an error reading communication packets” 错误

数据库关系型数据库技术服务知识库
问题描述

在 MySQL中看到如下错误,我该如何排查并解决此类问题?

Aborted connection xxx to db: '<database name>' user: '<user name>' host: '<host IP>' (Got an error reading communication packets)
问题分析

上述错误通常意味着客户端-服务器连接非正常断开,同时 RDS for MySQL 会将状态变量 aborted_connects 和 aborted_clients 计数器+1,然后会出发日志记录到错误日志中[1]。 触发此警告有可能与如下几个方面有关:

  1. 客户端-服务器连接异常断开
  2. 客户端或驱动程序不兼容
  3. 超过 wait_timeout[2] 或 interactive_timeout[3] 阈值的空闲连接
  4. 获取连接数据包时,客户端连接超过 connect_timeout[4] 秒数阈值
  5. 连接超出超出 max_allowed_packet 参数值[5]
  6. 其他原因
解决方案

根据我们之前的分析,连接超时相关的参数的原定设置值可能不适合您的数据库实例与应用程序,您可以考虑如下方法来进行规避此类错误: 1.适当修改 interactive_timeoutwait_timeout。使用连接池的应用程序配置的超时参数必须与连接池设置相匹配。


# 查看当前配置
mysql> show variables like 'interactive_timeout';
+---------------------+-------+
| Variable_name       | Value |
+---------------------+-------+
| interactive_timeout | 600   |
+---------------------+-------+
1 row in set (0.01 sec)

mysql> show variables like 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout  | 600   |
+---------------+-------+
1 row in set (0.00 sec)

2.connect_timeout 用于连接认证过程,为 connect_timeout 设置一个较高的值,看这样做是否有助于减少连接中止错误消息的发生率。

#查看当前配置
mysql> show variables like 'connect_timeout';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| connect_timeout | 10    |
+-----------------+-------+
1 row in set (0.00 sec)

3.如果您的应用程序需要处理结果集巨大的查询/大事务,建议您高 max_allowed_packets 的值。

#查看当前配置
mysql> show variables like 'max_allowed_packet';
+--------------------+----------+
| Variable_name      | Value    |
+--------------------+----------+
| max_allowed_packet | 33554432 |
+--------------------+----------+
1 row in set (0.01 sec)

4.在应用程序中,客户端在断开连接之前,请先调用 mysql_close() 函数[6] 5.检查当前客户端是否和数据库版本相匹配。 :本文不会给出具体的建议值,因为这与您的应用程序息息相关。

参考文档

[1] https://dev.mysql.com/doc/refman/5.7/en/communication-errors.html [2] https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_wait_timeout [3] https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_interactive_timeout [4] https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_connect_timeout [5] https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_max_allowed_packet [6] https://dev.mysql.com/doc/c-api/8.0/en/mysql-close.html 如果您有其他问题,欢迎您联系火山引擎技术支持服务

30
0
0
0
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论