回忆之城
生命在于折腾
posts - 575,comments - 9,trackbacks - 0

一、环境

数据库为mysql5.0

二、问题描述

mysql在使用远程连接时,建立连接很慢,几十秒到一分多钟。

三、解决方案

1、在mysql安装目录下的my.ini(windows系统)配置文件中找到[mysqld],在该行的下面添加如下配置

  1. [mysqld]  
  2.   
  3. skip-name-resolve  

2、重启mysql服务,测试即可

四、解释说明

1、查看mysql文档,看到如下解释

[plain] view plaincopy
  1. How MySQL uses DNS  
  2.   
  3. 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.  
  4.   
  5. 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.  
  6.   
  7. 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.  
  8.   
  9. 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.  
  10.   
  11. You can disable the hostname cache with –skip-host-cache. You can clear the hostname cache with FLUSH HOSTS or mysqladmin flush-hosts.  
  12.   
  13. If you don't want to allow connections over TCP/IP, you can do this by starting mysqld with –skip-networking.  

从文档中我们可以看到加入上述参数配置的原因。mysql在远程连接时先采用域名解析去查找主机,当查找不到时才会采用ip地址去找。加入这个参数就禁止了mysql采用DNS解析的方式,而直接通过ip进行连接

2、当然根据文档中描述,如果加入了“skip-name-resolve”配置的话,mysql就不能通过域名解析了。如果应该把mysql数据库中的user表中的host字段改成相应的ip。如“localhost”应该改为“127.0.0.1”

posted on 2015-03-17 17:38 回忆之城 阅读(303) 评论(0)  编辑 收藏 引用 所属分类: unix/linux数据库
只有注册用户登录后才能发表评论。