前几天给公司的开发人员虚拟出来一台mysql服务器,使用ssh登录后show databases、show tables都很快,但是同事使用连接工具连接数据


库需要等几秒,用程序连接mysql,可能等上半分钟甚至更长,白白耽误开发人员的时间。

开发人员和运维都在同一局域网,网速没问题,排除了网的问题。虚拟出来的服务器,又加了2G内存,问题依然没解决。百度一下,找到了一


个解决方法,就是禁掉mysql对外部连接进行DNS解析。

vi /etc/my.cnf文件

[mysqld]

skip-name-resolve

表示跳过反向解析

发生上述情况的原因在于mysql服务器在接收到一个远程ip访问的时候,默认会去查该ip的反向解析,这个反查的过程会比较慢,如果该ip没


有反解,mysql也有可能会卡死在这个连接上。

禁止MySQL对外部连接进行DNS解析,从而导致mysql中出现大量状态为Connect的连接,影响mysql使用。使用这一选项可以消除MySQL进行DNS


解析的时间。但需要注意,如果开启该选项,则所有远程主机连接授权都要使用IP地址方式,否则MySQL将无法正常处理连接请求!


在mysql官网上找到了关于mysql如何使用DNS的解释,如下:

When a new thread connects to mysqld, mysqld will span 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. 


链接地址:http://live.dadanini.at/cds/mysql/doc/D/N/DNS.html