这篇文章将为大家详细讲解有关mysql忘了root密码的解决方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

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

其中--skip-grant-tables 选项前面曾经介绍过,意思是启动 MySQL 服务的时候跳过权限表认证。 启动后,连接到 MySQL 的 root 将不需要口令。

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

用空密码的 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字段后更改密码成功。

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

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

关于mysql忘了root密码的解决方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。