【轉自 http://blog.csdn.net/u014410695/article/details/50630233】
以下方法親測有效,過程使用的工具只有mac的終端無需workbench
當我們通過終端連接mysql數據庫時候我們會看到這樣的信息
ERROR 1045: Access denied for user: 'root@localhost' (Using password: NO)
- 1
- 2
或者
ERROR 1045: Access denied for user: 'root@localhost' (Using password: YES)
- 1
- 2
解決上面錯誤的方法之一就是重新設置我們的mysql的root密碼。
1.在終端輸入下面命令關閉正在運行的mysql,如果msyql沒有運行可以跳過,需要輸入mysql的密碼。如果mysql密碼忘記了,可以直接通過系統偏好設置里面關閉!
/usr/local/mysql/bin/mysqladmin -u root -p shutdown
- 1
系統便好設置關閉如圖
2.進入mysql的bin目錄執行如下命令
$ cd /usr/local/mysql/bin $ sudo su
- 1
- 2
之后輸入管理員密碼會看到
sh-3.2#
- 1
之后我們輸入下面命令以安全模式運行mysql
sh-3.2#./mysqld_safe --skip-grant-tables &
- 1
運行結束我們打開mac的系統偏好設置,選擇msyql,我們會發現Mysql重新運行了如圖
回到終端點擊Command + N 重新打開一個終端
輸入
mysql -u -root
- 1
這時候我們不需要密碼就能進入mysql
Your MySQL connection id is 57 Server version: 5.7.10 MySQL Community Server (GPL) Copyright (c) 2000, 2015, 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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
注意:這里有的時候會進不來,這個時候可以嘗試重啟mac電腦,具體原因我也不清楚。
3.修改root密碼
首先執行下面命令為了能夠修改任意的密碼
mysql> FLUSH PRIVILEGES;
- 1
之后執行修改密碼的SQL語句,這里的qsd19001008可以替換你自己想要修改的密碼
mysql> SET PASSWORD FOR root@'localhost' = PASSWORD('qsd19001008');
- 1
如果你的子帳號可以登錄msyql你也可以嘗試下面的方法
mysql>UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';
- 1
或
mysql>USE mysql
UPDATE user SET Password = PASSWORD('newpwd') WHERE Host = 'localhost' AND User = 'root';
- 1
- 2
- 3
又或者
mysql>USE mysql
UPDATE user SET Password = PASSWORD('newpwd') WHERE Host = '%' AND User = 'root';
- 1
- 2
- 3
最后刷新
FLUSH PRIVILEGES;
- 1
Control+D推出mysql,然后關閉安全模式數據庫,這里要輸入你剛才設置數據密碼就好啦
/usr/local/mysql/bin/mysqladmin -u root -p shutdown
- 1
到目前為止你就找回了你mysql的密碼啦
正常啟動mysql數據庫,輸入剛才設置的密碼qsd19001008
/usr/local/mysql/share/mysql.server start
- 1
如果上面的命令不執行,同理到系統偏好里開啟mysql服務器。
最后執行下面的終端命令,然后輸入剛才設置的密碼qsd19001008我們就可以正常進入mysql啦
$mysql -u root -p
- 1