解決遠端連線mysql很慢的方法(mysql_connect 開啟串連慢)

來源:互聯網
上載者:User

在進行 ping和route後發現網路通訊都是正常的,而且在mysql機器上進行本地串連發現是很快的,所以網路問題基本上被排除了。以前也遇到過一次這樣的問題,可後來就不知怎麼突然好了,這次又遭遇這樣的問題,所以想看看是不是mysql的配置問題。在查詢mysql相關文檔和網路搜尋後,發現了一個配置似乎可以解決這樣的問題,就是在mysql的設定檔中增加如下配置參數:

[mysqld]
skip-name-resolve

在linux下設定檔是/etc/my.cnf,在windows下設定檔是mysql安裝目錄下的my.ini檔案。注意該配置是加在 [mysqld]下面,在更改配置並儲存後,然後重啟mysql並遠端連線測試,一切恢複如初。該參數的官方解釋資訊如下:

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.

根據文檔說明,如果你的mysql主機查詢DNS很慢或是有很多用戶端主機時會導致串連很慢,由於我們的開發機器是不能夠串連外網的,所以DNS解析是不可能完成的,從而也就明白了為什麼串連那麼慢了。同時,請注意在增加該配置參數後,mysql的授權表中的host欄位就不能夠使用網域名稱而只能夠使用 ip地址了,因為這是禁止了網域名稱解析的結果。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.