mysql 8.0以上的版本忘记密码,重置密码终极解决方案

今天忘记了linux centos 上 mysql的 root 密码
按照ChatGpt 给的方法尝试了多次,均告失败,看来 AI 对于程序员来说也不是万能的。
ChatGpt给的方法是先越权登录

sudo systemctl stop mysqld
sudo systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
sudo systemctl start mysqld
mysql -u root
USE mysql;

使用ALTER命令或者直接 update user 表的authentication_string,但是越权登录后,ALTER命令不能使用,报错

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

GPT又给出了直接update user表中authentication_string字段的方案

UPDATE user SET authentication_string=PASSWORD('new_password') WHERE User='root' AND Host='localhost';
UPDATE user SET authentication_string=CONCAT('*', UPPER(SHA1(UNHEX(SHA1('new_password'))))) WHERE User='root' AND Host='localhost';
UPDATE user SET authentication_string=PASSWORD('new_password'), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';

但由于 mysql8.0以上版本取消了PASSWORD()函数,还有就是加密算法 sha1 还是 sha2的问题,以及plugin插件不存在等原因,要么修改不成功,要么修改成功了,登录验证不过去

mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

mysql> UPDATE mysql.user SET plugin = 'mysql_native_password', authentication_string = PASSWORD('new_password') WHERE User = 'root' AND Host = 'localhost';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('new_password') WHERE User = 'root' AND Host = 'localhost'' at line 1

正确有效的解决方法还是自己搜出来的,如下:

# 越权登录后先使用update把user表中对应root用户的authentication_string字段置为空
update user set authentication_string = "" where user = "root";

# 这样就可以使用空密码正常登录了,取消越权登录,
sudo systemctl unset-environment MYSQLD_OPTS
sudo systemctl restart mysqld

# 空密码正常登录
mysql -u root -p
输入空密码,登录成功

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
FLUSH PRIVILEGES;

# 密码修改成功,重新登录
mysql -u root -p
# 输入新密码,成功登录
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.32 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> 

相关推荐

  1. mysql5.6密码忘记

    2024-03-22 16:52:02       34 阅读
  2. 思科路由器忘记密码怎么

    2024-03-22 16:52:02       40 阅读
  3. 华为:交换机忘记console密码

    2024-03-22 16:52:02       104 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-22 16:52:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-22 16:52:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-22 16:52:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-22 16:52:02       20 阅读

热门阅读

  1. 【React】React组件的导入与导出

    2024-03-22 16:52:02       21 阅读
  2. [Qt学习笔记]Qt线程间数据通讯及数据共享

    2024-03-22 16:52:02       20 阅读
  3. 多线程(3)线程基本状态

    2024-03-22 16:52:02       21 阅读
  4. c++算法学习笔记 (16) 约数

    2024-03-22 16:52:02       18 阅读
  5. AI大模型学习:挑战与机遇

    2024-03-22 16:52:02       22 阅读
  6. Ubuntu自启GUI程序

    2024-03-22 16:52:02       19 阅读
  7. 2024届 C++ 刷题 笔试强训 Day 04

    2024-03-22 16:52:02       18 阅读
  8. websocket 升级协议时的协议切换点

    2024-03-22 16:52:02       18 阅读
  9. C语言实现飞行小游戏

    2024-03-22 16:52:02       23 阅读