最近遇到一个MySQL连接的问题,远程连接MySQL时遇到“ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0”错误,如下所示:

[root@DB-Server ~]# mysql -h 10.13.65.93 -u onecard -p

Enter password:

ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0

这个测试的MySQL位于阿里云Kubernetes(K8s)中Docker容器里面,而且在远程连接MySQL出现上面错误的时候,Docker也会出现下面错误。

一般出现“ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet'”错误的原因较多:

1:网络异常或时延非常高的时候, 超过连接时间限制(系统变量connect_timeout)会导致这个错误。MySQL客户端与数据库建立连接需要发起三次握手协议,正常情况下,这个时间非常短,但是一旦网络异常,网络超时等因素出现,就会导致这个握手协议无法完成,MySQL有个参数connect_timeout,它是MySQL服务端进程mysqld等待连接建立完成的时间,单位为秒。如果超过connect_timeout时间范围内,仍然无法完成协议握手话,MySQL客户端会收到异常。  更多详细信息可以参考我这篇博客“MySQL参数max_connect_errors分析释疑”,但是当前这个案例中,不存在网络延时情况,如下所示:

[root@DB-Server ~]# ping 10.13.65.93

PING 10.13.65.93 (10.13.65.93) 56(84) bytes of data.

64 bytes from 10.13.65.93: icmp_seq=1 ttl=97 time=36.1 ms

64 bytes from 10.13.65.93: icmp_seq=2 ttl=97 time=36.3 ms

64 bytes from 10.13.65.93: icmp_seq=3 ttl=97 time=36.1 ms

64 bytes from 10.13.65.93: icmp_seq=4 ttl=97 time=36.0 ms

64 bytes from 10.13.65.93: icmp_seq=5 ttl=97 time=36.1 ms

64 bytes from 10.13.65.93: icmp_seq=6 ttl=97 time=36.2 ms

64 bytes from 10.13.65.93: icmp_seq=7 ttl=97 time=36.1 ms

64 bytes from 10.13.65.93: icmp_seq=8 ttl=97 time=36.2 ms

 

--- 10.13.65.93 ping statistics ---

8 packets transmitted, 8 received, 0% packet loss, time 7003ms

rtt min/avg/max/mdev = 36.092/36.205/36.354/0.205 ms

2:域名解析会导致这个问题。当客户端连接上来,服务器端都会对客户端进来的IP地址进行DNS解析,来获得客户端的域名或主机名,如果DNS解析出了问题或DNS解析相当慢,就会导致连接验证用户出现问题。而skip-name-resolve这个参数的意义就是禁止域名解析。官方文档解释如下:

For each new client connection, the server uses the client IP address to check whether the client host name is in the host cache. If so, the server refuses or continues to process the connection request depending on whether or not the host is blocked. If the host is not in the cache, the server attempts to resolve the host name. First, it resolves the IP address to a host name and resolves that host name back to an IP address. Then it compares the result to the original IP address to ensure that they are the same. The server stores information about the result of this operation in the host cache. If the cache is full, the least recently used entry is discarded.

The server handles entries in the host cache like this:

  • When the first TCP client connection reaches the server from a given IP address, a new cache entry is created to record the client IP, host name, and client lookup validation flag. Initially, the host name is set to NULL and the flag is false. This entry is also used for subsequent client TCP connections from the same originating IP.

当有一个新的客户端连接通过TCP进来时,MySQL Server会为这个IP在host cache中建立一个新的记录,包括IP,主机名和client lookup validation flag,分别对应host_cache表中的IP,HOST和HOST_VALIDATED这三列。第一次建立连接因为只有IP,没有主机名,所以HOST将设置为NULL,HOST_VALIDATED将设置为FALSE。

  • If the validation flag for the client IP entry is false, the server attempts an IP-to-host name-to-IP DNS resolution. If that is successful, the host name is updated with the resolved host name and the validation flag is set to true. If resolution is unsuccessful, the action taken depends on whether the error is permanent or transient. For permanent failures, the host name remains NULL and the validation flag is set to true. For transient failures, the host name and validation flag remain unchanged. (In this case, another DNS resolution attempt occurs the next time a client connects from this IP.)

MySQL Server检测HOST_VALIDATED的值,如果为FALSE,它会试图进行DNS解析,如果解析成功,它将更新HOST的值为主机名,并将HOST_VALIDATED值设为TRUE。如果没有解析成功,判断失败的原因是永久的还是临时的,如果是永久的,则HOST的值依旧为NULL,且将HOST_VALIDATED的值设置为TRUE,后续连接不再进行解析,如果该原因是临时的,则HOST_VALIDATED依旧为FALSE,后续连接会再次进行DNS解析。

  • If an error occurs while processing an incoming client connection from a given IP address, the server updates the corresponding error counters in the entry for that IP. For a description of the errors recorded, see Section 26.12.17.1, “The host_cache Table”

如果在处理来自给定IP地址的传入客户端连接时发生错误,则服务器会更新该IP条目中的相应错误计数器。 有关记录的错误的说明,请参见第26.12.17.1节“host_cache表”。

这个案例里面,因为MySQL位于阿里云Kubernetes(K8s)中Docker容器里面,对公司内部的IP地址进行DNS解析确实会出现问题。我们在配置文件设置skip_name_resolve后,确实解决了这个问题。然后本来以为找到了原因的我,在本地两台机器上测试时发现(一台MySQL版本为5.6.41, 一台MySQL版本为5.6.23),即使两台服务器相互不能做DNS解析,如下截图所示,但是从192.168.27.180连接DB-Server时,并不会报这个错误。Why? 即使我将connect_timeout调整为2,依然不会出现这个错误。看来MySQL的连接不像我们表面看的那样简单。还是相当复杂。只是目前的技术水平,还做不到进一步分析!

另外,在这个案例的测试过程中,发现skip_name_resolve为OFF的情况下,将connect_timeout设大,也不会出现这个错误

mysql>  show variables like '%connect_timeout%';

+-----------------+-------+

| Variable_name   | Value |

+-----------------+-------+

| connect_timeout | 10    |

+-----------------+-------+

1 row in set (0.01 sec)

 

mysql> set global connect_timeout=30;

Query OK, 0 rows affected (0.00 sec)

 

mysql> 

然后从客户端连接MySQL数据库就成功了,如下所示,只是IP地址并不是客户端的IP地址,而是Port IP。

当然这种情况下Kubernetes(K8s)中Docker下MySQL并没有挂掉,反而当系统变量connect_timeout=10的情况下,如果没有开启系统变量skip_name_resolve,每次远程连接MySQL就会出现Kubernetes(K8s)中Docker下MySQL挂掉,重启的过程,所以极度怀疑是疑因为在连接过程,Docker下MySQL挂掉重启才出现这个错误。但是对K8s了解不多,涉及太广,没法进一步分析具体原因了。

ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0的更多相关文章

  1. 连接 MySQL 报错:Lost connection to MySQL server at 'reading authorization packet', system error: 34

    报错信息: Lost connection to MySQL server at 解决方案: use mysql; ; flush privileges; 参考: https://blog.csdn. ...

  2. Lost connection to MySQL server at 'reading authorization packet', system error: 0_Mysql

    1.大多数时候设置"set global connect_timeout=60:"是可以解决问题的. 我们可以通过执行“SHOWSTATUS LIKE 'aborted%'”,可以 ...

  3. mysql登陆时出现ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

    有4到5天没开mysql,这天晚上打=打开phpstudy,想进去mysql练习练习,结果丢给我这个 ERROR 2013 (HY000): Lost connection to MySQL serv ...

  4. MySQL5.1升级5.6后,执行grant出错:ERROR 2013 (HY000): Lost connection to MySQL server during query【转载】

    转载: MySQL5.5升级5.6后,执行grant出错:ERROR 2013 (HY000): Lost connection to -mysql教程-数据库-壹聚教程网http://www.111 ...

  5. [奇葩问题] ERROR 2013 (HY000): Lost connection to MySQL server during query

    查询一条耗时30s以上语句,实际为2分钟多. mysql> select version(); +------------+ | version() | +------------+ | 5.6 ...

  6. ERROR 2013 (HY000): Lost connection to MySQL server at 'waiting for initial communication packet', system error: 2

    ERROR (HY000): Lost connection to MySQL server at 'waiting for initial communication packet', system ...

  7. MySQL远程连接丢失问题解决方法Lost connection to MySQL server at ‘reading initial communication packet’, system error: 0

    最近远程连接mysql总是提示 Lost connection 很明显这是连接初始化阶段就丢失了连接的错误 其实问题很简单,都是MySQL的配置文件默认没有为远程连接配置好,只需要更改下MySQL的配 ...

  8. 虚拟机中MySQL连接问题:Lost connection to MySQL server at 'reading initial communication packet, system error: 0 以及 host is not allowed to connect mysql

    环境:在VirtualBox中安装了Ubuntu虚拟机,网络使用了NAT模式,开启了端口转发. 局域网内其他计算机访问虚拟机中的MySQL Server出现两个问题: Lost connection ...

  9. Lost connection to MySQL server at ‘reading initial communication packet', system error: 0 mysql远程连接问题

    在用Navicat for MySQL远程连接mysql的时候,出现了 Lost connection to MySQL server at ‘reading initial communicatio ...

随机推荐

  1. 你所不知道的html5与html中的那些事(三)

    文章简介: 关于html5相信大家早已经耳熟能详,但是他真正的意义在具体的开发中会有什么作用呢?相对于html,他又有怎样的新的定义与新理念在里面呢?为什么一些专家认为html5完全完成后,所有的工作 ...

  2. [hdu6201]transaction transaction transaction(树形dp)

    题意:某人在一棵树中在某处买物品,价格为i,在某处卖物品,价格为j,每单位距离花费价格1,求最大赚钱数. 解题关键:两次树形dp,分别求出每个点作为被减和被加情况下的最大值,最后取一下max即可. 该 ...

  3. Linux JDK1.4卸载与1.6的安装

    Linux JDK卸载与安装 一.jdk1.4卸载 Redhat Enterprise 5 中自带安装了jdk1.4,在安装jdk1.6前,把jdk1.4卸载: 1.首先查看系统自带JDK的版本: [ ...

  4. linux 下定位程序假死

    ps -ef | grep 程序名称 pstack 程序的进程ID

  5. ubuntu: 终端全屏快捷键

    ubuntu12.04终端全屏 (2012-06-08 19:49:04) 转载▼ 进入终端全屏: ctrl+alt+f1 ctrl+alt+f2 ctrl+alt+f3 ctrl+alt+f4 ct ...

  6. Oracle SQL调优之分区表

    目录 一.分区表简介 二.分区表优势 三.分区表分类 3.1 范围分区 3.2 列表分区 3.3 散列分区 3.4 组合分区 四.分区相关操作 五.分区相关查询 附录:分区表索引失效的操作 一.分区表 ...

  7. Unity---协程的简单使用

    1.作用 协程的作用一共有两点: 延时执行代码. 在某项操作完后再执行代码. 就是控制代码在特性的时间执行. 2.协程和线程的区别 协程不是异步,不需要考虑同步和锁问题 线程避免了无意义的调度,提高了 ...

  8. java基础第七篇之接口

    1.什么是接口: * 接口是方法的集合,而且接口中的方法全是抽象的 * 你可以把接口看成一个特殊的"抽象类",接口中的方法全是抽象的 * * * 2.java中怎么定义接口: * ...

  9. linux 查看系统版本号(转)

    一.查看Linux内核版本命令(两种方法): 1.cat /proc/version [root@localhost ~]# cat /proc/versionLinux version 2.6.18 ...

  10. [Java]三大特性之封装

    封装这个我们可以从字面上来理解,简单来说就是包装的意思,专业点就是信息隐藏. 是指利用抽象数据类型将数据和基于数据的操作封装在一起,使其构成一个不可分割的独立实体,数据被保护在抽象数据类型的内部,尽可 ...