无法连接到mysql 127.0.0.1但可以在localhost上连接

时间:2015-01-22 21:20:04

标签: mysql localhost privileges grant

C:\Users\dp\Desktop>mysql -u user -pXXXX -h 127.0.0.1 -P 3306
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'mywayuser'@'localhost' (using passwo
rd: YES)

C:\Users\dp\Desktop>mysql -u user -pXXXX -h localhost -P 3306
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 14
Server version: 5.6.21-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, 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.

mysql> exit

我已经为127.0.0.1授予了所有特权。

我做错了什么?

1 个答案:

答案 0 :(得分:4)

user@localhostuser@127.0.0.1是MySql中的不同凭据。对于Mysql,localhost的主机基本上是#34;通过套接字连接,而不是IP"。

要在尝试时通过套接字将locallicity连接到localhost,您需要在mysql.user表的localhost专门为所需用户创建一个条目:

 CREATE USER 'user'@'localhost'  IDENTIFIED BY PASSWORD 'xxxxx';

即使抓住所有"%"不会工作。如果要使用它,则必须具有localhost条目。然后为该用户授予权限。

或者您可以使用ip 127.0.0.1或任何其他主机名进行连接,该主机名映射到特殊主机127.0.0.1以外的localhost

mysql -u user -pXXXX -h 127.0.0.1 -P 3306

是的,如果mysql没有像这样对待localhost,我会更喜欢,但它就是......