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

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


当前回答

我为自己找到的最好的解决办法是。

我的用户是声纳,每当我试图从外部或其他机器连接到我的数据库,我得到错误

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

此外,我正在尝试从另一台机器和通过詹金斯工作我的URL访问是

alm-lt-test.xyz.com

如果你想远程连接,你可以用以下不同的方式指定它:

mysql -u sonar -p -halm-lt-test.xyz.com
mysql -u sonar -p -h101.33.65.94
mysql -u sonar -p -h127.0.0.1 --protocol=TCP
mysql -u sonar -p -h172.27.59.54 --protocol=TCP

要访问这个URL,你只需要执行以下查询。

GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'alm-lt-test.xyz.com' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'127.0.0.1' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'172.27.59.54' IDENTIFIED BY 'sonar';

其他回答

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

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 UI(如SQLYoug),生成的密码带有错误的散列,也会出现这个问题。

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

更新:在v8.0.15(可能是这个版本),PASSWORD()函数不起作用。

你必须:

确保你已经停止MySQL。 使用特权绕过安全模式运行服务器:sudo mysqld_safe——skip-grant-tables 登录:mysql -u root 更新mysql。SET authentication_string=null WHERE user ='root'; mysql> FLUSH PRIVILEGES; mysql >退出; 再次登录:mysql -u root mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd'

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

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