創(chuàng): 管長龍 譯
作者:Carlos Tutte、Marcos Albe翻譯:管長龍
在我們學習 MySQL 或從事 MySQL DBA 工作期間,時常會遇到:“我嘗試連接到 MySQL 并且收到1045 錯誤,但我確定我的用戶和密碼都沒問題”。
不管你現(xiàn)在是否是高手還是高高手,都不可避免曾經(jīng)在初學的時候犯過一些很初級的錯誤,例如:用戶名密碼都填錯了。而且工作一段時間后也偶爾會遇到一些不常見錯誤原因。
一、連接錯誤的主機
[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 實例,同時您可能嘗試連接到另一個主機端口實例。
修復:仔細檢查您是否嘗試連接到 localhost,或者確保指定主機和端口(如果它不是 localhost):
[root@localhost ~]# mysql -u root -p123456 -h <IP> -P 3306
二、用戶不存在
[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)
如果用戶不存在,請創(chuàng)建一個新用戶:
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)
三、用戶存在但客戶端主機無權(quán)連接
[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 命令來獲取服務(wù)器 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
然后,您可以創(chuàng)建具有正確主機(客戶端 IP)的用戶,或使用'%'(通配符)來匹配任何可能的 IP:
mysql> CREATE USER 'nonexistant'@'%' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.00 sec)
四、密碼錯誤,或者用戶忘記密碼
mysql> CREATE USER 'nonexistant'@'%' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.00 sec)
修復:檢查和/或重置密碼:
您無法從 MySQL 以純文本格式讀取用戶密碼,因為密碼哈希用于身份驗證,但您可以將哈希字符串與“PASSWORD”函數(shù)進行比較:
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' 不是正確的登錄密碼。
如果您需要覆蓋密碼,可以執(zhí)行以下查詢:
mysql> set password for 'nonexistant'@'%'='hello$!world'; Empty set (0.00 sec)
五、Bash 轉(zhuǎn)換密碼中的特殊字符
[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>
六、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>
最后,如果您真的被鎖定并需要繞過身份驗證機制以重新獲得對數(shù)據(jù)庫的訪問權(quán)限,請執(zhí)行以下幾個簡單步驟:
本文按常見到復雜的順序?qū)⒖赡軋?1045 的錯誤原因全部列舉出來,看完了還不趕快收藏!
參考:https://www.percona.com/blog/2019/07/05/fixing-a-mysql-1045-error/
PS :今天上傳了一個項目在服務(wù)器上面,然后數(shù)據(jù)庫出現(xiàn)錯誤,在網(wǎng)上找了各種方法,親測一下有用;
[mysqld]項下添加
skip-grant-tables
sudo service mysql restart
mysql -uroot -p
use mysql;
update user set password=password('123') where user='root';
flush privileges;
exit;
sudo vim /etc/mysql/my.cnf #skip-grant-tables(注釋或刪除)
mysql -uroot -p123