mysql临时密码忘记了怎么办

发布时间: 2023-11-21 12:42 阅读: 文章来源:1MUMB4163PS

这是一个比较傻的问题,自己安装完MySQL,由于设置了多次密码把密码忘记了,怎么办呢,一顿查解决办法按如下,我的是MySQL8.0.

1、先停止服务

systemctl stop mysqld

2、修改配置文件

修改配置文件,默认文件路径如下/etc/my.cnf增加一行代码skip-grant-tables 如下[mysqld]skip-grant-tables

3、无密码登录,密码不输入直接回车

[root@begincode etc]# mysql -u root -pEnter password:Welcome to the MySQL monitor.Commands end with ; or \g.Your MySQL connection id is 7Server version: 8.0.31 MySQL Community Server - GPLCopyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.mysql>

4、修改密码

mysql> alter user ‘root‘@‘localhost‘ identified by ‘你的密码‘;ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement如果提示上面这个错误 ,输入下面的命令 然后重试mysql> flush privileges;mysql> alter user ‘root‘@‘localhost‘ identified by ‘你的密码‘;

5、恢复my.cnf文件

6、用新密码登录

其他版本 小于8版本用下面命令尝试

mysql> update mysql.user set password=password(‘123456‘) where user=‘root‘;
•••展开全文