使用mysql数据库时,遇到如下错误:
ERROR 1044 (42000): Access denied for user ”@’localhost’ to database ‘mysql’
这是由于mysql数据库的user表里,存在用户名为空的账户即匿名账户,导致登录的时候是虽然用的是root,但实际是匿名登录的,通过错误提示里的”@’localhost’可以看出来。
具体解决步骤如下:
1.关闭mysql
# service mysqld stop
2.屏蔽权限
# mysqld_safe --skip-grant-table
or
# /usr/bin/mysqld_safe --skip-grant-table
Starting demo from .....
3.新开起一个终端输入
# mysql -u root mysql
mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';//重置root密码
mysql> delete from user where USER='';//删除空用户
mysql> FLUSH PRIVILEGES;//记得要这句话,否则如果关闭先前的终端,又会出现原来的错误
mysql> exit
4.重启mysql
# service mysqld restart
其他问题
问题1:
对于mysql 5.7,使用时可能遇到:
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
要求重置密码,但重置root密码时:
“the unknown field Password error above use”
则使用:
mysql>set authentication_string=password('my_password') where user='root';
或者
mysql> set password=password('new_password');
问题2:
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
提示密码复杂度不够,密码要包括数字,大小写,特殊符号等
mysql> set password=password('!XXXXX);
Query OK, 0 rows affected, 1 warning (0.00 sec)
参考:
http://www.cnblogs.com/Anker/p/3551610.html
http://myway84.iteye.com/blog/1001915
https://superuser.com/questions/603026/mysql-how-to-fix-access-denied-for-user-rootlocalhost