SQL 解决MySQL远程请求慢的问题

阅读(4613)

现象

远程连接MySQL服务端很慢,约有一个固定的10s延时。

原因

MySQL服务端会对每一个请求的IP做DNS反向解析。如果DNS配置不当则会引起超时,超时时间大约10s。

For each new client connection, the server uses the client IP address to check whether the client host name is in the host cache. If not, the server attempts to resolve the host name.

via: https://dev.mysql.com/doc/refman/5.0/en/host-cache.html

解决

方案一: 配置好服务端的DNS,并对这些解析足够快。
修改 /etc/resolv.conf 对应的内容。

方案二:禁用DNS解析

To disable DNS host name lookups, start the server with the --skip-name-resolve option. In this case, the server uses only IP addresses and not host names to match connecting hosts to rows in the MySQL grant tables. Only accounts specified in those tables using IP addresses can be used.

即,修改MySQL配置文件 /etc/my.cnf,增加一行:

skip-name-resolve 

重启MySQL。

副作用是不能基于域名来请求和授权了。

另外,SSH也存在远程连接慢的问题,原因和此类似。

Tags: mysql,dns