Written by: Wen-Fong Ke
Category: Articles
Hits: 8714

如果你把 MySQL 的 root 密碼忘了, 先不要急著將 MySQL 砍掉重裝...

下面是更改 root 的密碼的方法。先決條件為

  1. 符合以上條件的話,首先登入那台機器,將 mysqld 停止。
  2. 用下面指令啟動 mysql (Windows 使用者不用加 --user=root):     
    shell> mysqld --skip-grant-tables --user=root
  3. 登入你的 MySQL
    shell> mysql -u root
  4. 重新設定密碼:
    mysql> UPDATE mysql.user SET Password=PASSWORD('newpwd')
    -> WHERE User='root';mysql> FLUSH PRIVILEGES;</li> <li>重新啟動 mysqld</li> </ol> <p>另法:</p> <ol> <li>開啟一個檔案,輸入以下敘述 (一行),將其存為 mysql-init.txt。<br /> SET PASSWORD FOR 'root'@'localhost' =
    PASSWORD('MyNewPassword');
  5. 將 mysqld 停止。
  6. 以下面這行指令重新啟動 mysql
    • UNIX
      shell> mysqldmysqld_safe --init-file=/path/to/mysql-init &
    • Windows
      shell> mysqld --init-file=\path\to\mysql-init.txt
  7. 現在用 ‘MyNewPassword’ 即可登入。
  8. 把 mysql-init 刪除。