忘記了您的 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.
2.接下來,不檢查權限啟動MySQL服務器。為此,請運行:
$ sudo systemctl edit mysql
這打開 mysql
systemd
配置文件是您的默認文本編輯器。在我的情況下 nano
編輯。
添加以下行:
[Service] ExecStart= ExecStart=/usr/sbin/mysqld --skip-grant-tables --skip-networking
添加上述行後, CTRL+O
什麼時候 ENTER
保存文件並按 CTRL+X
關。
是這裡, --skip-grant-tables
選項允許您以完全權限連接到 MySQL 數據庫服務器而無需密碼。它還禁用帳戶管理報告,例如: ALTER USER
什麼時候 SET 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#@!';

何時使用 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
輸入這個 root
訪問 MySQL shell 提示的密碼:
mysql>
還有另一種修改MySQL的方法 root
Linux 密碼。這種方法略有不同。如果您想了解更多,請查看以下鏈接。
>> How To Reset MySQL Root User Password In Linux
結論是
如您所見,重置 MySQL 根密碼很容易。如果您仔細按照上述步驟操作,您可以在幾分鐘內恢復您的 MySQL 數據庫的 root 密碼。再也不會輸了。記住您的密碼或將其保存在安全的地方。
數據庫LinuxMySQL 8MySQL 數據庫MySQL 服務器mysql root 密碼重置Ubuntu