標籤:mysql串連、dns
前幾天給公司的開發人員虛擬出來一台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
本文出自 “Jacky鑫” 部落格,請務必保留此出處http://jackyxin.blog.51cto.com/1976631/1662254
mysql串連慢的問題