首先让我提一下,我已经看了很多建议的问题,但没有找到相关的答案。以下是我正在做的事情。
我连接到我的亚马逊EC2实例。我可以用这个命令以MySQL根目录登录。
mysql -u root -p
然后我创建了一个新的用户账单,主机为%。
CREATE USER 'bill'@'%' IDENTIFIED BY 'passpass';
给予用户bill所有的权限。
grant all privileges on *.* to 'bill'@'%' with grant option;
然后我从根用户退出,尝试用bill登录。
mysql -u bill -p
输入正确的密码,得到了这个错误。
ERROR 1045(28000)。用户'bill'@'localhost'(使用密码:YES)的访问被拒绝。
当你运行
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或使用TCP/IP的127.0.0.1。
mysql -u bill -p [email protected]
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.user表中进行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>
OK看我以匿名用户身份登录。
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客户端的协议。
当你输入 "mysql -u root -p "时,你正在通过本地unix套接字连接到mysql服务器。
然而,你给的授权,'bill'@'%'
只匹配TCP/IP连接,这很奇怪。
如果你想授予本地unix套接字的访问权,你需要授予'bill'@'localhost'的权限,奇怪的是,这与'bill'@'127.0.0.1'不同。
你也可以使用TCP/IP与mysql命令行客户端连接,以配合你已经授予的权限,例如,运行mysql -u root -p -h 192.168.1.123
或你的盒子有的任何本地IP地址。