Explanation of MySQL skip name resolve

Keywords: MySQL DNS mysqladmin

mysql connection is slow. Log in to the server to view mysql logs:
IP address 'XX.XX.XX.XX' has been resolved to the host name 'XX.XX.XX.XX.ro.ovo.sc', which resembles IPv4-address itself.


The reason is that mysql performs DNS reverse resolution on the connected client.
There are two solutions:
1. Write the ip address of the client in the / etc/hosts file of the mysql server. Just give it a name.
2. Add – skip name resolve to my.cnf.
For the first method, which is not practical and stupid, the skip name resolve option can disable dns resolution. However, in this way, the host name cannot be used in the authorization table of mysql, only IP can be used.
I understand that this is how mysql handles client-side parsing,
1. When the mysql client is connected, the server will take the initiative to check the domain name of the client.
2. First find the / etc/hosts file and search for the corresponding relationship between the domain name and IP.
3. If the hosts file is not set, look up the DNS settings. If the DNS server is not set, it will immediately return failure, which is equivalent to mysql setting the skip name resolve parameter. If the DNS server is set, reverse resolution will be performed until timeout.



The so-called reverse parsing is as follows:
After receiving the connection request, MySQL gets the ip address of the client. In order to better match the permission records in mysql.user (some are defined by hostname).
If mysql server has set up dns server, and the client ip does not have corresponding hostname on dns, this process is slow, resulting in connection waiting.
 

After adding skip name resolve, you will skip the process.

Official explanation

How MySQL
uses DNS When a new thread connects to mysqld, mysqld will
spawn a new thread to handle the request. This thread will first check
if the hostname is in the hostname cache. If not the thread will call
gethostbyaddr_r() and gethostbyname_r() to resolve the hostname. If
the operating system doesn't support the above thread-safe calls, the
thread will lock a mutex and call gethostbyaddr() and gethostbyname()
instead. Note that in this case no other thread can resolve other
hostnames that is not in the hostname cache until the first thread is
ready. You can disable DNS host lookup by starting mysqld with
–skip-name-resolve. In this case you can however only use IP names in
the MySQL privilege tables. If you have a very slow DNS and many
hosts, you can get more performance by either disabling DNS lookop
with –skip-name-resolve or by increasing the HOST_CACHE_SIZE define
(default: 128) and recompile mysqld. You can disable the hostname
cache with –skip-host-cache. You can clear the hostname cache with
FLUSH HOSTS or mysqladmin flush-hosts. If you don't want to allow
connections over TCP/IP, you can do this by starting mysqld with
–skip-networking.

Note: after adding the configuration parameter, the host field in the authorization table of mysql cannot use the domain name but only the ip address, because this is the result of domain name resolution prohibition.

Posted by lohmk on Wed, 06 May 2020 22:54:02 -0700