zoukankan      html  css  js  c++  java
  • 被我误解的max_connect_errors【转】

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

    第一节  什么是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”那太好办了,故意输错密码不就行了,并且网上搜索了下该参数的说明,大量的文章充斥着” 防止暴力破解密码”的内容,于是兴高采烈的去做了测试。

    第二节  测试max_connect_errors

    1,创建账号:

    b984add34b00bee3d562d96d25fa07ccb8145a7a

    2,设置max_connect_errors为3:

    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 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,创建账号

    b984add34b00bee3d562d96d25fa07ccb8145a7a

    2,设置max_connect_errors为3:

    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)的错误

    转自

    被我误解的max_connect_errors-博客-云栖社区-阿里云
    https://yq.aliyun.com/articles/159612?t=t1

  • 相关阅读:
    shell字符串截取
    QT,QT SDK, QT Creator 区别
    linux -- 扩容 /home 空间( xfs文件系统分区扩容指定挂载点)
    条件变量与互斥量
    越努力越幸运--2018年7月22日周记
    越努力越幸运--动态数组vector
    越努力越幸运--3-日常bug修复
    越努力越幸运--2-LD_PRELOAD, fork ,僵尸进程
    越努力越幸运--1
    makefile--回顾基础篇
  • 原文地址:https://www.cnblogs.com/paul8339/p/7809556.html
Copyright © 2011-2022 走看看