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”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值