mysql忘记root密码

时间:2022-04-13 00:33:43

好久没有用电脑登录mysql_server了,今天登录竟然忘记root密码了。记录一下重置root密码的过程:

关闭运行的mysql_svr

cd /usr/local/mysql/support-files
./mysql.server stop

以特殊方式启动mysql_svr(可以不用输入密码)

cd /usr/local/mysql/bin
./mysqld_safe --skip-grant-tables &

登录mysql

mysql -u root

更新密码

select * from mysql.user;   查看db的用户信息
update mysql.user set authentication_string=password('test') where user='root'; //更新root用户的密码为 test
flush privileges; //刷新写入

重新登录

mysql -u root -ptest      //可以登录到db

查询db信息的时候会报错:

If the ALTER USER statement fails to reset the password, try repeating the procedure using the following statements to modify the user table directly

需要重新设置一遍密码:

SET PASSWORD = PASSWORD('test'); 

然后就可以使用重置的密码正常操作db了~

更换root密码

set password for root@localhost = password('123');   //登录db后,更换root密码为123

参考:http://blog.csdn.net/u014410695/article/details/50630233

FAQ:
* 很多帖子用这种方式更换密码:

set password for 'root'@'localhost'='123';

但是实际会报这个问题:

You are using MySQL as an anonymous user and anonymous users are not allowed to change passwords。
或者:
The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement