当前位置: 岑溪网站开发工作室 > 代码记录库 > SQL相关 >

mysql5.0 x 远程连接数据库慢的解决方案

发表时间:2016-10-18 16:47 来源:未知 分类:SQL相关 作者:岑溪网站开发 点击:

今使用mysql 打开远程数据库操作中发现,每执行一个动作,都需要等上几十秒或更久。

   正常情况 不可能要这么久,于是去了解了下MYSQL。终于明白了,原来是mysql在远程连接时先采用域名解析去查找主机,当查找不到时才会采用ip地址去找

   这样我们可以从这里解决了。

  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在远程连接时先采用域名解析去查找主机,当查找不到时才会采用ip地址去找。
加入这个参数就禁止了mysql采用DNS解析的方式,而直接通过ip进行连接
当然根据文档中描述,如果加入了“skip-name-resolve”配置的话,mysql就不能通过域名解析了。
如果应该把mysql数据库中的user表中的host字段改成相应的ip。如“localhost”应该改为“127.0.0.1”
 
解决方案
 
1、在mysql安装目录下的my.ini(windows系统)配置文件中找到[mysqld],在该行的下面添加如下配置
 
[mysqld]
 
skip-name-resolve
 
 
在[mysqld]后面加入skip-name-resolve 后重启MYSQL 远程连接数据库慢解决了,感谢 支持!
 
 
上一篇:php mysql查询数据库某字段时间与现在时间 下一篇:没有了

岑溪网站二次开发

岑溪网站二次开发

SQL相关年推荐榜