關於skip_name_resolve參數的總結


作為MySQL調優的一部分,很多人都推薦開啟skip_name_resolve。這個參數是禁止域名解析的(當然,也包括主機名)。很多童鞋會好奇,這背后的原理是什么,什么情況下開啟這個參數比較合適。

 

基於以下原因,MySQL服務端會在內存中維護着一份host信息, 包括三部分:IP,主機名和錯誤信息。主要用於非本地TCP連接。

1. 通過在第一次建立連接時緩存IP和host name的映射關系,同一主機的后續連接將直接查看host cache,而不用再次進行DNS解析。

2. host cache中同樣會包含IP登錄失敗的錯誤信息。可根據這些信息,對這些IP進行相應的限制。后面將會具體提到。

host cache的信息可通過performance_schema中host_cache表查看。

 

那么,IP和host name的映射關系是如何建立的呢?

1. 當有一個新的客戶端連接進來時,MySQL Server會為這個IP在host cache中建立一個新的記錄,包括IP,主機名和client lookup validation flag,分別對應host_cache表中的IP,HOST和HOST_VALIDATED這三列。第一次建立連接因為只有IP,沒有主機名,所以HOST將設置為NULL,HOST_VALIDATED將設置為FALSE。

2. MySQL Server檢測HOST_VALIDATED的值,如果為FALSE,它會試圖進行DNS解析,如果解析成功,它將更新HOST的值為主機名,並將HOST_VALIDATED值設為TRUE。如果沒有解析成功,判斷失敗的原因是永久的還是臨時的,如果是永久的,則HOST的值依舊為NULL,且將HOST_VALIDATED的值設置為TRUE,后續連接不再進行解析,如果該原因是臨時的,則HOST_VALIDATED依舊為FALSE,后續連接會再次進行DNS解析。

 

另,解析成功的標志並不只是通過IP,獲取到主機名即可,這只是其中一步,還有一步是通過解析后的主機名來反向解析為IP,判斷該IP是否與原IP相同,如果相同,才判斷為解析成功,才能更新host cache中的信息。

 

基於上面的總結,下面談談 host cache的優缺點:

缺點:當有一個新的客戶端連接進來時,MySQL Server都要建立一個新的記錄,如果DNS解析很慢,無疑會影響性能。如果被允許訪問的主機很多,也會影響性能,這個與host_cache_size有關,這個參數是5.6.5引入的。5.6.8之前默認是128,5.6.8之后默認是-1,基於max_connections的值動態調整。所以如果被允許訪問的主機很多,基於LRU算法,先前建立的連接可能會被擠掉,這些主機重新進來時,會再次進行DNS查詢。

優點:通常情況下,主機名是不變的,而IP是多變的。如果一個客戶端的IP經常變化,那基於IP的授權將是一個繁瑣的過程。因為你很難確定IP什么時候變化。而基於主機名,只需一次授權。而且,基於host cache中的失敗信息,可在一定程度上阻止外界的暴力破解攻擊。

 

關於阻止外界的暴力破解攻擊,涉及到max_connect_errors參數,默認為100,官方的解釋如下:

If more than this many successive connection requests from a host are interrupted without a successful connection, the server blocks that host from further connections.

如果某個客戶端的連接達到了max_connect_errors的限制,將被禁止訪問,並提示以下錯誤:

Host 'host_name' is blocked because of many connection errors.
Unblock with 'mysqladmin flush-hosts'

 

下面來模擬一下

首先,設置max_connect_errors的值

mysql> show variables like 'max_connect_errors';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 100   |
+--------------------+-------+
1 row in set (0.00 sec)

mysql> set global max_connect_errors=2;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'max_connect_errors';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 2     |
+--------------------+-------+
1 row in set (0.00 sec)

通過telnet模擬interrupted without a successful connection。

[root@mysql-slave1 ~]# telnet 192.168.244.145 3306
Trying 192.168.244.145...
Connected to 192.168.244.145.
Escape character is '^]'.
N
5.6.26-log
          K]qA1nYT!w|+ZhxF1c#|kmysql_native_password
^]
!#08S01Got packets out of orderConnection closed by foreign host.
[root@mysql-slave1 ~]# telnet 192.168.244.145 3306
Trying 192.168.244.145...
Connected to 192.168.244.145.
Escape character is '^]'.
N
Y#>PVB(>!Bl}NKnjIj]sMmysql_native_password
^]
!#08S01Got packets out of orderConnection closed by foreign host.
[root@mysql-slave1 ~]# mysql -h192.168.244.145 -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
ERROR 1129 (HY000): Host '192.168.244.144' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'

即便后來使用了正確的賬號和密碼登錄,依舊會被阻止。

再來看看host_cache表中的信息,sum_connect_errors為2了。

mysql> select ip,host,host_validated,sum_connect_errors,count_authentication_errors from performance_schema.host_cache;
+-----------------+------+----------------+--------------------+-----------------------------+
| ip              | host | host_validated | sum_connect_errors | count_authentication_errors |
+-----------------+------+----------------+--------------------+-----------------------------+
| 192.168.244.144 | NULL | YES            |                  2 |                           0 |
+-----------------+------+----------------+--------------------+-----------------------------+
1 row in set (0.00 sec)

 

該阻止會一直生效,直到采取以下操作:

1. mysql> flush hosts;

2. # mysqladmin flush-hosts

3. truncate table performance_schema.host_cache;

4. 或者等待該記錄從host cache中被擠掉。

 

如果要禁止DNS解析,可設置skip_name_resolve參數,這樣,mysql.user表中基於主機名的授權將無法使用,且錯誤日志中會提示:

[Warning] 'user' entry 'root@mysql-slave1' ignored in --skip-name-resolve mode.

這里,通過mysql-slave1訪問,將會拒絕訪問

[root@mysql-slave1 ~]# mysql -h192.168.244.145 -uroot -p123
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'192.168.244.144' (using password: YES)

 

host cache是默認開啟的,如果要禁掉,可將host_cache_size設置為0,該參數是個動態參數,可在線修改。

 

如果要完全禁掉TCP/IP連接,可在MySQL啟動時,設置skip-networking參數。

 

總結:

1. 從原理上看,DNS解析一般只針對客戶端的第一次連接,客戶端數據量比較小的情況下,開銷其實不大,完全不必禁掉skip_name_resolve參數,帶來的好處就是,為客戶端和多變的IP直接解耦,只需對主機名進行一次授權。

可通過\s查看當前連接使用的是socket還是TCP。

2. 奇怪的是,對於skip_name_resolve參數,雖然官方文檔說的是布爾值,

但如果在配置文件中指定了,無論是skip_name_resolve=off或者skip_name_resolve=0。

最后,通過show variables like '%skip_name_resolve%'查看均顯示ON。將該參數設置為OFF的唯一辦法是不寫該參數(因為它默認值即為OFF)。

3. 在skip_name_resolve=ON的情況下,在本地通過-h127.0.0.1沒有問題。

[root@localhost ~]# mysql -uroot -h127.0.0.1 -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.31-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

root@(none) 09:02:15> \s
--------------
mysql  Ver 14.14 Distrib 5.6.31, for Linux (x86_64) using  EditLine wrapper

Connection id:        4
Current database:    
Current user:        root@127.0.0.1
SSL:            Not in use
Current pager:        stdout
Using outfile:        ''
Using delimiter:    ;
Server version:        5.6.31-log MySQL Community Server (GPL)
Protocol version:    10
Connection:        127.0.0.1 via TCP/IP
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
TCP port:        3306
Uptime:            11 min 10 sec

Threads: 1  Questions: 20  Slow queries: 0  Opens: 70  Flush tables: 1  Open tables: 63  Queries per second avg: 0.029
--------------

root@(none) 09:02:18> show variables like '%skip_name_resolve%';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| skip_name_resolve | ON    |
+-------------------+-------+
1 row in set (0.06 sec)

 

如果該參數設置為OFF,則上述方式就會報錯,通過報錯信息可以看出,它直接將127.0.0.1轉化為localhost了。

[root@localhost ~]# mysql -uroot -h127.0.0.1 -p123456 -P3306
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

注意: 't1'@'%'中包含't1'@'127.0.0.1',如果開啟skip_name_resolve參數,則't1'@'%'中定義的密碼可用於't1'@'127.0.0.1'的登錄,如果沒有開啟該參數,則't1'@'127.0.0.1'會轉化為't1'@'localhost'登錄,此時't1'@'%'定義的密碼並不適用。

 

參考:

1. http://www.tuicool.com/articles/7R7BRb

2. http://dev.mysql.com/doc/refman/5.7/en/host-cache.html

3. http://dev.mysql.com/doc/refman/5.7/en/blocked-host.html

 


免責聲明!

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



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