mysql的err日志中的信息中有一堆类似warning

2017-04-04 16:55:05 2082 [Warning] IP address '117.44.24.236' could not be resolved: Name or service not known
2017-04-04 18:30:30 2082 [Warning] IP address '111.121.193.254' could not be resolved: Name or service not known
2017-04-04 19:08:24 2082 [Warning] IP address '221.9.251.229' has been resolved to the host name '229.251.9.221.adsl-pool.jlccptt.net.cn', which resembles IPv4-address itself.
2017-04-04 19:36:18 2082 [Warning] IP address '140.205.201.46' could not be resolved: Name or service not known
2017-04-04 19:50:11 2082 [Warning] IP address '116.52.12.102' has been resolved to the host name '102.12.52.116.broad.km.yn.dynamic.163data.com.cn', which resembles IPv4-address itself.
2017-04-04 21:01:09 2082 [Warning] IP address '49.4.142.98' could not be resolved: Name or service not known
2017-04-04 21:04:05 2082 [Warning] IP address '182.18.72.162' could not be resolved: Name or service not known
2017-04-04 21:04:37 2082 [Warning] IP address '49.4.143.137' could not be resolved: Name or service not known
2017-04-04 21:15:22 2082 [Warning] IP address '49.4.142.97' could not be resolved: Name or service not known
2017-04-04 22:18:49 2082 [Warning] IP address '116.226.188.238' could not be resolved: Name or service not known
2017-04-04 23:53:40 2082 [Warning] IP address '222.186.21.110' could not be resolved: Name or service not known
2017-04-05 00:14:43 2082 [Warning] IP address '157.52.163.70' could not be resolved: Name or service not known
2017-04-05 00:28:47 2082 [Warning] IP address '116.31.100.147' could not be resolved: Name or service not known
2017-04-05 02:18:02 2082 [Warning] IP address '14.152.95.39' could not be resolved: Name or service not known
2017-04-05 03:27:18 2082 [Warning] IP address '222.186.58.235' could not be resolved: Name or service not known
2017-04-05 05:09:49 2082 [Warning] IP address '119.10.73.172' could not be resolved: Name or service not known
2017-04-05 07:28:54 2082 [Warning] IP address '49.67.105.53' could not be resolved: Name or service not known
2017-04-05 08:07:41 2082 [Warning] IP address '64.56.78.159' could not be resolved: Name or service not known
2017-04-05 11:24:52 2082 [Warning] IP address '195.228.45.81' has been resolved to the host name '81.45-228-195.hosting.adatpark.hu', which resembles IPv4-address itself.
2017-04-05 14:59:27 2082 [Warning] IP address '124.173.115.142' could not be resolved: Name or service not known
2017-04-05 15:24:24 2082 [Warning] IP address '58.221.44.168' could not be resolved: Name or service not known

解决办法:

三、解决的办法:
1、修改配置文件

可以通过两个参数来disable这个功能,在MYSQL的配置文件中[mysqld]中加入下面的参数:

[mysqld]
--skip-host-cache
--skip-name-resolve

重新授权,将所有访问数据库服务器的授权方式都改成IP形式的。

grant all on *.* to ‘root’@’172.16.12.68’identified by ‘123456’;

2、添加授权。

将所有访问数据库服务器的授权方式都改成IP形式。
不同的用户用不同的用户名和密码。

grant all on *.* to ‘user_68’@’172.16.12.68’identified by ‘pwd_68’;
grant all on *.* to ‘user_67’@’172.16.12.67’identified by ‘pwd_67’;
....

然后去 mysql数据库下面的 user表  和db表 下面删除掉那些含有含有主机名字的权限记录。

官方解释:https://dev.mysql.com/doc/refman/5.5/en/host-cache.html

8.12.5.2 DNS Lookup Optimization and the Host Cache

The MySQL server maintains a host cache in memory that contains information about clients: IP address, host name, and error information. The server uses this cache for nonlocal TCP connections. It does not use the cache for TCP connections established using a loopback interface address (127.0.0.1 or ::1), or for connections established using a Unix socket file, named pipe, or shared memory.

For each new client connection, the server uses the client IP address to check whether the client host name is in the host cache. If not, the server attempts to resolve the host name. First, it resolves the IP address to a host name and resolves that host name back to an IP address. Then it compares the result to the original IP address to ensure that they are the same. The server stores information about the result of this operation in the host cache. If the cache is full, the least recently used entry is discarded.

The server performs host name resolution using the thread-safe gethostbyaddr_r() and gethostbyname_r() calls if the operating system supports them. Otherwise, the thread performing the lookup locks a mutex and calls gethostbyaddr() and gethostbyname() instead. In this case, no other thread can resolve host names that are not in the host cache until the thread holding the mutex lock releases it.

The server uses the host cache for several purposes:

    By caching the results of IP-to-host name lookups, the server avoids doing a DNS lookup for each client connection. Instead, for a given host, it needs to perform a lookup only for the first connection from that host.

    The cache contains information about errors that occur during the connection process. Some errors are considered “blocking.” If too many of these occur successively from a given host without a successful connection, the server blocks further connections from that host. The max_connect_errors system variable determines the number of permitted errors before blocking occurs. See Section B.5.2.6, “Host 'host_name' is blocked”. 

网友解释:

现象:

程序连接mysql时,mysql的error.log里面提示:

[Warning] IP address '10.0.0.220' could not be resolved: Name or service not known

原因:

Mysql数据库服务器没有配置 /etc/hosts,也没有DNS服务,导致mysqld线程解析IP对应的主机名时,解析失败。

参考资料:

Mysql域名解析:

当一个新的客户端尝试跟mysqld创建连接时,mysqld产生一个新线程来处理这个请求。新线程会先检查请求建立连接的主机名是否在Mysql的主机名缓冲中,如果不在,线程会尝试去解析请求连接的主机名。

解析的逻辑如下:

a. Mysql线程通过gethostbyaddr()把获取的IP地址解析成主机名,然后通过gethostbyname()把获取的主机名解析成IP地址,保障主机名和IP地址对应关系的准确;

b. 如果操作系统支持使用安全进程的gethostbyaddr_r()和gethostbyname_r() 调用,Mysqld线程可以用它俩来优化主机名解析;

c. 如果操作系统不支持安全线程调用,Mysqld进程先做一个互斥锁,然后调用gethostbyaddr()和gethostbyname()解析主机名。此时,在第一个进程释放掉主机名缓冲池的主机名之前,其它进程无法再次解析这个主机名; <-------MySQL手册里面在此处说的host name ,意思应该是指同一个IP地址和对应的第一个主机名关系。

在启动mysqld进程是,可以使用 --skip-name-resolve 参数禁用DNS的主机名解析功能,禁用该功能后,在MySQL授权表里面,你只能使用IP地址。

如果你所处环境的DNS非常慢 或者 有很多主机, 你可以通过禁用DNS解析功能--skip-name-resolve 或者 提高 HOST_CACHE_SIZE大小 来提升数据库的响应效率。

禁用主机名缓冲的发方法: 使用--skip-host-cache 参数; 刷新主机名缓冲区: 执行 flush hosts 或者执行mysqladmin flush-hosts;

禁用TCP/IP连接: 使用--skip-networking参数。

参考文档:

  • http://www.uedsc.com/ip-address-could-not-be-resolved.html

发表评论