MySQL報(bào)1045錯(cuò)誤的幾種可能場(chǎng)景
在我們學(xué)習(xí) MySQL 或從事 MySQL DBA 工作期間,時(shí)常會(huì)遇到:“我嘗試連接到 MySQL 并且收到1045 錯(cuò)誤,但我確定我的用戶和密碼都沒問題”。
不管你現(xiàn)在是否是高手還是高高手,都不可避免曾經(jīng)在初學(xué)的時(shí)候犯過一些很初級(jí)的錯(cuò)誤,例如:用戶名密碼都填錯(cuò)了。而且工作一段時(shí)間后也偶爾會(huì)遇到一些不常見錯(cuò)誤原因。
一、連接錯(cuò)誤的主機(jī)
[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)
如果未指定要連接的主機(jī)(使用 -h 標(biāo)志),則 MySQL 客戶端將嘗試連接到 localhost 實(shí)例,同時(shí)您可能嘗試連接到另一個(gè)主機(jī)端口實(shí)例。
修復(fù):仔細(xì)檢查您是否嘗試連接到 localhost,或者確保指定主機(jī)和端口(如果它不是 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)
修復(fù):仔細(xì)檢查用戶是否存在:
mysql> SELECT User FROM mysql.user WHERE User='nonexistant'; Empty set (0.00 sec)
如果用戶不存在,請(qǐng)創(chuàng)建一個(gè)新用戶:
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)
三、用戶存在但客戶端主機(jī)無權(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)
修復(fù):您可以通過以下查詢檢查 MySQL 允許連接的主機(jī)用戶/主機(jī):
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)建具有正確主機(jī)(客戶端 IP)的用戶,或使用'%'(通配符)來匹配任何可能的 IP:
mysql> CREATE USER 'nonexistant'@'%' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.00 sec)
四、密碼錯(cuò)誤,或者用戶忘記密碼
mysql> CREATE USER 'nonexistant'@'%' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.00 sec)
修復(fù):檢查和/或重置密碼:
您無法從 MySQL 以純文本格式讀取用戶密碼,因?yàn)槊艽a哈希用于身份驗(yàn)證,但您可以將哈希字符串與“PASSWORD”函數(shù)進(jìn)行比較:
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)
修復(fù):通過在單引號(hào)中包裝密碼來防止 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)
修復(fù):添加 -ssl-mode 標(biāo)志(-ssl 標(biāo)志已棄用但也可以使用)
[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>
如果真的被鎖定并需要繞過身份驗(yàn)證機(jī)制以重新獲得對(duì)數(shù)據(jù)庫的訪問權(quán)限,請(qǐng)執(zhí)行以下幾個(gè)簡(jiǎn)單步驟,
- 停止實(shí)例
- 編輯 my.cnf 并在 [mysqld] 下添加 skip-grant-tables(這樣可以在不提示輸入密碼的情況下訪問 MySQL)。在 MySQL 8.0 上,跳過網(wǎng)絡(luò)是自動(dòng)啟用的(只允許從 localhost 訪問 MySQL),但對(duì)于以前的 MySQL 版本,建議在 [mysqld] 下添加 -skip-networking
- 啟動(dòng)實(shí)例
- 使用 root 用戶訪問(mysql -uroot -hlocalhost);
- 發(fā)出必要的 GRANT / CREATE USER / SET PASSWORD 以糾正問題(可能設(shè)置一個(gè)已知的 root 密碼將是正確的事情:SET PASSWORD FOR 'root'@'localhost'='S0vrySekr3t'
- 停止實(shí)例
- 編輯 my.cnf 并刪除 skip-grant-tables 和 skip-networking
- 再次啟動(dòng) MySQL
- 您應(yīng)該能夠使用 roothost 從 root 用戶登錄,并對(duì) root 用戶執(zhí)行任何其他必要的糾正操作。
到此這篇關(guān)于MySQL報(bào)1045錯(cuò)誤的幾種可能場(chǎng)景的文章就介紹到這了,更多相關(guān)MySQL報(bào)1045錯(cuò)誤內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 解決mysql ERROR 1045 (28000)-- Access denied for user問題
- mysql ERROR 1045 (28000)問題的解決方法
- mysql數(shù)據(jù)庫中1045錯(cuò)誤的解決方法
- 解決centos下MySQL登錄1045問題
- MySQL忘記root密碼錯(cuò)誤號(hào)碼1045的解決辦法
- 解決MySQL添加新用戶-ERROR?1045?(28000)的問題
- 全面分析MySQL?ERROR?1045出現(xiàn)的原因及解決
- MySQL8.0+版本1045錯(cuò)誤的問題及解決辦法
- Navicat連接MySQL提示1045錯(cuò)誤解決(重置MySQL密碼)
- mysql錯(cuò)誤碼1045解決方案
相關(guān)文章
在MySQL中如何存取List<String>數(shù)據(jù)
這篇文章主要介紹了在MySQL中如何存取List<String>數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
MySQL的索引詳細(xì)介紹(全網(wǎng)最新整理)
這篇文章詳細(xì)介紹了MySQL中索引的定義、數(shù)據(jù)結(jié)構(gòu)、優(yōu)缺點(diǎn)、使用場(chǎng)景以及如何創(chuàng)建和刪除索引,還講解了如何使用EXPLAIN關(guān)鍵字分析查詢性能,并提供了索引失效的常見原因和優(yōu)化建議,最后,文章提到慢查詢?nèi)罩镜氖褂梅椒ê妥饔?感興趣的朋友跟隨小編一起看看吧2026-01-01
MySQL數(shù)據(jù)庫遷移快速導(dǎo)出導(dǎo)入大量數(shù)據(jù)
今天小編就為大家分享一篇關(guān)于MySQL數(shù)據(jù)庫遷移快速導(dǎo)出導(dǎo)入大量數(shù)據(jù),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
兩大步驟教您開啟MySQL 數(shù)據(jù)庫遠(yuǎn)程登陸帳號(hào)的方法
在工作實(shí)踐和學(xué)習(xí)中,如何開啟 MySQL 數(shù)據(jù)庫的遠(yuǎn)程登陸帳號(hào)算是一個(gè)難點(diǎn)的問題,以下內(nèi)容便是在工作和實(shí)踐中總結(jié)出來的兩大步驟,能幫助DBA們順利的完成開啟 MySQL 數(shù)據(jù)庫的遠(yuǎn)程登陸帳號(hào)。2011-03-03
Mysql8.0.42啟用SSL安全連接完整步驟(等保整改、docker部署)
作為一名剛?cè)胄械拈_發(fā)者,你可能會(huì)遇到需要開啟MySQL數(shù)據(jù)庫的SSL加密連接的需求,這篇文章主要介紹了Mysql8.0.42啟用SSL安全連接(等保整改、docker部署)的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2026-04-04

