MySQL5.7忘记root密码并重置密码

可以尝试使用 mysqld 来进行安全模式启动。

但是要注意,使用 mysqld 进行安全模式启动的过程要复杂一些。以下是一个基本的步骤:

  1. 找到 my.cnf 文件my.cnf 是 MySQL 的配置文件,通常位于 /etc/my.cnf/etc/mysql/my.cnf~/.my.cnf

  2. 编辑 my.cnf 文件: 在文件的 [mysqld] 部分下方添加以下行:

    skip-grant-tables
    

    这将允许 MySQL 在下次启动时跳过权限表,使你可以直接登录。

  3. 重启 MySQL 服务

    sudo systemctl stop mysqld
    sudo systemctl start mysqld
    
  4. 连接到 MySQL 并重置密码

    mysql -u root
    USE mysql;
    UPDATE user SET authentication_string=PASSWORD("newpassword") WHERE User='root';
    FLUSH PRIVILEGES;
    EXIT;
    

    "newpassword" 替换为你想要的新密码。

  5. 恢复原始的 my.cnf 设置: 编辑 my.cnf 文件,移除 skip-grant-tables 行,然后再次重启 MySQL 服务。

请记住,在编辑任何配置文件之前,最好先做一个备份,以防止出现问题。.

猜你喜欢

转载自blog.csdn.net/u011197085/article/details/131566043