你忘記了你的 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 密碼。永遠不要再失去它。記住您的密碼或將其保存在安全的地方。
DatabaseLinuxMySQL 8MySQL DatabaseMySQL ServerReset mysql root password Ubuntu