朋友问为什么在mysql的server上不能以localhost的方式登录mysql?
错误信息为:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
问:密码是否正确?
答:用同样的用户,以127.0.0.1的方式就能连接登录。
mysql.user中的信息如下:
mysql> select user,password,host from mysql.user where user=‘root’;
+---------+-------------------------------------------+----------------+
| user | password | host |
+---------+-------------------------------------------+----------------+
| root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | localhost |
| root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | 127.0.0.1 |
| root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | ::1 |
+---------+-------------------------------------------+----------------+
3 rows in set (0.00 sec)
$mysql -uroot -p -P 3336 -h localhost
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
$mysql -uroot -p -h 127.0.0.1 -P3336
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
这是因为如果用localhost作为主机名,mysql使用套接字连接,而不是TCP连接。
如果想使用localhost的方式连接mysql,可以指定--protocol=TCP
$ mysql -uroot -p --protocol=TCP -P 3336 -h localhost
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.