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

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


当前回答

不确定其他人是否会发现这有帮助,但我遇到了同样的错误,并搜索任何匿名用户…但实际上并没有。问题最终是用户帐户被设置为“要求SSL”-这是我在PHPMyAdmin通过进入用户帐户并单击编辑用户的权限发现的。只要我取消了这个选项,一切都按预期工作!

其他回答

如果您忘记密码或想修改密码。你可以遵循以下步骤:

1 :stop your mysql [root@maomao ~]# service mysqld stop Stopping MySQL: [ OK ] 2 :use “--skip-grant-tables” to restart mysql [root@mcy400 ~]# mysqld_safe --skip-grant-tables [root@cy400 ~]# Starting mysqld daemon with databases from /var/lib/mysql 3 : open a new window and input mysql -u root [root@cy400 ~]# mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. 4 : change the user database mysql> use mysql Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed 5 : modify your password your new password should be input in "()" mysql> update user set password=password('root123') where user='root'; Query OK, 3 rows affected (0.00 sec) Rows matched: 3 Changed: 3 Warnings: 0 6 : flush mysql> flush privileges; 7: quit mysql> quit Bye 8: restart mysql [root@cy400 ~]# service mysqld restart; Stopping MySQL: [ OK ] Starting MySQL: [ OK ]

宾果!你可以用用户名和新密码连接你的数据库:

[root@cy400 ~]# mysql -u root -p <br>
Enter password: admin123 <br>
Welcome to the MySQL monitor.  Commands end with ; or \g. <br>
Your MySQL connection id is 2 <br>
Server version: 5.0.77 Source distribution <br>
Type 'help;' or '\h' for help. Type '\c' to clear the buffer. <br>
mysql> quit <br>
Bye

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.

您可能有一个匿名用户“@'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,但我被授予了匿名用户的访问权限。这是因为上面建议的“排序规则”。注意,在大多数默认安装中,都存在一个无密码的匿名用户(应该保护/删除)。

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

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

3307 => 3306工作!

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

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

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

当你运行mysql -u bill -p时,localhost被解析为你的ip,因为它是127.0.0.1,在你的/etc/hosts文件中,默认的127.0.0.1 localhost存在。因此,mysql将您解释为bill@localhost,而不是授予bill@'%'。这就是为什么有2个不同的记录根用户的结果选择主机,用户从mysql.user;查询

有两种方法可以处理这个问题。

一个是指定一个ip,当您尝试登录时,该ip不会被/etc/hosts文件反向解析。例如,服务器的ip为10.0.0.2。执行mysql -u bill -p -h 10.0.0.2命令即可登录。如果输入select user();,将得到bill@10.0.0.2。当然,在/etc/hosts文件中,任何域名都不应该解析为这个ip。

其次,您需要授予该特定域名的访问权限。对于bill@localhost,您应该调用命令授予*上的所有特权。*到bill@localhost,通过“billpass”识别;. 在这种情况下,你可以用mysql -u bill -p命令登录。登录后,选择user();命令返回bill@localhost。

但这仅适用于您尝试登录同一主机上的mysql服务器。从远程主机,mysql的行为是预期的,'%'将授予您登录。