小编给大家分享一下解决mysql忘了root密码的方法,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

mysql忘了root密码解决方法:

1、修改配置文件my.cnf,在配置文件[mysqld]下添加skip-grant-tables,重启MySQL服务即可免密码登录

#SERVERSECTION#----------------------------------------------------------------------##ThefollowingoptionswillbereadbytheMySQLServer.Makesurethat#youhaveinstalledtheservercorrectly(seeabove)soitreadsthis#file.#[mysqld]skip-grant-tables

2、用空密码的 root 用户连接到 MySQL,并且更改 root 口令:

[root@localhostmysql]#mysql-urootWelcometotheMySQLmonitor.Commandsendwith;or\g.YourMySQLconnectionidis53Serverversion:5.0.41-community-logMySQLCommunityEdition(GPL)Type'help;'or'\h'forhelp.Type'\c'toclearthebuffer.mysql>mysql>ALTERUSER'root'@'localhost'IDENTIFIEDWITHmysql_native_passwordBY'123';ERROR1290(HY000):TheMySQLserverisrunningwiththe--skip-grant-tablesoptionsoitcannotexecutethisstatementmysql>updateusersetauthentication_string=NULLwhereuser='root';QueryOK,1rowaffected(0.00sec)

上面是先用grant的方式修改root密码,但是由于使用了配置了skip-grant-tables 选项,使用“alter user”命令更改密码失败,直

接更新 user 表的authentication_string字段后更改密码成功。

3、到my.cnf 中删除skip-grant-tables选项,然后直接用mysql -uroot免密码登录后,在执行如下语句重设密码

mysql>ALTERUSER'root'@'localhost'IDENTIFIEDWITHmysql_native_passwordBY'123';

看完了这篇文章,相信你对解决mysql忘了root密码的方法有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!