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

我连接到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)的访问被拒绝


当前回答

所以对我来说,这个问题与我映射的端口有关。

3306 => 3306不工作(因为我有一个本地mysql运行)

3307 => 3306工作!

这是在建立ssh隧道的上下文中:

ssh -N -L 3307:rdsDns:3306 ec2User@ec2Dns -i key.pem -v

3307为本端端口,3306为远端端口。

其他回答

您可能有一个匿名用户“@'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运行在不区分大小写的操作系统(如Windows)上,也可能发生这种情况。

例:我发现尝试使用这些凭据连接到数据库失败:

mysql>授予数据库ev105的select权限。*到'specialuser',标识为's3curepa5wrd';

$ mysql -specialuser' -p's3curepa5wrd' -h10.61.130.89 databaseV105

错误1045(28000):拒绝访问用户'specialuser'@'10.0.1.113'(使用密码:YES)

但是,这成功了:

Mysql >授予select权限*到'specialuser',标识为's3curepa5wrd';

mysql ' - h10300,300,400 database105 -p

输入密码:

省得你自己头疼……您的问题可能是口令周围没有引号。至少我的案子绕了我三个小时。

[client]
user = myusername
password = "mypassword"   # <----------------------- VERY IMPORTANT (quotes)
host = localhost

http://dev.mysql.com/doc/refman/5.7/en/option-files.html

搜索“这是一个典型的用户选项文件:”,看看他们在那里陈述的例子。祝你好运,我希望能为别人节省一些时间。

我有类似的问题,因为我的密码包含“;”字符破坏我的密码时,我第一次创建它。如果对你有帮助,请谨慎使用。