您的位置:首页 > 数据库 > MySQL

mysql在本地无法使用密码登陆

2014-12-15 00:00 260 查看
摘要: mysql为新用户添加了权限,但仍然提示Access denied

为自己添加了访问数据库权限:

<!-- lang: shell -->
mysql> CREATE USER 'caohong'@'%' IDENTIFIED BY '123456';
mysql> GRANT ALL PRIVILEGES ON testdb.* to 'caohong'@'%' WITH GRANT OPTION;

结果提示访问失败:

<!-- lang: shell -->
mysql -u caohong -p
Password:
ERROR 1045 (28000): Access denied for user 'caohong'@'localhost' (using password: YES)

但可以无密码的方式可以登陆
mysql -u caohong

解决方法参考stackoverflow

说明:

查一下无密码登陆的当前账号:

<!-- lang: shell -->
mysql> select user(), current_user();
+-------------------+----------------+
| user()            | current_user() |
+-------------------+----------------+
| caohong@localhost | @localhost     |
+-------------------+----------------+
1 row in set (0.08 sec)

用无密码的方式登陆localhost,实际上使用的是匿名用户,可以用root密码看一下,当前允许使用匿名用户:

<!-- lang: shell -->
mysql --user=root mysql
mysql> select user, host from mysql.user;
+---------+-----------+
| user    | host      |
+---------+-----------+
| caohong | %         |
| test    | %         |
| root    | 127.0.0.1 |
| root    | ::1       |
|         | localhost |
| root    | localhost |
| test    | localhost |
+---------+-----------+

解决方法是删除这个匿名用户:

mysql> drop user ''@'localhost';
Query OK, 0 rows affected (0.09 sec)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql
相关文章推荐