重置Linux下MySQL root用户密码的方法

如果忘记MySQL root用户的密码,我们可以登录到Linux系统里,通过修改my.cnf配置文件来重置,具体的操作如下。

1、编辑 /etc/my.cnf 文件,在[mysqld]下面增加一行 skip-grant-tables ,保存退出。

mysql skip grant tables

2、重启MySQL服务,我用的是MariaDB,操作方式一样。

systemctl restart mariadb.service

3、此时,我们就可以以无密码的方式登录root用户,提示输入root密码的时候,直接回车。

mysql root无密码登录

4、修改root用户的密码,“wker.com”为密码,一行一行运行下面三条命令。

update mysql.user set password=password('wker.com') where User="root" and Host="localhost";

flush privileges;

grant all on *.* to 'root'@'localhost' identified by 'wker.com' with grant option;

mysql update root用户登录密码

5、再次编辑 /etc/my.cnf 文件,删除原来的 skip-grant-tables 这行,重启 MySQL 服务。此时,我们就可以用新设置的root密码登录了。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>