欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

解决远程连接mysql很慢的方法(mysql_connect 打开连接慢)

程序员文章站 2023-12-16 14:52:04
在进行 ping和route后发现网络通信都是正常的,而且在mysql机器上进行本地连接发现是很快的,所以网络问题基本上被排除了。以前也遇到过一次这样的问题,可后来就不知怎...
在进行 ping和route后发现网络通信都是正常的,而且在mysql机器上进行本地连接发现是很快的,所以网络问题基本上被排除了。以前也遇到过一次这样的问题,可后来就不知怎么突然好了,这次又遭遇这样的问题,所以想看看是不是mysql的配置问题。在查询mysql相关文档和网络搜索后,发现了一个配置似乎可以解决这样的问题,就是在mysql的配置文件中增加如下配置参数:

[mysqld]
skip-name-resolve

      在linux下配置文件是/etc/my.cnf,在windows下配置文件是mysql安装目录下的my.ini文件。注意该配置是加在 [mysqld]下面,在更改配置并保存后,然后重启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主机查询dns很慢或是有很多客户端主机时会导致连接很慢,由于我们的开发机器是不能够连接外网的,所以dns解析是不可能完成的,从而也就明白了为什么连接那么慢了。同时,请注意在增加该配置参数后,mysql的授权表中的host字段就不能够使用域名而只能够使用 ip地址了,因为这是禁止了域名解析的结果

上一篇:

下一篇: