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

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

猜你喜欢

转载自blog.csdn.net/qq_41322460/article/details/130246038