mysql could not be resolved: Name or service not known
错误日志有类似警告:
1.120119 16:26:04 [Warning] IP address '192.168.1.10' could not be resolved: Name or service not kno
问题: mysql DNS反解:skip-name-resolve
错误日志有类似警告:
1.120119 16:26:04 [Warning] IP address '192.168.1.10' could not be resolved: Name or service not known
2.120119 16:26:04 [Warning] IP address '192.168.1.14' could not be resolved: Name or service not known
3.120119 16:26:04 [Warning] IP address '192.168.1.17' could not be resolved: Name or service not known
通过show processlist发现大量类似如下的连接:
1.|592|unauthenticated user|192.168.1.10:35320|NULL|Connect| |login|NULL|
2.|593|unauthenticated user|192.168.1.14:35321|NULL|Connect| |login|NULL|
3.|594|unauthenticated user|192.168.1.17:35322|NULL|Connect| |login|NULL|
skip-name-resolve 参数的作用:不再进行反解析(ip不反解成域名),这样可以加快数据库的反应时间。
修改配置文件添加并需要重启:
sql> flush hosts;
# mysql -h 192.168.1.1 -uroot -proot
ERROR 1045 (28000): Access denied for user 'root'@'hostname_online' (using password: YES) ### IP解析为hostname_online,不是h_tt_%,访问被拒。
# grep 192.168.1.1 /etc/hosts
192.168.1.1 hostname_online
192.168.1.1 h_tt_1
# mysql -h 192.168.1.1 -uroot -proot
ERROR 1045 (28000): Access denied for user 'root'@'hostname_online' (using password: YES)#### mysqld没有刷新主机池缓冲池中的IP和主机名信息,此时IP对应hostname_online
sql> flush hosts;
# mysql -h 192.168.1.1 -uroot -proot
ERROR 1045 (28000): Access denied for user 'root'@'hostname_online' (using password: YES) #### mysqld解析了/etc/hosts里面同一个IP对应的第一个主机名关系时,就不再解析后面这个IP对应的主机名关系
# grep 192.168.1.1 /etc/hosts
192.168.1.1 h_tt_1
192.168.1.1 hostname_online
sql> flush hosts;
# mysql -h 192.168.1.1 -uroot -proot
sql> exit
【实验:】验证解析相同IP对应的第一个主机名关系后,就不再解析相同IP:
Sql>grant usage on *.* to root@'h_tt_%' identified by ‘root';
Sql>flush hosts;
# grep h_tt /etc/hosts # grep h_tt /etc/hosts
192.168.1.1hostname_online 192.168.1.1h_tt_1
192.168.1.1h_tt_1 192,168.1.2h_tt_1
访问mysql被拒绝; 从两个IP都可以访问mysql.
【结论】
此实验验证了,上述mysql手册中对"How MySQL Uses DNS"的解释。
即mysqld线程解析/etc/hosts是,是以IP作为唯一标识的,及时一个IP对应了多个主机名,但是mysqld线程只解析第一条对应关系,不论后面有几条这个IP对应的不同主机名的记录,Mysqld进程都不会去解析,都是无效的。
【适用环境:】
没有DNS服务器,主机非常非常多,或者 不想维护/etc/hosts里面手动配置的IP和主机名对应列表时,可以在mysql授权时执行主机名为"%" 或者禁用IP和主机名解析功能(--skip-name-resolve)。