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

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


当前回答

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

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

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

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

其他回答

调试总结

检查输入错误:用户名或密码。 检查主机名并与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时没有指定-p参数。

mysql -p

我没有问题,但它是错误的调用mysql没有密码。

当你逃离

mysql -u bill -p

得到了这个错误

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

Mysqld希望您以bill@localhost的身份连接

尝试创建bill@localhost

CREATE USER bill@localhost IDENTIFIED BY 'passpass';
grant all privileges on *.* to bill@localhost with grant option;

如果要远程连接,必须指定DNS名称、公共IP或127.0.0.1,使用TCP/IP:

mysql -u bill -p -hmydb@mydomain.com
mysql -u bill -p -h10.1.2.30
mysql -u bill -p -h127.0.0.1 --protocol=TCP

登录后,请运行这个

SELECT USER(),CURRENT_USER();

USER()报告您如何尝试在MySQL中进行身份验证

CURRENT_USER()报告你如何被允许从MySQL认证MySQL。用户表

这将使您更好地了解如何以及为什么允许您登录mysql。为什么知道这个观点很重要?它与用户身份验证排序协议有关。

下面是一个例子:我将在我的桌面MySQL上创建一个匿名用户

mysql> select user,host from mysql.user;
+---------+-----------+
| user    | host      |
+---------+-----------+
| lwdba   | %         |
| mywife  | %         |
| lwdba   | 127.0.0.1 |
| root    | 127.0.0.1 |
| lwdba   | localhost |
| root    | localhost |
| vanilla | localhost |
+---------+-----------+
7 rows in set (0.00 sec)

mysql> grant all on *.* to x@'%';
Query OK, 0 rows affected (0.02 sec)

mysql> select user,host from mysql.user;
+---------+-----------+
| user    | host      |
+---------+-----------+
| lwdba   | %         |
| mywife  | %         |
| x       | %         |
| lwdba   | 127.0.0.1 |
| root    | 127.0.0.1 |
| lwdba   | localhost |
| root    | localhost |
| vanilla | localhost |
+---------+-----------+
8 rows in set (0.00 sec)

mysql> update mysql.user set user='' where user='x';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> select user,host from mysql.user;
+---------+-----------+
| user    | host      |
+---------+-----------+
|         | %         |
| lwdba   | %         |
| mywife  | %         |
| lwdba   | 127.0.0.1 |
| root    | 127.0.0.1 |
| lwdba   | localhost |
| root    | localhost |
| vanilla | localhost |
+---------+-----------+
8 rows in set (0.00 sec)

mysql>

好吧,看我以匿名用户登录:

C:\MySQL_5.5.12>mysql -urol -Dtest -h127.0.0.1 --protocol=TCP
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.5.12-log MySQL Community Server (GPL)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select user(),current_user();
+---------------+----------------+
| user()        | current_user() |
+---------------+----------------+
| rol@localhost | @%             |
+---------------+----------------+
1 row in set (0.00 sec)

mysql>

认证顺序非常严格。它从最具体的到最不具体的。我在DBA StackExchange中写过这种身份验证样式。

不要忘记在必要时显式地调用TCP作为mysql客户端的协议。

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

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

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

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

mysql -u root

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

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

百分号表示所有ip,所以localhost是多余的…本地主机不需要第二条记录。

实际上有,'localhost'在mysql中是特殊的,它意味着通过unix套接字(或windows上的命名管道)连接,而不是TCP/IP套接字。使用%作为主机不包括'localhost'

MySQL用户帐户有两个组成部分:用户名和主机名。用户名标识用户,主机名指定用户可以从哪些主机连接。用户名和主机名的组合:

< user_name >的@ < host_name >的 您可以为主机名指定特定的IP地址或地址范围,或者使用百分比字符(“%”)使该用户能够从任何主机登录。

注意,用户帐户是由用户名和主机名同时定义的。例如,“root”@“%”与“root”@“localhost”是不同的用户帐户。