Detailed analysis of the use of MySQL DNS

  • 2020-05-14 05:07:22
  • OfStack

When the mysql client connects to the mysql server (process: mysqld), mysqld creates a new thread to process the request. The thread first checks to see if the hostname is in the hostname cache. If not, the thread attempts to resolve the hostname.
If the system is thread-safe, gethostbyaddr_r () and gethostbyname_r() are called to perform hostname resolution;
If the system does not support thread-safe calls, the thread locks a mutex and calls gethostbyaddr() and gethostbyname(). In this case, no other thread can resolve a host name that is not in the hostname cache until the first thread unlocks the mutex.
Disable DNS hostname lookup by starting mysqld with the -- skip-name-resolve option. At this point, you can only use the IP address in the MySQL authorization table, not the host name.
If DNS resolution is slow and includes many hosts, improve performance by disabling DNS to find or add HOST_CACHE_SIZE definitions (default: 128) with -- skip-name-resolve and recompiling mysqld;
Disable hostname caching by starting the server with the -- skip-host-cache option. To clear the hostname cache, execute the FLUSH HOSTS statement or the mysqladmin flush-hosts command.
If you want to disable TCP/IP connections completely, start mysqld with the -- skip-networking option.

Related articles: