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


MySQL的error log 出現大量的 DNS反解析錯誤。

DNS解析是指,將 域名解析成ip地址;

DNS反解析是指,將IP地址反解析成域名;

Version: '5.5.40-log'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server (GPL)
151023  7:57:55 [Warning] IP address 'xxx.xxx.xx.xxx' could not be resolved: Name or service not known
151023  8:28:46 [Warning] IP address 'xxx.xxx.xx.xxx' could not be resolved: Name or service not known 
151023 9:44:19 [Warning] IP address 'xxx.xxx.xx.xxx' could not be resolved: Name or service not known
151023 9:56:41 [Warning] IP address 'xxx.xxx.xx.xxx' could not be resolved: Name or service not known
151023 10:04:28 [Warning] IP address 'xxx.xxx.xx.xxx' could not be resolved: Name or service not known
151023 11:20:38 [Warning] IP address 'xxx.xxx.xx.xxx' could not be resolved: Name or service not known
51023 11:22:07 [Warning] IP address 'xxx.xxx.xx.xxx' could not be resolved: Name or service not known
151023 12:04:46 [Warning] IP address 'xxx.xxx.xx.xxx' could not be resolved: Name or service not known
1
51023 13:03:41 [Warning] IP address 'xxx.xxx.xx.xxx' could not be resolved: Temporary failure in name resolution
151023 13:12:53 [Warning] IP address 'xxx.xxx.xx.xxx' could not be resolved: Temporary failure in name resolution
151023 14:41:14 [Warning] IP address 'xxx.xxx.xx.xxx' has been resolved to the host name 'xxx.xxx.xx.xxx.broadxxxxx.com.cn', which resembles IPv4-address itself.
151023 15:00:39 [Warning] IP address 'xxx.xxx.xx.xxx' could not be resolved: Name or service not known
151023 15:23:51 [Warning] IP address 'xxx.xxx.xx.xxx' could not be resolved: Name or service not known
151023 15:53:19 [Warning] IP address 'xxx.xxx.xx.xxx' could not be resolved: Name or service not known

出現該錯誤的原因:

MySQL 數據庫服務器沒有配置 /etc/hosts,也沒有DNS服務,導致mysqld線程解析IP對應的主機名時,解析失敗。

解決方法:

使用--skip-name-resolve 參數禁用DNS的主機名解析功能,禁用該功能后,在MySQL授權表里面,你只能使用IP地址。

所以我們修改MySQL的my.cnf中的參數:

[mysqld]
skip_host_cache
skip-name-resolve=1
could not be resolved: Temporary failure in name resolution 的警告,可以使用 skip_host_cache 來搞定;
而 could not be resolved: Name or service not known 的警告,可以通過 skip_name_resolve=1 來搞定。禁止了域名解析,顯然就不會出該警告了。

注意:skip_host_cache=1 會報錯。直接 skip_host_cache 或者 skip-host-cache 就行了。

修改之后,需要重啟。

注意:mysql中的權限表,就不能使用域名了,必須使用IP地址來設置。

 

msyql文檔:

--skip-name-resolve

Do not resolve host names when checking client connections. Use only IP addresses. If you use this option, all Host column values 
in the grant tables must be IP addresses or localhost. See Section 8.12.6.2, “DNS Lookup Optimization and the Host Cache”. Depending on the network configuration of your system and the Host values for your accounts, clients may need to connect using an
explicit --host option, such as --host=localhost, --host=127.0.0.1, or --host=::1. An attempt to connect to the host 127.0.0.1 normally resolves to the localhost account. However, this fails if the server is run
with the --skip-name-resolve option, so make sure that an account exists that can accept a connection.

For example, to be able to connect as root using --host=127.0.0.1 or --host=::1, create these accounts: CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY 'root-password'; CREATE USER 'root'@'::1' IDENTIFIED BY 'root-password';
--skip-host-cache

Disable use of the internal host cache for faster name-to-IP resolution. In this case, the server performs a DNS lookup every time 
a client connects. See Section 8.12.6.2, “DNS Lookup Optimization and the Host Cache”. Use of --skip-host-cache is similar to setting the host_cache_size system variable to 0, but host_cache_size is more flexible
because it can also be used to resize, enable, or disable the host cache at runtime, not just at server startup. If you start the server with --skip-host-cache, that does not prevent changes to the value of host_cache_size, but such changes
have no effect and the cache is not re-enabled even if host_cache_size is set larger than 0.

其實,按照上面的介紹,如果我們啟用了 skip-name-resolve=1, 那么因為禁止了 ip 和 hostname 之間的解析,那么就沒有必要啟用 skip-host-cache 了。啟用的話,可能會減少一點點內存的占用吧?

 

 

--------------------------------分割線---------------------------------------

Mysql域名解析(轉自:http://www.jb51.net/article/70893.htm):

當一個新的客戶端嘗試跟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;

 

=============================

啟用 skip_name_resolve=1 之后,可能會有新的Warining:

[Warning] 'proxies_priv' entry '@ root@localhost.localdomain' ignored in --skip-name-resolve mode.

解決方法,刪除表 mysq.proxies_priv 中值為 localhost.localdomain 的行

參見:http://blog.itpub.net/14184018/viewspace-1061224/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM