mysql 5.7 忘记root密码

发布于 2024-04-12  212 次阅读


# 打开mysql配置文件,在[mysqld]下添加skip-grant-tables
[root@wordpress ~]# vim /etc/my.cnf
[mysqld]
skip-grant-tables

# 重启mysql服务
[root@wordpress ~]# systemctl restart mysqld

# 执行输入mysql登录数据库
[root@wordpress ~]# mysql 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.42 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, 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> 

# 修改数据库密码
mysql> update mysql.user set authentication_string=password('$new_password') where user='root' and host='localhost'; 

# 刷新数据库,使其生效
mysql> flush privileges;

#退出数据库
mysql> quit

# 将在mysql配置文件中新增的内容删除,然后重启数据库

# 尝试使用新密码进行登录mysql
[root@wordpress ~]# mysql -uroot -p'$new_password'

一沙一世界,一花一天堂。君掌盛无边,刹那成永恒。