首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫

MySQL / phpMyAdmin 根密碼遺忘?五分鐘重置 root 密碼完整步驟

     編輯:MySQL 資料庫相關知識

適用場景

  • Linux / Windows 通用
  • MySQL / MariaDB / Percona Server

步驟 1:停止 MySQL 服務

# systemd
systemctl stop mysqld
# 或 SysV
service mysql stop
# 找不到服務名時
find / -name "mysql.server"  # 通常在 /usr/local/mysql/share/mysql/mysql.server
/usr/local/mysql/share/mysql/mysql.server stop

步驟 2:啟動跳過權限表模式

# 找到 mysqld_safe
find / -name "mysqld_safe"
# 啟動 (背景執行)
/usr/local/mysql/bin/mysqld_safe --skip-grant-tables --skip-networking &
  • --skip-grant-tables:不載入權限表,任意用戶免密登入
  • --skip-networking:僅允許本地 socket 連線,安全性考量

步驟 3:免密登入並修改密碼

mysql -u root
# 進入 mysql> 提示符後
FLUSH PRIVILEGES;  -- 5.7+ 必須先執行
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewStrongPassword123!';
-- 或舊版
UPDATE mysql.user SET authentication_string = PASSWORD('NewStrongPassword123!') WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;
QUIT;

步驟 4:正常重啟驗證

# 結束跳過權限的進程
pkill mysqld
# 或
killall mysqld

# 正常啟動
systemctl start mysqld
# 或
service mysql start

# 驗證
mysql -u root -p
# 輸入新密碼

Windows 環境快速版

net stop mysql
mysqld --skip-grant-tables --skip-networking
# 新開 CMD 視窗
mysql -u root
UPDATE mysql.user SET authentication_string = PASSWORD('newpass') WHERE User='root';
FLUSH PRIVILEGES;
exit
# 關閉 mysqld 視窗 (Ctrl+C)
net start mysql

phpMyAdmin 同步更新

修改 config.inc.php:

$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = 'NewStrongPassword123!';
$cfg['Servers'][$i]['auth_type'] = 'cookie';  -- 建議 cookie 模式
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved