这是一个比较傻的问题,自己安装完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 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.31 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
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';