ERROR 1045 (28000): Access denied for user 'root'@'localhost'

e71hao發表於2016-07-28
一、錯誤描述:
錯誤如標題:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)。我用root@localhost授權登陸,然後給其中
一個使用者授權,然後報如上錯誤:
[mysql@centos6-clone ~]$ mysql -u root -p
Enter password: 
Your MySQL connection id is 2
Server version: 5.6.25-log MySQL Community Server (GPL)
mysql>  grant all  on *.* to 'fi'@'127.0.0.1' identified by "fi";
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
mysql> exit
Bye
按理來說,root@localhost是最高許可權。居然不能給其他人授權。1045錯誤是授權拒絕。怎麼辦?
二、錯誤分析
檢視下root許可權:
mysql> show grants;
+----------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                            |
+----------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*E74858DB86EBA20BC33D0AECAE8A8108C56B17FA' |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION                                                         |
+----------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
我們看root@localhost 有許可權:ALL PRIVILEGES,但是沒有WITH GRANT OPTION 語句,就是別人授權的許可權。所以,不能授權的原因是:
沒有這個WITH GRANT OPTION 語句。
三、錯誤解決
知道原因了,解決問題就很簡單了。用另外一個方式登入root@127.0.0.1,然後給root@localhost授權,這樣root@localhost 就可以給其他使用者
授權了。
[mysql@centos6-clone ~]$ mysql -uroot -p -h127.0.0.1
Enter password: 
mysql> SHOW GRANTS
    -> ;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@127.0.0.1                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY PASSWORD '*E74858DB86EBA20BC33D0AECAE8A8108C56B17FA' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>  GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*E74858DB86EBA20BC33D0AECAE8A8108C56B17FA' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
[mysql@centos6-clone ~]$ mysql -uroot -p
Enter password: 
mysql> grant all  on *.* to 'fo'@'%' identified by "fo";
Query OK, 0 rows affected (0.00 sec)







來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30393770/viewspace-2122687/,如需轉載,請註明出處,否則將追究法律責任。

相關文章