MySQL DNS Details

MySQL manual section 7.5.6. How MySQL Uses DNS can be summarized in one sentence: A new thread looks for hostname in cache, if not present it tries to resolve it. Simple enough, however in cases where MySQL is primarily accessed over a network (such as a dedicated backend server), DNS resolution can become the cause of a very elusive type of performance degradation, sometimes to the point of complete MySQL failure.

How MySQL Really Uses DNS

If you're not a programmer, the difference between gethostbyaddr() and gethostbyname() as mentioned in the MySQL manual may not tell you what you should know. gethostbyaddr() performs a reverse DNS lookup; it tries to resolve an IP address to a hostname. gethostbyname() performs a forward DNS lookup; it tries to resolve a hostname to an IP address. When a network connection is made to MySQL it only knows the remote end's IP address. Since grant tables can specify privileges by hostname, MySQL has to resolve every IP address to its hostname. For added security MySQL then also resolves that hostname to an IP address to make sure this IP address matches the IP address of the remote end. In short, for every TCP/IP connection, MySQL does a reverse and forward lookup. For added performance, successful lookups are cached and not tried again until either MySQL is restarted or issued a FLUSH HOSTS; command.

DNS topics in the MySQL manual are largely based on working setups—were DNS resolves quickly and correctly. However, we know DNS isn't always quick or correct. In less common cases DNS doesn't even respond. The following looks at how MySQL handles these conditions, as well as a potential bug involving DNS which stops MySQL from authenticating network connections.

This document isn't geared toward MySQL source code hackers, but in case you do want to see the respective source code it can be found in sql/hostname.cc starting at line 196 through the end of the function, which is ip_to_hostname(), for version 4.0.22 on a Linux system. Numbers in brackets like [200] scattered throughout the document are source code line references.

远程连接MySQL速度慢的问题可能是由于默认安装的MySQL开启了DNS的反向解析所导致的。解决这个问题的方法是在my.ini(Windows系统)或my.cnf(UNIX或LINUX系统)文件的\[mysqld\]下添加"skip-name-resolve"选项。这样可以禁用DNS解析,从而提高连接速度。但是需要注意的是,使用这个选项后,连接MySQL时不能再使用localhost,而需要使用IP地址。同时,如果之前是按照localhost对用户进行授权的,也需要修改用户的登录权限。另外,还可以添加"skip-grant-tables"选项来关闭访问控制,但这样会对任何用户的访问不做限制。如果MySQL服务器没有开启远程账户,可以在my.ini中添加"skip-grant-tables"选项。需要注意的是,默认情况下,show databases语句对所有用户都是开放的。如果需要重新开启访问控制,可以使用mysqladmin flush-privileges或mysqladmin reload命令。所以,如果你的MySQL远程连接很慢,可以尝试在my.ini或my.cnf文件中添加"skip-name-resolve"和"skip-grant-tables"选项来提高连接速度。 #### 引用[.reference_title] - *1* [转载 远程连接mysql速度慢的解决方法](https://blog.youkuaiyun.com/qn_lf/article/details/83678358)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值