今天安装mysql,然后输入mysql -u root -p,出现enter password,我直接点击回车,结果出现如果下错误:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)。
解决方法:
#1.停止mysql数据库
[root@wdsx]# service mysqld stop
#2.找到MySQL的配置文件my.cnf
[root@wdsx]# vi /etc/my.cnf
#3.在my.cnf里添加:
skip-grant-tables
#4.重启MySQL:
[root@wdsx]# service mysqld restart
#5.使用root登录mysql数据库
[root@wdsx]# mysql -u root
#6.更新root密码
mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
#最新版MySQL请采用如下SQL:
mysql> UPDATE user SET authentication_string=PASSWORD('newpassword') where USER='root';
#7.刷新权限
mysql> FLUSH PRIVILEGES;
#8.退出mysql
mysql> quit;
#9.注释或者删除my.cnf里的:
#skip-grant-tables
#10.重启mysql
[root@wdsx]# service mysqld restart
#11.使用root用户重新登录mysql
[root@wdsx]# mysql -uroot -p
Enter password: <输入新设的密码newpassword>