主機

How Smart Tools Are Shaping the Future of Creative Work

Eric Chuang
2020-01-26

因為電腦的開發環境使用 MAMP,所以筆記一下,
下次如果不小心搞砸了比較容易恢復。

問題發生起因

在 mysql_error.log 檔案中,發現了幾筆 [Note] Access denied for user ‘root’@’localhost’ (using password: YES) 的警告。想要用正常管道重設 root 密碼,結果在重設過程中,按下了 control + c,結果這段被終端機解讀成文字的指令成了新的密碼⋯⋯

系統環境

  • MAMP Pro 5.4
  • MySQL 5.7.26

MAMP 與 MAMP Pro 的 MySQL 精靈 (daemon) 在同一個路徑下,所以基本上以下的解法是可以共通的。其他作業系統只需要找到 MySQL 精靈的位置,解法亦同。

解決步驟

首先,關閉 MySQL 服務。接著透過終端機指令碼重啟 MySQL,並加入 –skip-grant-tables 與 –skip-networking 的選項。前者會暫時性准許所有使用者不需要透過密碼登入,因此基於安全性考量,加入了後者,用以中止遠端連線。

程式碼參考

$ /Applications/MAMP/Library/bin/mysqld_safe \
--skip-grant-tables \
--skip-networking \
--socket=/Applications/MAMP/tmp/mysql/mysql.sock \
--pid-file=/Applications/MAMP/tmp/mysql/mysql.pid &

/Applications/MAMP/Library/bin/mysqld_safe 是安全模式精靈的路徑,如果在其他作業系統,有可能已經動態連結到 mysqld_safe 的指令中。

接著會看到如下提示:

mysqld_safe Starting mysqld daemon with databases from /Applications/MAMP/db/mysql

輸入下列指令以開啟 mysql,輸入 FLUSH PRIVILEGES 清除所有權限,並將 NewPassword 改成欲使用的密碼。

$ /Applications/MAMP/Library/bin/mysql
#如果是其他作業系統,或自行安裝的 mysql
$ mysql
$ mysql> FLUSH PRIVILEGES;
$ mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';

關於 FLUSH PRIVILEGES 的設定,根據 MySQL 的文件,是因為我們在開啟安全模式時,啟用了 –skip-grant-tables 的選項,藉此繞過密碼驗證,也就是在該 MySQL 的工作階段中,並不會有任何密碼驗證,此時的系統較不安全。因此透過 FLUSH PRIVILEGES 的指令,讓該工作階段重新讀取權限資料表 (grant table)。

參考文件

Sign up for our newsletter

Subscribe to our newsletter to receive the latest updates, expert insights, and practical tips directly in your inbox. Stay informed with content designed to help you grow.

+480.000 happy customers

Gain more with expert solutions

Unlock the full potential of your business with our expert solutions. Our team provides tailored strategies, professional guidance, and actionable insights to help you achieve better results.

  • Eric Chuang
  • •
  • 2025-01-23
如何同時使用 Webinoly 與 Cloudflare 保護網站安全

在社群夥伴勇哥的文章〈Webinoly 最新版快速安裝〉中,提到使用 Webinoly 來建立 WordPre […]

主機