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

 

顺便再转贴

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

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值