首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> MySQL 資料庫 >> MySQL 綜合教學

MySQL 忘記密碼解決方案:Windows 與 Linux 環境完整重置教學

     編輯:MySQL 綜合教學

Windows 環境下 MySQL 密碼重置

實際操作步驟

  1. 關閉正在運行的 MySQL 服務
  2. 開啟 DOS 視窗,切換到 mysql\bin 目錄
  3. 輸入 mysqld --skip-grant-tables 按 Enter。若無提示訊息即成功
  4. 另開一個 DOS 視窗(第一個視窗會佔用),同樣切換到 mysql\bin 目錄
  5. 輸入 mysql 按 Enter,成功會顯示 mysql> 提示符
  6. 連線權限資料庫:mysql> use mysql;
  7. 修改密碼:mysql> update user set password=password("520") where user="root";
  8. 刷新權限(必要步驟):mysql> flush privileges;
  9. 退出:mysql> \q
  10. 登出系統重新進入,使用 root 與新密碼 123456 登入

實操畫面示例

第一步:
C:\Documents and Settings\Administrator>cd D:\web\www.php100.com\Mysql\MySQL Se
rver5.5\bin
C:\Documents and Settings\Administrator>d:
D:\web\www.php100.com\Mysql\MySQL Server5.5\bin>mysqld --skip-grant-tables

第二步:
Microsoft Windows [版本 5.2.3790]
(C) 版權所有 1985-2003 Microsoft Corp.
C:\Documents and Settings\Administrator>cd D:\web\www.php100.com\Mysql\MySQL Se
rver5.5\bin
C:\Documents and Settings\Administrator>d:
D:\web\www.php100.com\Mysql\MySQL Server5.5\bin>mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.10 MySQL Community Server (GPL)
...
mysql> use mysql;
Database changed
mysql> update user set password=password("520") where user="root";
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> \q
Bye
D:\web\www.php100.com\Mysql\MySQL Server5.5\bin>

Linux 環境下 MySQL Root 密碼重置

步驟 1:確認伺服器安全狀態

重置期間 MySQL 完全處於無密碼保護狀態,建議關閉對外連接埠、停止 Apache 及所有使用者程序,最安全是在 Console 操作並拔除網路線。

步驟 2:修改 MySQL 登入設定

# vi /etc/my.cnf
在 [mysqld] 區段加入:skip-grant-tables

例如:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-grant-tables

存檔離開 vi。

步驟 3:重新啟動 mysqld

# /etc/init.d/mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]

步驟 4:登入並修改 Root 密碼

# /usr/bin/mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3 to server version: 3.23.56
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> USE mysql;
Database changed
mysql> UPDATE user SET Password = password('new-password') WHERE User = 'root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye

步驟 5:恢復 MySQL 登入設定

# vi /etc/my.cnf
刪除 [mysqld] 區段中剛加入的 skip-grant-tables
存檔離開 vi。

步驟 6:重新啟動 mysqld

# /etc/init.d/mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]

完成後即可使用新密碼登入 MySQL。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved