首先让我提一下,我已经看了很多建议的问题,但没有找到相关的答案。这就是我正在做的。

我连接到Amazon EC2实例。我可以用这个命令登录MySQL根目录:

mysql -u root -p

然后我用host %创建了一个新的用户帐单

CREATE USER 'bill'@'%' IDENTIFIED BY 'passpass';

授予用户bill的所有权限:

grant all privileges on *.* to 'bill'@'%' with grant option;

然后我退出root用户,尝试用bill登录:

mysql -u bill -p

输入正确的密码并得到以下错误:

错误1045(28000):用户“账单”@“localhost”(使用密码:YES)的访问被拒绝


当前回答

当你的密码包含一些特殊字符,如@,$等,也会发生这种情况。 为了避免这种情况,你可以把password用单引号括起来:

$ mysql -usomeuser -p's0mep@$$w0Rd'

或者在输入时不用密码。请留空,然后在终端要求时输入。这是推荐的方法。

$ mysql -usomeuser -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 191
Server version: 5.5.46-0ubuntu0.14.04.2 (Ubuntu)

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

其他回答

您可能有一个匿名用户“@'localhost'或“@'127.0.0.1'”。

根据手册:

当可能有多个匹配时,服务器必须确定其中的哪一个 使用它们。它解决这个问题如下:(…) 当客户端尝试连接时,服务器会检查mysql表的[行。用户]。 服务器使用与客户端主机名和用户名匹配的第一行。 (…) 服务器使用排序规则首先对具有最特定的Host值的行进行排序。 字面主机名[如'localhost']和IP地址是最具体的。

因此,当从本地主机连接时,这样的匿名用户将“屏蔽”任何其他用户,如“[any_username]”@“%”。

'bill'@'localhost'不匹配'bill'@'%',但会匹配(例如)“@事先“localhost”。

推荐的解决方案是删除这个匿名用户(这通常是一件好事)。


下面的编辑大多与主要问题无关。这些只是为了回答本帖其他评论中提出的一些问题。

编辑1

通过套接字验证为'bill'@'%'。


    root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql -ubill -ppass --socket=/tmp/mysql-5.5.sock
    Welcome to the MySQL monitor (...)
    
    mysql> SELECT user, host FROM mysql.user;
    +------+-----------+
    | user | host      |
    +------+-----------+
    | bill | %         |
    | root | 127.0.0.1 |
    | root | ::1       |
    | root | localhost |
    +------+-----------+
    4 rows in set (0.00 sec)
    
    mysql> SELECT USER(), CURRENT_USER();
    +----------------+----------------+
    | USER()         | CURRENT_USER() |
    +----------------+----------------+
    | bill@localhost | bill@%         |
    +----------------+----------------+
    1 row in set (0.02 sec)
    
    mysql> SHOW VARIABLES LIKE 'skip_networking';
    +-----------------+-------+
    | Variable_name   | Value |
    +-----------------+-------+
    | skip_networking | ON    |
    +-----------------+-------+
    1 row in set (0.00 sec)
    

编辑2

完全相同的设置,除了我重新激活了网络,我现在创建了一个匿名用户“@'localhost'。


    root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql
    Welcome to the MySQL monitor (...)
    
    mysql> CREATE USER ''@'localhost' IDENTIFIED BY 'anotherpass';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> Bye

    root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql -ubill -ppass \
        --socket=/tmp/mysql-5.5.sock
    ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)
    root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql -ubill -ppass \
        -h127.0.0.1 --protocol=TCP
    ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)
    root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql -ubill -ppass \
        -hlocalhost --protocol=TCP
    ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)

编辑3

与编辑2中的情况相同,现在提供匿名用户的密码。


    root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql -ubill -panotherpass -hlocalhost
    Welcome to the MySQL monitor (...)

    mysql> SELECT USER(), CURRENT_USER();
    +----------------+----------------+
    | USER()         | CURRENT_USER() |
    +----------------+----------------+
    | bill@localhost | @localhost     |
    +----------------+----------------+
    1 row in set (0.01 sec)

来自编辑1的结论1:可以通过套接字验证为'bill'@'%'。

结论2,来自编辑2:一个人是通过TCP连接还是通过套接字连接对身份验证过程没有影响(除非一个人不能通过套接字以'something'@'localhost'的身份连接,显然)。

来自编辑3的结论3:尽管我指定了-ubill,但我被授予了匿名用户的访问权限。这是因为上面建议的“排序规则”。注意,在大多数默认安装中,都存在一个无密码的匿名用户(应该保护/删除)。

I discovered yet another case that appears on the surface to be an edge case; I can export to the file system, via SELECT INTO .. OUTFILE as root, but not as regular user. While this may be a matter of permissions, I've looked at that, and see nothing especially obvious. All I can say is that executing the query as a regular user who has all permissions on the data base in question returns the access denied error that led me to this topic. When I found the transcript of a successful use of SELECT INTO … OUTFILE in an old project, I noticed that I was logged in as root. Sure enough, when I logged in as root, the query ran as expected.

当你输入mysql -u root -p时,你是在通过本地unix套接字连接到mysql服务器。

不管你给的授权是什么,'bill'@'%'只匹配TCP/IP连接。

如果你想要授予对本地unix套接字的访问权限,你需要授予'bill'@'localhost'权限,奇怪的是,这与'bill'@'127.0.0.1'不一样。

您也可以使用TCP/IP连接mysql命令行客户端,以匹配您已经授予的权限,例如运行mysql -u root -p -h 192.168.1.123或任何您的本地IP地址。

我通过删除旧的错误用户“账单”条目来解决这个问题(这是重要的部分:都来自mysql。用户和mysql.db),然后创建和前面一样的用户:

FLUSH PRIVILEGES;
CREATE USER bill@localhost IDENTIFIED BY 'passpass';
grant all privileges on *.* to bill@localhost with grant option;
FLUSH PRIVILEGES;

工作完毕,用户正在连接。现在我将删除一些特权从它:)

如果您使用旧版本的MySQL UI(如SQLYoug),生成的密码带有错误的散列,也会出现这个问题。

用SQL脚本创建用户可以解决这个问题。