解決遠程鏈接mysql很慢的方法

開發某應用系統鏈接公司的測試服務器的mysql數據庫鏈接打開的很慢,可是鏈接本地的mysql數據庫很快,剛開始認爲多是網絡鏈接問題致使的,在進行 ping和route後發現網絡通訊都是正常的,並且在mysql機器上進行本地鏈接發現是很快的,因此網絡問題基本上被排除了,因此想看看是否是mysql的配置問題。在查詢mysql相關文檔和網絡搜索後,發現了一個配置彷佛能夠解決這樣的問題,就是在mysql的配置文件中增長以下配置參數:mysql

[mysqld]
skip-name-resolve

在linux下配置文件是/etc/my.cnf,在windows下配置文件是mysql安裝目錄下的my.ini文件。注意該配置是加在 [mysqld]下面,在更改配置並保存後,而後重啓mysql並遠程鏈接測試,一切恢復如初。該參數的官方解釋信息以下:

How MySQL uses DNSlinux

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.sql

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.windows

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.測試

      根據文檔說明,若是你的mysql主機查詢DNS很慢或是有不少客戶端主機時會致使鏈接很慢,因爲咱們的開發機器是不可以鏈接外網的,因此DNS解析是不可能完成的,從而也就明白了爲何鏈接那麼慢了。同時,請注意在增長該配置參數後,mysql的受權表中的host字段就不可以使用域名而只可以使用 ip地址了,由於這是禁止了域名解析的結果。this

相關文章
相關標籤/搜索