mysql、tcp/ip 和密码

mysql、tcp/ip 和密码

我不知道为什么会发生这种事。

我的用户有两个帐户: ans@localhost用于套接字连接和ans@%用于 TCP/IP 连接。我的主机名是valiant

mysql> select host,user,password from mysql.user where user = 'ans';
+-----------+------+-------------------------------------------+
| host      | user | password                                  |
+-----------+------+-------------------------------------------+
| localhost | ans  | *40BA1B1CA7EE7B1B5FE4B2B754F74CD789DA6959 |
| %         | ans  |                                           |
+-----------+------+-------------------------------------------+
2 rows in set (0.05 sec)

请注意,该ans@%帐户没有密码。

$ mysql -uans -hvaliant
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 221
Server version: 5.1.73 Source distribution
mysql>

我可以不使用密码登录,这意味着我正在使用我的 TCP/IP 帐户,我可以使用以下命令进行验证\s

mysql> \s
Current user:           ans@valiant
Connection:             valiant via TCP/IP

因此我给该帐户指定了一个密码。

mysql> set password for 'ans'@'%' = password('foo');
Query OK, 0 rows affected (0.00 sec)
mysql> set password for 'ans'@'localhost' = password('foo');
Query OK, 0 rows affected (0.00 sec)
mysql> select host,user,password from mysql.user where user = 'ans';
+-----------+------+-------------------------------------------+
| host      | user | password                                  |
+-----------+------+-------------------------------------------+
| localhost | ans  | *F3A2A51A9B0F2BE2468926B4132313728C250DBF |
| %         | ans  | *F3A2A51A9B0F2BE2468926B4132313728C250DBF |
+-----------+------+-------------------------------------------+
2 rows in set (0.00 sec)

我可以通过本地套接字登录

$ mysql -uans -pfoo
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 233
Server version: 5.1.73 Source distribution   
mysql>

但不通过 TCP/IP

$ mysql -uans -pfoo -hvaliant
ERROR 1045 (28000): Access denied for user 'ans'@'valiant' (using password: YES)

我错过了什么?

答案1

很典型。我一问问题,就找不到答案。

mysql> select host,user,password from mysql.user ;
+-----------+------+-------------------------------------------+
| host      | user | password                                  |
+-----------+------+-------------------------------------------+
| localhost | root | *64B2393C4607E57C549B41537134BC7C3314164E |
| localhost |      |                                           |
| valiant   |      |                                           |
| localhost | ans  | *F3A2A51A9B0F2BE2468926B4132313728C250DBF |
| %         | ans  | *F3A2A51A9B0F2BE2468926B4132313728C250DBF |
+-----------+------+-------------------------------------------+
5 rows in set (0.00 sec)

mysql有帮助地(?)创建了两个具有空白用户名和空白密码的用户,允许任何用户无需密码即可从该主机进行连接。

mysql> drop user ''@'valiant';
Query OK, 0 rows affected (0.00 sec)

$ mysql -uans -pfoo -hvaliant
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 249
Server version: 5.1.73 Source distribution

相关内容