技術分享 | MySQL 客戶端連不上(1045 錯誤)緣由全解析

原創做者: 管長龍 譯html

做者:Carlos Tutte、Marcos Albe 翻譯:管長龍mysql

在咱們學習 MySQL 或從事 MySQL DBA 工做期間,時常會遇到:「我嘗試鏈接到 MySQL 而且收到1045 錯誤,但我肯定個人用戶和密碼都沒問題」。sql

無論你如今是不是高手仍是高高手,都不可避免曾經在初學的時候犯過一些很初級的錯誤,例如:用戶名密碼都填錯了。並且工做一段時間後也偶爾會遇到一些不常見錯誤緣由。數據庫

 

1、鏈接錯誤的主機

[root@localhost ~]# mysql -u root -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

若是未指定要鏈接的主機(使用 -h 標誌),則 MySQL 客戶端將嘗試鏈接到 localhost 實例,同時您可能嘗試鏈接到另外一個主機端口實例。bash

修復:仔細檢查您是否嘗試鏈接到 localhost,或者確保指定主機和端口(若是它不是 localhost):服務器

[root@localhost ~]# mysql -u root -p123456 -h <IP> -P 3306

 

2、用戶不存在

[root@localhost ~]# mysql -u nonexistant -p123456 -h localhost
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'nonexistant'@'localhost' (using password: YES)

修復:仔細檢查用戶是否存在:網絡

mysql> SELECT User FROM mysql.user WHERE User='nonexistant';
Empty set (0.00 sec)

若是用戶不存在,請建立一個新用戶:ide

mysql> CREATE USER 'nonexistant'@'localhost' IDENTIFIED BY 'sekret';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

 

3、用戶存在但客戶端主機無權鏈接

[root@localhost ~]# mysql -u nonexistant -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'nonexistant'@'localhost' (using password: YES)

修復:您能夠經過如下查詢檢查 MySQL 容許鏈接的主機用戶/主機:函數

mysql> SELECT Host, User FROM mysql.user WHERE User='nonexistant';
+-------------+-------------+
| Host | User |
+-------------+-------------+
| 192.168.0.1 | nonexistant |
+-------------+-------------+
1 row in set (0.00 sec)

若是須要檢查客戶端鏈接的 IP,可使用如下 Linux 命令來獲取服務器 IP:學習

[root@localhost ~]# ip address | grep inet | grep -v inet6
inet 127.0.0.1/8 scope host lo
inet 192.168.0.20/24 brd 192.168.0.255 scope global dynamic wlp58s0

或公共IP:

[root@localhost ~]# dig +short myip.opendns.com @resolver1.opendns.com
177.128.214.181

而後,您能夠建立具備正確主機(客戶端 IP)的用戶,或使用'%'(通配符)來匹配任何可能的 IP:

mysql> CREATE USER 'nonexistant'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)

 

4、密碼錯誤,或者用戶忘記密碼

mysql> CREATE USER 'nonexistant'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)

修復:檢查和/或重置密碼:

您沒法從 MySQL 以純文本格式讀取用戶密碼,由於密碼哈希用於身份驗證,但您能夠將哈希字符串與「PASSWORD」函數進行比較:

mysql> SELECT Host, User, authentication_string, PASSWORD('forgotten') FROM mysql.user WHERE User='nonexistant';
+-------------+-------------+-------------------------------------------+-------------------------------------------+
| Host | User | authentication_string | PASSWORD('forgotten') |
+-------------+-------------+-------------------------------------------+-------------------------------------------+
| 192.168.0.1 | nonexistant | *AF9E01EA8519CE58E3739F4034EFD3D6B4CA6324 | *70F9DD10B4688C7F12E8ED6C26C6ABBD9D9C7A41 |
| % | nonexistant | *AF9E01EA8519CE58E3739F4034EFD3D6B4CA6324 | *70F9DD10B4688C7F12E8ED6C26C6ABBD9D9C7A41 |
+-------------+-------------+-------------------------------------------+-------------------------------------------+
2 rows in set, 1 warning (0.00 sec)

咱們能夠看到 PASSWORD('forgotten')哈希與 authentication_string 列不匹配,這意味着 password string ='forgotten' 不是正確的登陸密碼。

若是您須要覆蓋密碼,能夠執行如下查詢:

mysql> set password for 'nonexistant'@'%' = 'hello$!world';
Empty set (0.00 sec)

 

5、Bash 轉換密碼中的特殊字符

[root@localhost ~]# mysql -u nonexistant -phello$!world
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'nonexistant'@'localhost' (using password: YES)

修復:經過在單引號中包裝密碼來防止 bash 解釋特殊字符:

[root@localhost ~]# mysql -u nonexistant -p'hello$!world'
mysql: [Warning] Using a password on the command line interface can be insecure
...
mysql>

 

6、SSL 是必須的,但客戶沒有使用

mysql> create user 'ssluser'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> alter user 'ssluser'@'%' require ssl;
Query OK, 0 rows affected (0.00 sec)
...
[root@localhost ~]# mysql -u ssluser -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'ssluser'@'localhost' (using password: YES)

修復:添加 -ssl-mode 標誌(-ssl 標誌已棄用但也可使用)

[https://dev.mysql.com/doc/relnotes/mysql/5.7/en/news-5-7-11.html]

[root@localhost ~]# mysql -u ssluser -p123456 --ssl-mode=REQUIRED
...
mysql>

最後,若是您真的被鎖定並須要繞過身份驗證機制以從新得到對數據庫的訪問權限,請執行如下幾個簡單步驟:

  1. 中止實例

  2. 編輯 my.cnf 並在 [mysqld] 下添加 skip-grant-tables(這樣能夠在不提示輸入密碼的狀況下訪問 MySQL)。在 MySQL 8.0 上,跳過網絡是自動啓用的(只容許從 localhost 訪問 MySQL),但對於之前的 MySQL 版本,建議在 [mysqld] 下添加 -skip-networking

  3. 啓動實例

  4. 使用 root 用戶訪問(mysql -uroot -hlocalhost);

  5. 發出必要的 GRANT / CREATE USER / SET PASSWORD 以糾正問題(可能設置一個已知的 root 密碼將是正確的事情:SET PASSWORD FOR 'root'@'localhost'='S0vrySekr3t'

  6. 中止實例

  7. 編輯 my.cnf 並刪除 skip-grant-tables 和 skip-networking

  8. 再次啓動 MySQL

  9. 您應該可以使用 roothost 從 root 用戶登陸,並對 root 用戶執行任何其餘必要的糾正操做。

 

本文按常見到複雜的順序將可能報 1045 的錯誤緣由所有列舉出來,看完了還不趕快收藏!

參考:https://www.percona.com/blog/2019/07/05/fixing-a-mysql-1045-error/

相關文章
相關標籤/搜索