欢迎光临
嗟嗟嗟~じぇじぇじぇ!~(''jjj'')/

mysql远程连接数据库慢的解决方案

一、环境

数据库为mysql5.0

二、问题描述

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

三、解决方案

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

[mysqld]
skip-name-resolve

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

四、解释说明

1、查看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进行连接

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

未经允许不得转载:嗟嗟嗟 » mysql远程连接数据库慢的解决方案
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址