以管理员身份为root用户设置新密码:
[root ~]# mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password:NO)
停止运行mysql的服务/守护进程
[root ~]# service mysql stop
mysql stop/waiting
使用以下选项启动没有任何权限的mysql; 此选项用于启动,不使用MySQL的权限系统。
[root ~]# mysqld_safe --skip-grant-tables &
此时,终端似乎停了下来。那就是,并使用新的终端进行后续步骤。
进入mysql命令提示符
[root ~]# mysql -u root
mysql>
修复root用户的权限设置;
mysql> use mysql;
Database changed
mysql> select * from user;
Empty set (0.00 sec)
mysql> truncate table user;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> grant all privileges on . to root@localhost identified by 'YourNewPassword' with grant option;
Query OK, 0 rows affected (0.01 sec)
*如果您不想要任何密码或更确切地说是空密码
mysql> grant all privileges on *.* to root@localhost identified by '' with grant option;
Query OK, 0 rows affected (0.01 sec)*
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
确认结果:
mysql> select host, user from user;
+-----------+------+
| host | user |
+-----------+------+
| localhost | root |
+-----------+------+
1 row in set (0.00 sec)
退出shell并以正常模式重启mysql。
mysql> quit;
[root ~]# kill -KILL [PID of mysqld_safe]
[root ~]# kill -KILL [PID of mysqld]
[root ~]# service mysql start
现在,您可以使用您设置的密码以root用户身份成功登录
[root ~]# mysql -u root -pYourNewPassword
mysql>