在MySQL数据库中,如果performance_schema.host_cache表中没有数据的话,需要从下面方面进行检查/排查- mysql> select count(*) from performance_schema.host_cache;
- +----------+
- | count(*) |
- +----------+
- | 0 |
- +----------+
- 1 row in set (0.03 sec)
- mysql>
复制代码 1. 检查变量host_cache_size是否为0
- mysql> show variables like '%host_cache_size%';
- +-----------------+-------+
- | Variable_name | Value |
- +-----------------+-------+
- | host_cache_size | 663 |
- +-----------------+-------+
- 1 row in set (0.00 sec)
- mysql>
复制代码 当host_cache_size=0时, performance_schema将停止采集和存储host_cache表中的监控数据,导致performance_schema.host_cache表为空.当前测试服务器的变量host_cache_size大小为663,host_cache_size 表示控制主机缓存的数量,设置为0时,禁用主机缓存,此时每次服务器连接时,服务器都会进行DNS查找.另外, 此系统变量设置后立即生效,不用重启数据库服务器.
host_cache_size的官方文档介绍如下所示:- The host cache is enabled by default. The host_cache_size system variable controls its size, as well as the size of
- the Performance Schema host_cache table that exposes the cache contents. The cache size can be set at server startup
- and changed at runtime.
- Setting host_cache_size to 0, either at server startup or at runtime, disables the host cache. With the cache disabled, the server performs a DNS lookup every time a client connects.
- Changing the cache size at runtime causes an implicit host cache flushing operation that clears the host cache, truncates the host_cache table, and unblocks any blocked hosts; see Flushing the Host Cache.
- To disable DNS host name lookups, start the server with the skip_name_resolve system variable enabled. In this case, the server uses only IP addresses and not host names to match connecting hosts to rows in the MySQL grant tables. Only accounts specified in those tables using IP addresses can be used. (A client may not be able to connect if no account exists that specifies the client IP address.)
- If you have a very slow DNS and many hosts, you might be able to improve performance either by enabling skip_name_resolve to disable DNS lookups, or by increasing the value of host_cache_size to make the host cache larger.
复制代码 host_cache表提供对主机缓存内容的访问,其中包含客户机主机名和IP地址信息,用于避免DNS查找。
2. 检查变量skip_name_resolve
- mysql> show variables like 'skip_name_resolve';
- +-------------------+-------+
- | Variable_name | Value |
- +-------------------+-------+
- | skip_name_resolve | ON |
- +-------------------+-------+
- 1 row in set (0.00 sec)
- mysql>
复制代码 如上所示,系统变量skip_name_resolve=ON,那么连接MySQL数据库时不用DNS解析,此时它也不会向host_cache表写入数据.另外,skip_name_resolve这个变量是一个只读变量,修改后需要重启MySQL实例才能生效.
系统变量skip_name_resolve:检查客户端连接时是否解析主机名,变量为off,则mysqld 会在检查客户端连接时解析主机名。若变量为on,mysqld只使用 IP ;在这种情况下,授权表中的所有列值都必须是IP地址。
另外,在测试过程发现系统变量skip_name_resolve设置为OFF后,需要成功连接MySQL数据库后才会向host_cache写入数据,如果第一次就是使用错误的密码连接访问数据库,此时host_cache表并不会写入一条数据.
注意:只有非本地主机的 TCP 连接会使用主机缓存;使用网络回环地址或 socket 建立的连接不会使用主机缓存. 所以测试验证的时候请注意,否则你的实验结果可能会不一样.
其实,早期的MySQL版本,还有一个变量skip-host-cache,它的作用类似于host_cache_size,但mysql在运行时无法对该参数进行变更,并且skip-host-cache在之后的版本中已弃用。当前测试版本为MySQL 8.0.38,无法测试,遂一笔带过.
3. 命令清空了host_cache
如果你或其他人执行了flush hosts命令, 它会刷新host_cache,刷新后会清除内存中的主机缓存. 不过这个是比较难排查的.
参考资料:
- https://dev.mysql.com/doc/refman/8.4/en/host-cache.html
- https://zhuanlan.zhihu.com/p/493702797
扫描上面二维码关注我如果你真心觉得文章写得不错,而且对你有所帮助,那就不妨帮忙“推荐"一下,您的“推荐”和”打赏“将是我最大的写作动力!本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |