機器A --> 192.168.103.94 -- 服務器java
機器B --> 192.168.4.2 -- 客戶端mysql
機器A =>sql
裝上mysql-server,啓動; 使用root用戶登陸,查看DNS解析是否開啓,執行以下命令:緩存
mysql> show variables like 'skip_name_resolve%'; +-------------------+-------+ | Variable_name | Value | +-------------------+-------+ | skip_name_resolve | OFF | +-------------------+-------+ 1 row in set (0.00 sec)
說明DNS解析是開啓狀態服務器
在/etc/hosts中添加:ide
192.168.4.2 mysql_client_host測試
在mysql中添加用戶testdns,host爲192.168.4.2this
mysql> grant all privileges on *.* to testdns@'192.168.4.2' identified by '000'; mysql> select user,host,password from user where user='testdns'; +---------+-------------+-------------------------------------------+ | user | host | password | +---------+-------------+-------------------------------------------+ | testdns | 192.168.4.2 | *B66FEB4CF5216D971C066EB920EC03B27A5A3F40 | +---------+-------------+-------------------------------------------+ mysql> flush privileges;
機器B =>
url
準備一套jdbc的測試程序:spa
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://192.168.103.94:3306/test
db.username=testdns
db.password=000
執行「select 1」查詢語句:查詢成功!
機器A=> 查看鏈接進程列表:
mysql> show processlist; +----+---------+------------------------+-------+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+---------+------------------------+-------+---------+------+-------+------------------+ | 3 | root | localhost | mysql | Query | 0 | NULL | show processlist | | 4 | testdns | mysql_client_host:4192 | test | Sleep | 2 | | NULL | +----+---------+------------------------+-------+---------+------+-------+------------------+
這裏顯示的主機名爲mysql_client_host,說明dns解析起做用了
繼續猜想當將testdns的host設置爲mysql_client_host也能夠,來印證下:
mysql> update user set host='mysql_client_host' where user='testdns'; mysql> select user,host,password from user where user='testdns'; +---------+-------------------+-------------------------------------------+ | user | host | password | +---------+-------------------+-------------------------------------------+ | testdns | mysql_client_host | *B66FEB4CF5216D971C066EB920EC03B27A5A3F40 | +---------+-------------------+-------------------------------------------+ mysql> flush privileges;
在機器B上測試 執行「select 1」查詢語句:查詢成功!
A機器上將/etc/hosts中的192.168.4.2 mysql_client_host 註釋掉, 而且mysql命令行執行flush hosts,由於mysql會緩存dns解析記錄;
B機器上再次測試,
java.sql.SQLException: null, message from server: "Host '192.168.4.2' is not allowed to connect to this MySQL server"
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:996)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:924)
小節:以上測試證實mysql的dns解析確實在起做用