重置 MYSQL Root 管理者密碼方式

若 Windows OS 資料庫 管理者帳號密碼 遭被 「修改 或 遺失」時

..ヽ(✿゚▽゚)ノ
.. ก็ʕ•͡ᴥ•ʔ ก้
..ヽ(✿゚▽゚)ノ

[ 停用 MySQL Service ]

開啟 CMD 命令提示字元(最高管理權限)

輸入 net stop mysql (建立 MySQL Name 的名稱)

[ 進入 MYSQL 安全模式 (略過許可檢查驗證) ]

(預設 安裝路徑 C:\Program Files\MySQL\MySQL Server)

輸入 cd C:\Program Files\MySQL\MySQL Server 5.7.37\bin

輸入 mysqld --skip-grant-tables

[ 修改 my.ini 檔案內容 ]

修改 ”應用程式 及 資料位置“

[mysqld] 

basedir = "C:\ProgramData\MySQL\MySQL Server 5.7.37"

datadir = "C:\ProgramData\MySQL\MySQL Server 5.7.37\Data"

輸入 mysqld --defaults-file="../my.ini" --skip-grant-tables

額外開啟 CMD 命令提示字元

輸入 cd C:\Program Files\MySQL\MySQL Server 5.7.37\bin

輸入 mysql (即可 跳過 「許可驗證連線」 步驟)

C:\Program Files\MySQL\MySQL Server 5.7.37\bin>mysql
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

[ 重置 root 使用者密碼 ]

消除 root 使用者帳號 authentication_string 欄位

輸入 update mysql.user set authentication_string="" where user="root";

mysql> update mysql.user set authentication_string="" where user="root";
Query OK, 1 row affected (0.00 sec)

mysql> select user,authentication_string from mysql.user\G
******* 1. row *******
         user: root
authentication_string:
******* 2. row *******
         user: mysql.sys
authentication_string: *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE

2 rows in set (0.00 sec)

[ 重新整理 許可權表 ]
輸入 flush privileges;
輸入 quit

[ 進行 重啟 MySQL 服務 ]
關閉 所有命令提示符視窗
開啟 工作管理員
關閉 mysqld.exe

[ 重置 root 管理者密碼 Password ]
✾(透過 表格 修改方式)✾
開啟 cmd
輸入 cd C:\Program Files\MySQL\MySQL Server 5.7.37\bin
輸入 mysql
輸入 SET PASSWORD FOR “UserName"=PASSWORD(“NewPassword");

mysql> set password for root@localhost = password("2Pa$$w0rd");
Query OK, 0 rows affected, 1 warning (0.00 sec)

✾(透過 mysqladmin 修改方式)✾
輸入 mysqladmin -u “UserName" -p password “NewPassword"
(執行 直接輸入新密碼 正確後即可修改完成)

C:\Program Files\MySQL\MySQL Server 5.7.37\bin> mysqladmin -u root -p password 2Pa$$w0rd
Enter password: ****

mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

✾(透過 Update 修改方式)✾
輸入 UPDATE mysql.user SET authentication_string=PASSWORD(“NewPassword") WHERE user="UserName";
(重置 管理者帳號時 必須使用 password() 函式值加密)

mysql> update mysql.user set authentication_string=password("2Pa$$w0rd") where user="root";
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

WeiYao
0

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

%d 位部落客按了讚: