最近在centOS 7上,經過yum安裝了mysql,安裝成功後,使用root登陸,出現了以下報錯:mysql
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
sql
然而實際上並無進行過root密碼設置(不知道是否是安裝過程有被忽略的地方?)。安全
對此就各類尋找答案,大體有下面一些狀況:app
有說root的隨機密碼位於/root/.mysql_secret中,可是我根本沒有/root/.mysql_secret文件。有文章表示this
> 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密碼已經不爲空了。code
最終找到對我來講有用的解決方案。server
產生緣由:ip
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:terminal
解決方法:get
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 -p
to 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
發佈出來,供你們參考。