无法通过我刚设置的用户详细信息从bash连接到MySQL数据库

时间:2012-06-27 11:05:06

标签: mysql macos

我刚刚创建了一个带有密码的新MySQL用户,并授予他们对数据库的所有权限。现在我想用我刚刚设置的密码连接到那个数据库,我不能:

mysql> create user 'reviews' identified by 'reviews';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on reviews.* to 'reviews'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit;
Bye
$ mysql -U reviews -u reviews -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'reviews'@'localhost' (using password: YES)

我做错了什么?我在一个bash shell中运行MacOS Lion,版本是MySQL的10.6版。

1 个答案:

答案 0 :(得分:0)

定义用户时,CREATE USER语句需要主机部分。否则它将使用默认的'%',因为GRANT语句包含@'localhost',你最终会得到一个有密码的用户,而另一个没有。

mysql> CREATE USER 'reviews' IDENTIFIED BY 'reviews';
mysql> GRANT ALL ON reviews.* TO 'reviews'@'localhost';

结果:

+---------+-------------------------------------------+-----------+
| user    | password                                  | host      |
+---------+-------------------------------------------+-----------+
| reviews | *03E3A1FBF4E68AB8F641C43AF6E952AFD967AFEA | %         |
| reviews |                                           | localhost |
+---------+-------------------------------------------+-----------+

使用以下内容获得您想要的内容:

mysql> CREATE USER 'reviews'@'localhost' IDENTIFIED BY 'reviews';
mysql> GRANT ALL ON reviews.* TO 'reviews'@'localhost';

请注意,最好在连接时指定数据库,否则在连接后需要更改默认数据库:

shell> mysql -u reviews -p -D reviews