MySQL技术专题—MySQL报错Got an error reading communication packets问题分析指南

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介: MySQL技术专题—MySQL报错Got an error reading communication packets问题分析指南

前因背景


当系统服务的MySQL错误日志中,发现大量以下类似信息:经常收到客户关于通信故障错误的问题—客户面临间歇性的”Got an error reading communication packet”错误,这里分析这个错误出现的原因,以及如何解决这个问题。


Aborted connection 1055898 to db: 'xxx' user: 'yyy' host: 'xxx.xxx.xxx.xxx' (Got timeout reading communication packets)
复制代码



官方解释


下面看看官网怎么说:


Aborted_connects:


If a client is unable even to connect, the server increments the Aborted_connects status variable.

A client attempts to access a database but has no privileges for it.
#客户端没有权限但是尝试访问MySQL数据库
A client uses an incorrect password.
#客户端输入的密码有误。
A connection packet does not contain the right information.
#连接包不包含正确信息
takes more than connect_timeout seconds to obtain a connect packet.
#超过连接时间限制,主要是这个系统变量connect_timeout控制(mysql默认是10s,基本上,除非网络环境极端不好,一般不会超时。)
复制代码



Aborted_clients:


lIf a client successfully connects but later disconnects improperly or is terminated, the server increments the Aborted_clients status variable

The client program did not call mysql_close() before exiting…
#客户端没有进行关闭
The client had been sleeping more than wait_timeout or interactive_timeout seconds without issuing any requests to the server.
#客户端睡眠时间超过了wait_timeout或interactive_timeout秒,而没有向服务器发出任何请求。
The client program ended abruptly in the middle of a data transfer.
#客户端程序在数据传输过程中突然终止。
复制代码



Aborted_connects OR Aborted_clients:


Other reasons for problems with aborted connections or aborted clients:

the max_allowed_packet variable value is too small or queries require more memory than you have allocated for mysqld
#max_allow_packet设置过小
Use of Ethernet protocol with Linux, both half and full duplex. Some Linux Ethernet drivers have this bug
#Linux以太网驱动程序有这个bug
A problem with the thread library that causes interrupts on reads.#线程库中导致读取中断的问题。
Badly configured TCP/IP. #tcp/iip 配置信息混乱
Faulty Ethernets, hubs, switches, cables, and so forth. This can be diagnosed properly only by replacing hardware.
#故障的以太网、集线器、交换机、电缆等等
复制代码

dev.mysql.com/doc/refman/…


Aborted connection情况下,这也意味着以下几个问题:


  1. 客户端正常连接,但是被异常结束(可能是程序没有正常关闭连接)
  2. 客户端sleep的时间超过了wait_timeout、或interactive_timeout的值(这会导致连接被mysql强制关闭)
  3. 客户端异常终端,或者查询超出max_allowed_packet的值




临时配置解决办法


设置最大包大小

set global max_allowed_packet = 1024*1024*1024;
复制代码


查看包大小

mysql> show variables like '%max_allowed_packet%';
+--------------------------+------------+
| Variable_name            | Value      |
+--------------------------+------------+
| max_allowed_packet       | 16777216   |
| slave_max_allowed_packet | 1073741824 |
+--------------------------+------------+
复制代码


永久配置解决办法

[mysqld]
max_allowed_packet=256M
复制代码


当然,也可能是其它原因导致的。坦白讲,异常中断是很难诊断的,也有可能是和网络、防火墙有关。


从以下几个方面考虑:


  1. 如果有大量的连接进程处于sleep状态时间较长,也就意味着应用没有正确、及时关闭数据库连接。强烈建议在应用中能恰当地关闭数据库连接,否则就需要依赖mysql的wait_timeout的设置来关闭连接了。


  1. 建议检查max_allowed_packet的值,确保该值设置的合理,这样客户端就不会接收到"packet too large"消息提示。如果设置不合理,会异常中断连接。


  1. 建议关注线程的time_wait数量。如果netstat发现有大量的连接处于time_wait状态,表示该建议应用端调整连接关闭问题了。



TIME_WAIT的处理方案
netstat -ano|grep TIME_WAIT
tcp        0      0 xxx.xxx.xxx.xxx:10054       xxx.xxx.xxx.xxx:55586      TIME_WAIT   timewait (32.97/0/0)
tcp        0      0 xxx.xxx.xxx.xxx:10054       xxx.xxx.xxx.xxx:55367      TIME_WAIT   timewait (27.82/0/0)
tcp        0      0 xxx.xxx.xxx.xxx:10054       xxx.xxx.xxx.xxx:55776      TIME_WAIT   timewait (37.09/0/0)
tcp        0      0 xxx.xxx.xxx.xxx:10054       xxx.xxx.xxx.xxx:56505      TIME_WAIT   timewait (54.61/0/0)
tcp        0      0 xxx.xxx.xxx.xxx:10054       xxx.xxx.xxx.xxx:55553      TIME_WAIT   timewait (31.94/0/0)
tcp        0      0 xxx.xxx.xxx.xxx:10054       xxx.xxx.xxx.xxx:56643      TIME_WAIT   timewait (57.73/0/0)
tcp        0      0 xxx.xxx.xxx.xxx:10054       xxx.xxx.xxx.xxx:55221      TIME_WAIT   timewait (23.70/0/0)
tcp        0      0 xxx.xxx.xxx.xxx:10054       xxx.xxx.xxx.xxx:55920      TIME_WAIT   timewait (41.18/0/0)
复制代码
网络问题分析


  • 检查DNS配置是否有延迟问题。


  • 检查是否同时配置了skip_name_resolve,且使用IP验证主机而不是使用主机名。设置该参数后,使用ip验证主机,而不是使用主机名。使用该参数后,mysql授权表中的host列必须是IP地址或者localhost。
  • 增加net_read_timeout、net_write_timeout的值,并观察是否还有该错误发生;
  • net_read_timeout很少会导致出错,除非网络环境非常差。


