ITPub博客

首页 > 数据库 > MySQL > MySQL远程访问时,非常慢,甚至出现连不上的情况

MySQL远程访问时,非常慢,甚至出现连不上的情况

MySQL 作者:dbasdk 时间:2014-09-25 16:04:39 0 删除 编辑

MySQL远程访问时,非常慢,甚至出现连不上的情况


权限已赋,防火墙等已关闭

解决方法,my.cnf里面添加
[mysqld]
skip-name-resolve

skip-name-resolve 
选项就能禁用DNS解析,连接速度会快很多。不过,这样的话就不能在MySQL的授权表中使用主机名了而只能用ip格式。

还有权限的问题,当用户设置限制只能访问某个数据库,如果这个数据库被删了,再重建这个指定数据库,限制用户还是不能访问这个数据,大概是删除数据库的时间,把该用户的访问权限也级联删除了,详细可以查看mysql.db的记录

若使用–skip-grant-tables系统将对任何用户的访问不做任何访问控制,但可以用 mysqladmin flush-privileges或mysqladmin reload来开启访问控制;默认情况是show databases语句对所有用户开放,

如果mysql服务器没有开远程帐户,就在my.cnf里面加上skip-grant-tables

 
实在不行:直接关闭系统的dns服务chkconfig name off

排除网络问题........

就MySQL本身而言,问题出在在mysql dns反解析

mysql>show processlist;

| 20681949 | unauthenticated user | 10.10.4.193:52497 | NULL | Connect |        | Reading from net                 | NULL                                                                                                 | 
| 20681948 | unauthenticated user | 10.10.4.193:52495 | NULL | Connect |        | Reading from net                 | NULL

发现有非常多的 unauthenticated user 尝试做登入使用 mysql 的情況 ,当这种情况无限制发生时就会造成系统十分缓慢。

查阅mysql官方网站得知,这属于官方一个系统上的特殊设定,就把他当成mysql的一个bug算了
不管链接的的方式是经过 hosts 或是 IP 的模式,他都会对 DNS 做反查 
mysqld 会尝试去反查 IP -> dns ,由于反查解析过慢,就会无法应付过量的查询。

 

解决办法:

/usr/local/mysql/bin/mysqld_safe --skip-name-resolve --user=mysql&

加 --skip-name-resolve 这么一个参数就可以,关闭mysql的dns反查功能。

或者修改mysql配置文件。

编辑/etc/my.cnf

在[mysqld]段中加入

skip-name-resolve

重启mysql

 

在/etc/my.cnf 的配置文件中加入如下一句,禁用DNS反响解析,就能大大加快MySQL连接的速度。
[mysqld]

下面加上这句
skip-name-resolve

#注意有些文章中写道加入–skip-name-resolve,经验证,在CentOS5下加入–skip-name-resolve会导致mysql守护进程无法启动。估计在其他linux系统下是一样的,windows下没有测试,skip-name-resolve应该就可以。

附录:( 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.

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29734436/viewspace-1280688/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 笔记mysql优化
请登录后发表评论 登录
全部评论

注册时间:2014-07-02

  • 博文量
    490
  • 访问量
    1388126