原文地址:http://www.jb51.net/article/27616.htm
症狀描述###
- 本機連接mysql速度很快
- 遠程ping mysql主機速度正常
- 遠程連接mysql速度需要等待30秒左右(問題所在)
- 遠程連接后訪問mysql中的表正常
原因是mysql主機不能訪問廣域網,連接DNS時連接不上,因此連接的時候會有延時。
解決辦法###
在my.cnf中添加如下配置
[mysqld]
skip-name-resolve
重啟mysql服務
說明###
根據文檔說明,如果你的mysql主機查詢DNS很慢或是有很多客戶端主機時會導致連接很慢,由於我們的開發機器是不能夠連接外網的,所以DNS解析是不可能完成的,從而也就明白了為什么連接那么慢了。同時,請注意在增加該配置參數后,mysql的授權表中的host字段就不能夠使用域名而只能夠使用 ip地址了,因為這是禁止了域名解析的結果。
以下是官方信息###
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.