被我误解的max_connect_errors

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 实为吾之愚见,望诸君酌之!闻过则喜,与君共勉

实为吾之愚见,望诸君酌之!闻过则喜,与君共勉 

第一节  什么是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,创建账号:

b984add34b00bee3d562d96d25fa07ccb8145a7a

2,设置max_connect_errors3

4776ffe81ccffb4de40ab1daacd8220f5be822d2

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

f376ff16b8b381fcf71f4b79893db1fd11246e51

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

6cd06be24be4b24af171a2710d5f5e790f977cb3

第三节 继续分析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 cachehost cache里保存了一些客户端的ip地址,主机名,以及这个客户端在与server建立连接时遇到的一些错误信息,host cache对不是本地的TCP连接才有效,所以host cache127.0.0.1 或者::1是无效的,并且对于Unix socket filenamed 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,创建账号

b984add34b00bee3d562d96d25fa07ccb8145a7a

2,设置max_connect_errors3

4776ffe81ccffb4de40ab1daacd8220f5be822d2

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

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

95e4568e7fe1b5cb44a21b30a9aa9210ef9f0a73

第一次telnet 10.26.254.217 3306

bce33a3ea7394f8e1d564c3921d3f294dcd79814

052299fb80bdd456d12855a8f84689011f13ce51

第二次 telnet 10.26.254.217 3306

9ac1875beec55c02bedc86afcee78400f63745c5

f7a6d9e971fdc753d65c5b116b95ed45572e0d55

第三次telnet 10.26.254.217 3306

aa6232eaa401e0c1bfa5cdcfc0f2168c379ec97a

09f732c6269039549da6393138a09d99cee2a878

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

3bba32ab58c65de19d912febc805d13f407c93b8

fe47d609cfd83b0867b5a64232d1a6aae1011505

问题复现了,出现了错误提示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

f0d9b5f93d58538762288eb6be86e8edb9cd6cab

2,再次查询performance_schema.host_cache

44dd050f8f353a71d9563c3b38092d0cc0bb65a9

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

5f889f9d574cf843a1423a12ac4a2d21b73d9f8d

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

5.2 打开skip-name-resolve 

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

123514e6b68f10befb209f4321c1ab82894fc7d3

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

3fe7fac36742a2b9391267ab4936372a891e2d7a

3,查询performance_schema.host_cache

4f308a50bcc5c27cb666b61228177261312cd0e0

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

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

 

 

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
6月前
|
人工智能
Timeout on reading data from socket
Timeout on reading data from socket
61 3
|
18天前
|
SQL 关系型数据库 MySQL
mysql:1153 Got a packet bigger than ‘max_allowed_packet’ bytes的解决方法
mysql:1153 Got a packet bigger than ‘max_allowed_packet’ bytes的解决方法
9 0
|
4月前
|
SQL 关系型数据库 MySQL
[ERR] 2006 - MySQL server has gone away,Got a packet bigger than 'max_allowed_packet' bytes
[ERR] 2006 - MySQL server has gone away,Got a packet bigger than 'max_allowed_packet' bytes
29 0
|
9月前
|
算法 Java
22-大厂面试题:Con-current Mode Failure如何导致以及解决
上文我们已经介绍了CMS垃圾收集器的工作原理以及流程,本篇我们接着深入说明CMS垃圾收集器的缺点以及所导致的一些问题应该如何解决。
89 0
|
11月前
【WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, st】
【WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, st】
142 0
|
关系型数据库 MySQL Linux
MySQL:Got a packet bigger than ‘max_allowed_packet‘ bytes
我们往MySQL导入数据的时候,当数据量过大的时候,就会报错,这是因为允许的最大数据超出了我们的默认设置,所以我们需要修改MySQL的默认设置
113 0
MySQL:Got a packet bigger than ‘max_allowed_packet‘ bytes
|
关系型数据库 MySQL 数据安全/隐私保护
被我误解的max_connect_errors
谈谈被我误解的max_connect_errors
被我误解的max_connect_errors
|
SQL 关系型数据库 MySQL
mysql的 max_allowed_packet 和 max_connections
mysql的 max_allowed_packet 和 max_connections
169 0
|
缓存 网络协议 关系型数据库
max_connect_errors参数
一次ERROR 1129 (HY000): Host 'xxx.xxx.xxx.xxx' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'报错,重新认识max_connect_errors参数。
1880 0
|
缓存 网络协议 关系型数据库
从Host blocked报错学习max_connect_errors
1.案发现场-Host blocked 一个案例场景是大数据抽取job任务连接MySQL实例抽数,任务报错如下图所示:报错表示,host被锁是由于有大量的连接错误,如果要解锁就执行mysqladmin flush-hosts;知识点!!!线索:host被锁,大量连接错误证人提示线索:确认最近新加p.
1209 0
从Host blocked报错学习max_connect_errors