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

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


当前回答

MySQL account names consist of a user name and a host name, The name 'localhost' in host name indicates the local host also You can use the wildcard characters “%” and “_” in host name or IP address values. These have the same meaning as for pattern-matching operations performed with the LIKE operator. For example, a host value of '%' matches any host name, whereas a value of '%.mysql.com' matches any host in the mysql.com domain. '192.168.1.%' matches any host in the 192.168.1 class C network.

以上只是介绍:

实际上,用户“bill”@“localhost”和“bill”@“%”是不同的MySQL帐户,因此两者都应该使用自己的身份验证细节,如密码。

欲了解更多信息,请参阅http://dev.mysql.com/doc/refman//5.5/en/account-names.html

其他回答

Try:

~$ mysql -u root -p
Enter Password:

mysql> grant all privileges on *.* to bill@localhost identified by 'pass' with grant option;

对于Mac用户,如果他们仍然有问题(就像我的情况),我发现这个解决方案为我工作:MySQL命令行'-bash命令未找到'

使用命令,即mysql在macbook终端上,你需要导出路径使用:

export PATH=$PATH:/usr/local/mysql/bin/

考虑默认安装,以root用户使用以下命令获取mysql提示符:

mysql -u root

否则您使用了错误的根密码。

参考:在OSX上设置MySQL root用户密码

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

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的行为是预期的,'%'将授予您登录。

这是以下两者之间的区别:

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

and

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

检查:

mysql> select user,host from mysql.user;
+---------------+----------------------------+
| user          | host                       |
+---------------+----------------------------+
| bill          | %                          | <=== created by first
| root          | 127.0.0.1                  |
| root          | ::1                        |
| root          | localhost                  |
| bill          | localhost                  | <=== created by second
+---------------+----------------------------+

命令

mysql -u bill -p

隐式访问'bill'@'localhost',而不是'bill'@'%'。

“bill”@“localhost”没有权限

你会得到错误:

ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)

解决问题:

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

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