mysql的参数设置:
mysql> show variables like '%timeout%';
+-----------------------------+----------+
| Variable_name               | Value    |
+-----------------------------+----------+
| connect_timeout             | 10       |
| interactive_timeout         | 1800     |
| lock_wait_timeout           | 31536000 |
| net_read_timeout            | 30       |
| net_write_timeout           | 60       |
| wait_timeout                | 1800     |
+-----------------------------+----------+
复制代码
  • 连接异常中断是因为连接没有被正常关闭。
  • server端不会导致连接abort,除非客户端/服务器端发生了网络问题。而不是server端的问题。


tcpdump,netstat -s
复制代码



log_warnings掩耳盗铃

image.png

image.png

mysql> show global variables like '%log_warning%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_warnings  | 2     |
+---------------+-------+
1 row in set (0.00 sec)
复制代码


如果log_warnings的值大于1,mysql会将类似信息写入错误日志:

[Warning] Aborted connection 305628 to db: 'db' user: 'dbuser' host: 'hostname' (Got an error reading communication packets)
[Warning] Aborted connection 305627 to db: 'db' user: 'dbuser' host: 'hostname' (Got an error reading communication packets)
复制代码


可以修改一下log_waring的值:


set global log_warnings=1;
复制代码


但这样直接修改,重启后会失效,修改配置文件mysql.cnf   log_warnings = 1




相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
9天前
|
关系型数据库 MySQL 数据库
ORM对mysql数据库中数据进行操作报错解决
ORM对mysql数据库中数据进行操作报错解决
33 2
|
11天前
|
SQL 关系型数据库 MySQL
MySQL 8.0报错--1118-Row size too large. The maximum row size for the used table type, not counting BLOBs,is 8126,
MySQL 8.0报错--1118-Row size too large. The maximum row size for the used table type, not counting BLOBs,is 8126,
MySQL 8.0报错--1118-Row size too large. The maximum row size for the used table type, not counting BLOBs,is 8126,
|
4天前
|
Oracle NoSQL 关系型数据库
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
14 2
|
4天前
|
数据采集 中间件 关系型数据库
Mac系统通过brew安装mysql5.7后,启动报错的解决办法
Mac系统通过brew安装mysql5.7后,启动报错的解决办法
12 2
|
6天前
|
SQL 关系型数据库 MySQL
9-21|mysql语句报错
9-21|mysql语句报错
|
7天前
|
关系型数据库 MySQL 数据库
docker启动mysql多实例连接报错Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’
docker启动mysql多实例连接报错Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’
30 0
|
16天前
|
NoSQL 关系型数据库 MySQL
微服务架构下的数据库选择:MySQL、PostgreSQL 还是 NoSQL?
在微服务架构中,数据库的选择至关重要。不同类型的数据库适用于不同的需求和场景。在本文章中,我们将深入探讨传统的关系型数据库(如 MySQL 和 PostgreSQL)与现代 NoSQL 数据库的优劣势,并分析在微服务架构下的最佳实践。
|
18天前
|
存储 SQL 关系型数据库
使用MySQL Workbench进行数据库备份
【9月更文挑战第13天】以下是使用MySQL Workbench进行数据库备份的步骤:启动软件后,通过“Database”菜单中的“管理连接”选项配置并选择要备份的数据库。随后,选择“数据导出”,确认导出的数据库及格式(推荐SQL格式),设置存储路径,点击“开始导出”。完成后,可在指定路径找到备份文件,建议定期备份并存储于安全位置。
158 11
|
2月前
|
弹性计算 关系型数据库 数据库
手把手带你从自建 MySQL 迁移到云数据库,一步就能脱胎换骨
阿里云瑶池数据库来开课啦!自建数据库迁移至云数据库 RDS原来只要一步操作就能搞定!点击阅读原文完成实验就可获得一本日历哦~
|
14天前
|
存储 SQL 关系型数据库
MySQL的安装&数据库的简单操作
本文介绍了数据库的基本概念及MySQL的安装配置。首先解释了数据库、数据库管理系统和SQL的概念,接着详细描述了MySQL的安装步骤及其全局配置文件my.ini的调整方法。文章还介绍了如何启动MySQL服务,包括配置环境变量和使用命令行的方法。最后,详细说明了数据库的各种操作,如创建、选择和删除数据库的SQL语句,并提供了实际操作示例。
57 13
MySQL的安装&数据库的简单操作
下一篇
无影云桌面