机器A --> 192.168.103.94 -- 服务器
机器B --> 192.168.4.2 -- 客户端
机器A =>
装上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中添加:
192.168.4.2 mysql_client_host
在mysql中添加用户testdns,host为192.168.4.2
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 =>
准备一套jdbc的测试程序:
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解析确实在起作用