最近在centOS 7上,通過yum安裝了mysql,安裝成功後,使用root登錄,出現了如下報錯:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
mysql版本為:Ver 14.14 Distrib 5.7.10, for Linux(X86_64) using EditLine wrapper
然而實際上並沒有進行過root密碼設置(不知道是不是安裝過程有被忽略的地方?)。
對此就各種尋找答案,大致有下面一些情況:
有說root的隨機密碼位於/root/.mysql_secret中,但是我根本沒有/root/.mysql_secret文件。有文章表示
> ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using > password: NO) :表示沒有生成root的臨時密碼 > > ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using > password: YES) :表示生成了root的臨時密碼。
有些資料說root的默認密碼為空,經過確認那是以前的老版本,Mysql 5.6及以後版本出處於安全考慮,root密碼已經不為空了。
最終找到對我來說有用的解決方案。
產生原因:
Now that the password MySQL had generated is expired, the problem is reduced to getting this password to work again (1) or generate a new one (2). This can be accomplished by running MySQL with the skip-grant-tables option which would make it ignore the access rights:
解決方法:
復制代碼 代碼如下:
Stop your MySQL server.
Add skip-grant-tables at the end of the [mysqld] section of my.cnf file and save it.
Start MySQL server.
In terminal, typemysql -u root -pto get into MySQL command prompt.
In the command prompt, typeUSE mysql;to get into the mysql database where it keeps database users.
Type
UPDATE user SET password_expired = 'N' WHERE User = 'root';
to let MySQL know the password is not expired (1) or
UPDATE user SET authentication_string = PASSWORD('YourNewPassword'), password_expired = 'N' WHERE User = 'root';
附上鏈接:Unable to access MySQL after it automatically generated a temporary password
發布出來,供大家參考。