如何在 Ubuntu Linux 上的 MySQL 8 中重置 Root 密码

你忘了你的 MySQL 数据库吗 root 用户密码? 不用担心! 本分步指南介绍了如何在 Ubuntu 20.04 操作系统上的 MySQL 8 中重置 root 密码。

笔记: 您可能已经知道,MySQL root 用户使用 auth_socket 在运行 MySQL 5.7 及更高版本的 Ubuntu 系统中使用 MySQL 服务器进行身份验证的插件。 所以你可以登录到 MySQL 服务器 root 用户 sudo mysql 只要您知道系统用户的命令 sudo 密码。 在这种情况下,无需更改 MySQL root 密码。 如果您已经更改了 MySQL root 的身份验证方法 用户 caching_sha2_password 或者 mysql_native_password,请按照以下步骤重置 root MySQL数据库的密码。

在 Ubuntu Linux 上的 MySQL 8 中重置 Root 密码

1.首先,使用命令停止MySQL服务:

$ sudo systemctl stop mysql

如果 MySQL 服务正在运行,这将停止它。 您可以使用以下命令验证 MySQL 服务的状态:

$ sudo systemctl status mysql

样本输出:

● mysql.service - MySQL Community Server
      Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
      Active: inactive (dead) since Mon 2021-05-31 11:01:15 UTC; 1min 15s ago
     Process: 1446 ExecStart=/usr/sbin/mysqld (code=exited, status=0/SUCCESS)
    Main PID: 1446 (code=exited, status=0/SUCCESS)
      Status: "Server shutdown complete"
 May 31 07:57:50 ubuntu2004.localdomain systemd[1]: Starting MySQL Community Server…
 May 31 07:57:51 ubuntu2004.localdomain systemd[1]: Started MySQL Community Server.
 May 31 11:01:14 ubuntu2004.localdomain systemd[1]: Stopping MySQL Community Server…
 May 31 11:01:15 ubuntu2004.localdomain systemd[1]: mysql.service: Succeeded.
 May 31 11:01:15 ubuntu2004.localdomain systemd[1]: Stopped MySQL Community Server.
停止 MySQL 服务

2. 接下来,启动 MySQL 服务器,无需任何权限检查。 为此,请运行:

$ sudo systemctl edit mysql

这将打开 mysql systemd 配置文件您的默认文本编辑器。 就我而言,它是 nano 编辑。

在其中添加以下行:

[Service]
ExecStart=
ExecStart=/usr/sbin/mysqld --skip-grant-tables --skip-networking
编辑 mysql systemd 配置文件

添加以上行后,按 CTRL+OENTER 保存文件,然后按 CTRL+X 到 close 它。

在这里, --skip-grant-tables 选项使您能够在没有密码和所有权限的情况下连接到 MySQL 数据库服务器。 它还将禁用帐户管理语句,例如 ALTER USERSET PASSWORD. 和 --skip-networking 选项用于阻止其他客户端连接到数据库服务器。 由于它禁用了所有远程连接,因此在您正常重新启动数据库服务器之前,任何远程客户端都无法访问数据库服务器。

3.重新加载 systemd 使用命令配置:

$ sudo systemctl daemon-reload

4.启动MySQL服务:

$ sudo systemctl start mysql

这将启动 MySQL 服务器 --skip-grant-table--skip-networking 选项。 您可以通过检查 MySQL 服务状态来验证它:

$ sudo systemctl status mysql
 ● mysql.service - MySQL Community Server
      Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Drop-In: /etc/systemd/system/mysql.service.d
              └─override.conf
      Active: active (running) since Mon 2021-05-31 11:39:18 UTC; 1min 23s ago
     Process: 1882 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
    Main PID: 1905 (mysqld)
      Status: "Server is operational"
       Tasks: 36 (limit: 2280)
      Memory: 331.0M
      CGroup: /system.slice/mysql.service
              └─1905 /usr/sbin/mysqld --skip-grant-tables --skip-networking
 May 31 11:39:16 ubuntu2004.localdomain systemd[1]: Starting MySQL Community Server…
 May 31 11:39:18 ubuntu2004.localdomain systemd[1]: Started MySQL Community Server.

5. 现在,连接到 MySQL 服务器 root 没有密码的用户:

$ sudo mysql -u root

您将立即进入 MySQL shell 提示符。

Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 10
Server version: 8.0.25-0ubuntu0.20.04.1 (Ubuntu)

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> 

6.我们登录数据库服务器时没有加载授权表(因为我们使用了 --skip-grant-tables 选项)。 所以我们不能用户 ALTER USER 他需要重置密码的命令。 要加载授权表,请从 MySQL shell 提示符运行以下命令:

mysql> FLUSH PRIVILEGES;

7.接下来,运行以下任一命令重置MySQL root 密码。

如果你正在使用 caching_sha2_password 插件,运行:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'Password123#@!';
在 Ubuntu Linux 上的 MySQL 8 中重置 root 密码在 Ubuntu Linux 上的 MySQL 8 中重置 root 密码

如果你使用 mysql_native_password 插件,改为运行:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Password123#@!';

代替 Password123#@! 用你自己的。

更改 MySQL 后 root 密码,从 MySQL shell 提示退出:

mysql> exit

8.还原修改过的 systemd 使用命令配置恢复正常设置:

$ sudo systemctl revert mysql

这将删除所有修改过的文件。

Removed /etc/systemd/system/mysql.service.d/override.conf.
Removed /etc/systemd/system/mysql.service.d.

9.重新加载 systemd 配置使更改生效:

$ sudo systemctl daemon-reload

10、最后正常重启MySQL服务器:

$ sudo systemctl restart mysql

11. 现在你应该可以连接到 MySQL 数据库了 root 用户与 new password 使用命令:

$ mysql -u root -p

Enter 这 root 访问 MySQL shell 提示的密码:

mysql> 

还有另一种改变MySQL的方法 root Linux 中的密码。 它与这种方法略有不同。 如果您有兴趣了解,请查看以下链接:

>> How To Reset MySQL Root User Password In Linux

结论

如您所见,重置 MySQL root 密码很容易! 如果您仔细按照上述步骤操作,您可以在几分钟内恢复您的 MySQL 数据库 root 密码。 不要再失去它。 记住或将您的密码保存在安全的地方。