【问题标题】:Can't connect to mysql 127.0.0.1 but can connect on localhost无法连接到 mysql 127.0.0.1 但可以在 localhost 上连接
【发布时间】:2015-01-22 21:20:03
【问题描述】:
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 授予了所有权限。

我做错了什么?

【问题讨论】:

    标签: mysql localhost privileges sql-grant


    【解决方案1】:

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

    要在尝试时通过套接字显式连接到 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,我会更喜欢,但它就是……

    【讨论】:

    • 这对我在将 MarinaDB 迁移到另一台服务器后帮助很大。
    猜你喜欢
    • 2018-04-24
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    • 2021-09-20
    • 2012-02-12
    • 2019-11-05
    相关资源
    最近更新 更多