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

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


当前回答

sudo -i 
mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';

访问mysql服务不需要登录(可以看到因为在shell mysql>)

其他回答

这可能只适用于少数人,但事实就是这样。不要用感叹号!在你的密码里。

我做到了,并使用MariaDB得到了上述错误。当我把它简化成数字和字母时,它是可行的。其他字符,如@和$工作得很好-我在同一实例的不同用户中使用了这些字符。

这个地址的第五次回复让我找到了解决办法。

确保没有其他正在运行的SQL实例正在使用本地主机帖子。在我们的例子中,本地主机上运行的另一个实例与登录冲突。把它关掉就解决了这个问题。

在我的案例中,一个相关的问题是试图连接使用:

mysql -u mike -p mypass

空格显然是允许在-u #uname#之间,但不允许在-p和#password#之间

因此需要:

mysql -u mike -pmypass

否则,在空格之间-p mpass mysql以' mpass '作为数据库名

当你逃离

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客户端的协议。

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

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;