被我误解的max_connect_errors


原文:https://developer.aliyun.com/article/755441

第一节 什么是max_connect_errors

一开始接触这个参数的时候,感觉他和max_connections的含义差不多,字面意思简单明了,这个参数的含义是最大连接错误数,翻翻mysql的文档中的解释是If more than this many successive connection requests from a host are interrupted without a successful connection, the server blocks that host from further connections,大意是:如果mysql服务器连续接收到了来自于同一个主机的请求,且这些连续的请求全部都没有成功的建立连接就被断开了,当这些连续的请求的累计值大于 max_connect_errors的设定值时,mysql服务器就会阻止这台主机后续的所有请求。”without a successful connection”那太好办了,故意输错密码不就行了,并且网上搜索了下该参数的说明,大量的文章充斥着” 防止暴力破解密码”的内容,于是兴高采烈的去做了测试。以下测试基于自建的mysql(非rds for mysql),由于rds for mysql无法直接设置set global,设置时需要在”rds控制台-参数这里”里进行设置:https://help.aliyun.com/document_detail/26179.html?spm=5176.11065259.1996646101.searchclickresult.44156de7pLffcV


第二节 测试max_connect_errors

1,创建账号:

image.png

2,设置max_connect_errors为3:

image.png

3,故意输错密码3次,第四次使用正确密码登录进行验证:

image.png

4,结论是第四次依然可以登录,即密码不对(认证失败)不属于” ”without a successful connection””的范畴,网上的” 防止暴力破解密码”也不成立了。

image.png


第三节 继续分析max_connect_errors

再继续看文档,发现还有以下说明:

You can unblock blocked hosts by flushing the host cache. To do so, issue a FLUSH HOSTS statement or execute a mysqladmin flush-hosts command.

大意是:

当你遇到主机被阻止的时候,你可以清空host cache来解决,具体的清空方法是执行flush hosts或者在mysql服务器的shell里执行 mysqladmin flush-hosts操作

既然清空host cache可以解决主机被阻止访问的问题,那应该与host cache有些关系,看看host cache的介绍可能会有些眉目,关于host cache,文档解释如下:

The MySQL server maintains a host cache in memory that contains information about clients: IP address, host name, and error information. The server uses this cache for nonlocal TCP connections. It does not use the cache for TCP connections established using a loopback interface address (127.0.0.1 or ::1), or for connections established using a Unix socket file, named pipe, or shared memory.

大意是:

Mysql服务器会在内存里管理一个host cache,host cache里保存了一些客户端的ip地址,主机名,以及这个客户端在与server建立连接时遇到的一些错误信息,host cache对不是本地的TCP连接才有效,所以host cache对127.0.0.1 或者::1是无效的,并且对于Unix socket file、named pipe以及 shared memory方式建立的连接也是无效的。并且通过了解,host cache的内容可以通过performance_schema.host_cache来查看,通过performance_schema.host_cache表里的几个列的描述信息,对之前的测试不成立的原因有些了解了,部分相关列如下:

  • IP
    The IP address of the client that connected to the server, expressed as a string.

连接到mysql server的主机的连接地址

  • HOST
    The resolved DNS host name for that client IP, or NULL if the name is unknown.

通过dns解析IP地址获取到的该IP地址对应的mysql client的主机名

  • SUM_CONNECT_ERRORS
    The number of connection errors that are deemed “blocking” (assessed against the max_connect_errors system variable). Only protocol handshake errors are counted, and only for hosts that passed validation (HOST_VALIDATED = YES).
  • COUNT_HANDSHAKE_ERRORS
    The number of errors detected at the wire protocol level.

通过SUM_CONNECT_ERRORS(连接错误计数)描述,重点是红色部分:只计算协议握手过程的错误(Only protocol handshake errors are counted),也就是说max_connect_errors 可能记录的是协议(不确定是tcp协议还是应用协议,通过抓包以及COUNT_HANDSHAKE_ERRORS的” the wire protocol level”说明可能是指应用协议)的握手过程中出现的错误 ,也就是可以说网络不好(无法顺利握手)会导致该问题。


第四节 继续测试max_connect_errors

通过之前的说明,需要模拟应用协议握手失败的情况,最后考虑使用telnet一些来做测试

1,创建账号

image.png

2,设置max_connect_errors为3:

image.png

3,先使用telnet 10.26.254.217 3306连接3次,第四次使用正确的账号密码尝试登陆:

telnet前查看performance_schema.host_cache的记录为空

image.png

第一次telnet 10.26.254.217 3306

image.png

image.png

第二次 telnet 10.26.254.217 3306

image.png

image.png

第三次telnet 10.26.254.217 3306

image.png

image.png

第四次执行mysql -h10.26.254.217 -utestcon -p123 -P3306

image.png

image.png

问题复现了,出现了错误提示ERROR 1129 (HY000): Host ‘10.24.236.231’ is blocked because of many connection errors; unblock with ‘mysqladmin flush-hosts’


第五节 ERROR 1129 (HY000)问题延伸

解决ERROR 1129 (HY000)的方法是执行flush host或者 mysqladmin flush-hosts,其目的是为了清空host cache里的信息,那是不是说不使用host cache就可以了?使host cache不生效的方式有如下两种:

1,设置 host_cache_size 为0/ 打开skip-host-cache

2,打开skip-name-resolve

需要通过测试看下推测是否生效

5.1 设置 host_cache_size 为0/ 打开skip-host-cache
1,设置host_cache_size为0

image.png

2,再次查询performance_schema.host_cache

image.png

3,继续之前的测试:先使用telnet 10.26.254.217 3306连接3次,第四次使用正确的账号密码尝试登陆

image.png

更改已经生效,max_connect_errors的作用无效了

5.2 打开skip-name-resolve
1,在cnf配置文件里设置skip-name-resolve 以此打开skip-name-resolve

image.png

2,继续之前的测试:先使用telnet 10.26.254.217 3306连接3次,第四次使用正确的账号密码尝试登陆

image.png

3,查询performance_schema.host_cache

image.png

更改已经生效,max_connect_errors的作用无效了,RDS for mysql 的skip_name_resolve是on的状态,

所以很少会出现ERROR 1129 (HY000)的错误