MySQL8.0+ 忘记 root 密码,如何重置密码

文章介绍了在MySQL8.0版本中,如果忘记root密码,如何通过停止服务,修改配置文件,添加skip-grant-table选项来跳过权限验证,然后重置密码的详细步骤。过程包括停止MySQL服务,编辑my.cnf文件,重启服务,使用免密码登录,更新用户授权以及恢复配置文件,最后重新启动服务并用新密码登录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MySQL8.0密码要求

需要大小写、字母、字符就皆有,这种不常用密码组合就很容易忘记

演示的MySQL版本

[root@ementorship data]# mysql --version
mysql  Ver 8.0.26 for Linux on x86_64 (Source distribution)

停止MySQL

systemctl stop mysqld 

修改配置文件,添加 skip-grant-table(跳过权限验证)

vim /etc/my.cnf

重启MySQL

systemctl restart mysqld

查看MySQL状态

[root@ementorship my.cnf.d]# systemctl status mysqld
● mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2023-04-19 15:36:21 CST; 24s ago
  Process: 73978 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
  Process: 73894 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)
  Process: 73869 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
 Main PID: 73932 (mysqld)
   Status: "Server is operational"
    Tasks: 37 (limit: 201000)
   Memory: 364.1M
   CGroup: /system.slice/mysqld.service
           └─73932 /usr/libexec/mysqld --basedir=/usr

Apr 19 15:36:20 ementorship systemd[1]: Starting MySQL 8.0 database server...
Apr 19 15:36:21 ementorship systemd[1]: Started MySQL 8.0 database server.

免密码登录

mysql -u root -p
  • 直接回车即可
[root@ementorship my.cnf.d]# 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.26 Source distribution

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

密码重置(MySQL8.0+版本与之前版本有所不同)

flush privileges;
use mysql;
update user set authentication_string='' where user='root';
ALTER user 'root'@'localhost' IDENTIFIED BY 'Pwd1234@';
  • 关闭免密登陆
vim /etc/my.cnf

删除 skip-grant-table

  • 重启MySQL
systemctl restart mysqld
  • 登录MySQL(密码输入不显示)
mysql -u root -p
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AloneMou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值