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

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

<!-- 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)