ERROR 1045 (28000): Access denied for user

同事諮詢一個關於MySQL用戶權限的問題,相同的用戶密碼在遠程機器可以訪問成功。但相同的用戶密碼在本機通過IP訪問確報錯。

ERROR 1045 (28000): Access denied for user。

首先想到的是該用戶的IP有訪問限制。檢查發現本機IP在授權的範圍內。

MariaDB [(none)]> select user,host from mysql.user where user='mgc';
+------+-------------+
| user | host        |
+------+-------------+
| mgc  | 192.168.0.% |
+------+-------------+
1 row in set (0.00 sec)

遠程機器通過IP可以正常訪問數據庫。

$ mysql -umgc -p123 -h192.168.0.100-P3336 -e"select @@server_id"  
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
|          43 |
+-------------+

本機通過IP訪問數據庫報ERROR 1045 (28000)的錯誤。

$ mysql -umgc -p123 -h192.168.0.100 -P3336 -e"select @@server_id"
ERROR 1045 (28000): Access denied for user 'mgc'@'test43' (using password: YES)

仔細查看報錯信息,ERROR 1045 (28000): Access denied for user ‘mgc’@’test43’ (using password: YES)

發現有些疑問?爲什麼用mgc@’192.168.0.100’ 用戶訪問,
卻報Access denied for user ‘mgc’@’test43’ 用戶無法訪問呢。
檢查一下mysql.user表中host爲’test43’的用戶。

MariaDB [(none)]> select user,host from mysql.user where host='test43';
+------+-------+
| user | host  |
+------+-------+
|      | test43|
| root | test43|
+------+-------+

發現mysql.user表中有個host爲’test43’的匿名用戶。刪除這個匿名用戶後,原腳本運行正常。

MariaDB [(none)]> drop user ''@'test43;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章