初次安裝mysql,net start mysql,然后輸入mysql -u root -p,
出現(xiàn)enter password,我直接點(diǎn)擊回車,結(jié)果出現(xiàn)如果下錯誤:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)。
或者在 my.cnf 配置了密碼
提示錯誤:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)。
解決方案
MySQL安裝時默認(rèn)的用戶是root,這里的root是指數(shù)據(jù)庫的用戶,root密碼一般在初始化MySQL時存放在你的日志文件中,日志文件的存放路徑可以通過my.cnf文件進(jìn)行自定義。
使用如下方法即可解決,本人已驗(yàn)證可行。
1.停止mysql數(shù)據(jù)庫
/etc/init.d/mysqld stop
(或者直接 kill -9 [PID] 殺進(jìn)程!)
2.執(zhí)行如下命令
mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
3.使用root登錄mysql數(shù)據(jù)庫
mysql -u root mysql
4.更新root密碼
mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
最新版MySQL請采用如下SQL:
mysql> UPDATE user SET authentication_string=PASSWORD('newpassword') where USER='root';
5.刷新權(quán)限
mysql> FLUSH PRIVILEGES;
6.退出mysql
mysql> quit
7.重啟mysql
/etc/init.d/mysqld restart
8.使用root用戶重新登錄mysql
mysql -uroot -p
Enter password: <輸入新設(shè)的密碼newpassword>