Databases 7 min read

Understanding MySQL Host Cache and skip_name_resolve Impact on Performance

The article analyzes a production issue where slave connections were slow due to missing skip_name_resolve, explains MySQL's reverse DNS resolution and host cache mechanisms, compares related parameters, shows how to inspect the host_cache table, and concludes that proper host cache settings can slightly improve MySQL performance.

Aikesheng Open Source Community
Aikesheng Open Source Community
Aikesheng Open Source Community
Understanding MySQL Host Cache and skip_name_resolve Impact on Performance

In a production environment with a master‑slave semi‑synchronous MySQL cluster, connections to the master were fast while connections to the slave experienced a ten‑second delay after the TCP three‑way handshake.

Packet capture revealed that the delay originated on the MySQL side; the slave’s configuration lacked the skip_name_resolve option. Adding this option and restarting the slave resolved the latency issue.

MySQL reverse resolution process

The client initiates a connection, the server creates a thread, checks the performance_schema.host_cache table for the hostname, and if not found, performs IP‑to‑hostname and hostname‑to‑IP lookups, comparing the results before proceeding to authentication.

1. mysql-client initiates connection;<br/>2. mysqld creates a thread;<br/>3. Thread checks performance_schema.host_cache;<br/>4. If present, proceeds to privilege check;<br/>5. If absent, resolves IP to hostname;<br/>6. Resolves hostname back to IP;<br/>7. Compares resolved IP with original;<br/>8. If match, caches entry and authenticates;<br/>9. If mismatch, returns error.

Only non‑local TCP connections use the host cache; connections via loopback or socket bypass it.

Purpose of the host cache

Caches IP‑to‑hostname lookup results, reducing DNS queries; the number of entries is controlled by host_cache_size.

Stores error information for failed connections; repeated errors from the same host can block further connections, governed by max_connect_errors.

Relevant parameters host_cache_size: sets the size of the host cache; a value of 0 disables caching, forcing DNS lookups on every connection. skip-host-cache: disables the host cache at runtime but cannot be changed dynamically and is deprecated in newer versions. skip_name_resolve: when enabled (ON), MySQL skips hostname resolution and requires all grant tables to use IP addresses only.

Viewing the host_cache table

Example query:

mysql> select * from performance_schema.host_cache\G<br/>*************************** 1. row ***************************<br/>IP: 10.186.61.21<br/>HOST: NULL<br/>HOST_VALIDATED: YES<br/>SUM_CONNECT_ERRORS: 0<br/>... (additional columns omitted for brevity) ...<br/>FIRST_SEEN: 2020-12-18 10:30:03<br/>LAST_SEEN: 2020-12-18 14:50:41<br/>FIRST_ERROR_SEEN: 2020-12-18 10:30:03<br/>LAST_ERROR_SEEN: 2020-12-18 14:46:37

Running FLUSH HOSTS clears the in‑memory host cache.

Impact on performance

When skip_name_resolve=OFF and host_cache_size=700, the initial DNS resolution and caching affect MySQL performance, but subsequent connections see only a slight improvement.

Conclusion

The observed behavior matches expectations: disabling hostname resolution or properly sizing the host cache reduces the latency caused by DNS lookups, leading to modest performance gains after the cache is populated.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performancehost_cacheskip_name_resolve
Aikesheng Open Source Community
Written by

Aikesheng Open Source Community

The Aikesheng Open Source Community provides stable, enterprise‑grade MySQL open‑source tools and services, releases a premium open‑source component each year (1024), and continuously operates and maintains them.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.