背景
今天突然手抖了一下,不小心把navicat中mysql的连接删掉了,当我想再次连接到mysql时才发现密码不记得了。马上百度了一下,发现还是有解决办法的,悬着的心这才放下来。
1.编辑 /etc/my.cnf 文件,在 [mysqld]配置中增加一句 skip-grant-tables
vim /etc/my.cnf
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
# 新增
skip-grant-tables
2.重启mysql
systemctl restart mysqld
3.登陆mysql,不用输入用户名,密码,直接回车就可以。
[root@localhost ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.28 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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.重置root密码
mysql> flush privileges;
mysql> alter user 'root'@'localhost' identified by 'your 新密码' ;
5.退出mysql命令行,编辑 /etc/my.cnf 删除 skip-grant-tables,再次重启mysql
# 删除这条配置
skip-grant-tables
systemctl restart mysqld
6.使用新密码登陆mysql
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.28 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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>