mysql中的 skip-name-resolve 问题

mysql连接很慢,登陆到服务器上查看服务器日志都是正常的,无可疑 记录,登陆到mysql服务器上,查看下进程,发 现有很多这样的连接:
218 | unauthenticated user | 192.168.10.6:44500 | NULL    | Connect | NULL | login | NULL
219 | unauthenticated user | 192.168.10.6:44501 | NULL    | Connect | NULL | login | NULL   
........


原因是由于mysql对连接的客户端进行DNS反向解析
有2种解决办法:
1,把client的ip写在mysql服务器的/etc/hosts文件里,随便给个名字就可以了。
2,在 my.cnf 中加入 –skip-name-resolve 。
对于第一种方法比较笨,也不实用,那么 skip-name-resolve 选项可以禁用dns解析,但是,这样不能在mysql的授权表中使用主机名了,只能使用IP。
我理解mysql是这样来处理客户端解析过程的,
1,当mysql的client连过来的时候,服务器会主动去查client的域名。
2,首先查找 /etc/hosts 文件,搜索域名和IP的对应关系。
3,如果hosts文件没有,则查找DNS设置,如果没有设置DNS服务器,会立刻返回失败,就相当于mysql设置了skip-name-resolve参数,如果设置了DNS服务器,就进行反向解析,直到timeout。



所谓反向解析是这样的:
mysql接收到连接请求后,获得的是客户 端的ip,为了更好的匹配mysql.user里的权限记录(某些是用hostname定义的)。
如果mysql服务器设置了dns服务器,并且客户端ip在dns上并没有相应的hostname,那么这个过程很慢,导致连接等待。

添加skip-name-resolve以后就跳过着一个过程了。


官方的解释

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地址了,因为这是禁止了域名解析的结果。


  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值