解决远程链接mysql很慢的方法

开发某应用系统链接公司的测试服务器的mysql数据库链接打开的很慢,可是链接本地的mysql数据库很快,刚开始认为多是网络链接问题致使的,在进行 ping和route后发现网络通讯都是正常的,并且在mysql机器上进行本地链接发现是很快的,因此网络问题基本上被排除了,因此想看看是否是mysql的配置问题。在查询mysql相关文档和网络搜索后,发现了一个配置彷佛能够解决这样的问题,就是在mysql的配置文件中增长以下配置参数:mysql

[mysqld]
skip-name-resolve

在linux下配置文件是/etc/my.cnf,在windows下配置文件是mysql安装目录下的my.ini文件。注意该配置是加在 [mysqld]下面,在更改配置并保存后,而后重启mysql并远程链接测试,一切恢复如初。该参数的官方解释信息以下:

How MySQL uses DNSlinux

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.sql

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.windows

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

相关文章
相关标签/搜索