在什么情況下,需要重置root密碼呢?那就是我們忘記了。還有一種比較坑的,那就是筆者的這種情況。按照正常的情況下,MySQL安裝完之后,會彈出一個對話框,顯示着一個臨時的root密碼,但無論筆者如何重裝MySQL,始終不給我一個臨時的root的密碼,導致筆者一直無法登陸MySQL,沒辦法,只能用硬的,那就是重置密碼。
停止MySQL的服務,打開系統的偏好設置,找到MySQL 進去后,點擊Stop MySQL Server即可。

step1: 蘋果->系統偏好設置->最下邊點mysql 在彈出頁面中 關閉mysql服務(點擊stop mysql server) step2: 進入終端輸入:cd /usr/local/mysql/bin/ 回車后 登錄管理員權限 sudo su 回車后輸入以下命令來禁止mysql驗證功能 ./mysqld_safe --skip-grant-tables & 回車后mysql會自動重啟(偏好設置中mysql的狀態會變成running)
開啟兩個終端,在第一個終端輸入sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables,輸入當前用戶的密碼:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
那么就換下面這種:
macdeMacBook-Pro:~ mac$ mysql -uroot -h127.0.0.1 -p Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
然后在第二個終端輸入sudo /usr/local/mysql/bin/mysql -u root,然后輸入當前用戶的密碼后,出現以下的界面
macdeMacBook-Pro:~ mac$ sudo /usr/local/mysql/bin/mysql -u root Password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.22 Homebrew Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
UPDATE mysql.user SET authentication_string=PASSWORD('新密碼') WHERE User='root';回車,出現以下的界面,說明修改成功。
mysql> UPDATE mysql.user SET authentication_string=PASSWORD('123456') WHERE User='root'; Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1
mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.01 sec)
mysql> \q
Bye
macdeMacBook-Pro:~ mac$ mysql -uroot -h127.0.0.1 -p Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) macdeMacBook-Pro:~ mac$ sudo /usr/local/mysql/bin/mysql -u root Password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.22 Homebrew Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> UPDATE mysql.user SET authentication_string=PASSWORD('123456') WHERE User='root'; Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1 mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.01 sec) mysql> \q Bye macdeMacBook-Pro:~ mac$
