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

我连接到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进行比较。用户表主机名。 检查用户是否存在。 检查host中是否包含IP地址或主机名。

在工作中,您很有可能多次遇到这个问题。这个问题发生在我的大多数时候,由于输入错误的用户名或密码。虽然这是其中一个原因,但还有很多其他的机会你可能会遇到这个问题。有时,它看起来非常相似,但当你深入挖掘时,你会发现导致这个错误的多种因素。这篇文章将详细解释大多数常见的原因,并解决这个问题。

可能的原因:

情况1:输入错误:用户名或密码。

这是导致此错误的最常见原因。如果您输入了错误的用户名或密码,您肯定会得到这个错误。

解决方案:

解决这类错误的方法非常简单。只需输入正确的用户名和密码。此错误将被解决。万一你忘记密码,你可以重置用户名/密码。 如果您忘记了admin / root帐户的密码,有许多方法可以重置/重新获取root密码。我将发布另一篇关于如何重置根密码,如果你忘记根密码。

案例2:从错误的主机访问。

MySQL提供了基于主机的用户访问限制作为安全特性。在我们的生产环境中,我们习惯于将访问请求限制为应用程序服务器。这个特性在许多生产场景中非常有用。

解决方案:

When you face this type of issue, first check whether your host is allowed or not by checking the mysql.user table. If it is not defined, you can update or insert new record to mysql.user table. Generally, accessing as a root user from remote machine is disabled and it is not a best practice, because of security concerns. If you have requirements to access your server from multiple machines, give access only to those machines. It is better not to use wildcards (%) and gives universal accesses. Let me update the mysql.user table, now the demouser can access MySQL server from any host.

情况3:服务器上不存在用户。

当您试图访问的用户在MySQL服务器上不存在时,就会发生这种类型的错误。

解决方案:

当你遇到这种类型的问题,只要检查用户是否存在于mysql。用户表。如果记录不存在,则用户无法访问。如果要求该用户访问,则使用该用户名创建一个新用户。

案例4:混合使用基于数字和名称的主机。

重要的几点

在定义用户主机时不建议使用通配符,尽量使用确切的主机名。 从远程机器禁用root登录。 使用代理用户概念。

与此主题相关的其他概念很少,详细讨论这些主题是本文的不同范围。我们将在接下来的文章中研究以下相关主题。

如果你忘记了MySQL服务器的root密码,该怎么办? MySQL访问权限问题和用户相关的表。 具有最佳实践的MySQL安全特性。

我希望这篇文章将帮助你修复MySQL错误代码1045拒绝用户访问MySQL。

其他回答

我希望你在mysql中删除debian-sys- maintenance用户没有造成更大的损害

让你的mysql守护进程正常运行。启动mysql客户端,如下所示

mysql -u debian-sys-maint -p

在另一个终端,cat /etc/mysql/debian.cnf文件。该文件包含密码;当提示输入密码时,粘贴该密码。

http://ubuntuforums.org/showthread.php?t=1836919

对我来说,这个问题是由MySQL 5.7.2的一个新特性引起的:如果用户的插件字段为空,用户条目将被忽略。

设置为mysql_native_password以重新启用它们:

UPDATE user SET plugin='mysql_native_password' WHERE user='foo';
FLUSH PRIVILEGES;

请参阅MySQL 5.7.2的发布说明,在«Authentication notes»下。

由于某些原因(可能是因为我在4.1之前的密码散列被删除了),mysql_upgrade脚本没有设置默认的插件值。

我是通过注意/var/log/mysql/error.log中的以下警告消息发现的:

[警告]用户条目“foo”@“%”的插件值为空。该用户将被忽略,任何人都不能再使用该用户登录。

我把这个答案贴在这里,也许是为了让别人不用像我一样花那么多荒谬的时间来研究这个问题。

Try:

~$ mysql -u root -p
Enter Password:

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

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

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

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

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

3307 => 3306工作!

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

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

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