在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

优网科技秉承"专业团队、品质服务" 的经营理念,诚信务实的服务了近万家客户,成为众多世界500强、集团和上市公司的长期合作伙伴!
优网科技成立于2001年,擅长网站建设、网站与各类业务系统深度整合,致力于提供完善的企业互联网解决方案。优网科技提供PC端网站建设(品牌展示型、官方门户型、营销商务型、电子商务型、信息门户型、微信小程序定制开发、移动端应用(手机站、APP开发)、微信定制开发(微信官网、微信商城、企业微信)等一系列互联网应用服务。