mysqli::real_connect(): Unexpected server response while doing caching_sha2 auth: 109 或者 Message: mysqli::real_connect(): (HY000/1045): Access denied for user 'root'@'114.254.211.9' (using password: YES).
前提條件:確定用戶和密碼已支持遠(yuǎn)程連接, 且密碼的加密規(guī)則已修改為 密碼驗(yàn)證插件 mysql_native_password 支持的密碼
報(bào)錯(cuò) mysqli::real_connect(): Unexpected server response while doing caching_sha2 auth: 109 來(lái)源1:mysql_native_password 本地可以連钞护,線上連不了
分析:
確認(rèn)過(guò)后版本一致盖喷,判斷是 mysql 的配置問(wèn)題
解決:
[mysqld]
default_authentication_plugin = mysql_native_password
重啟數(shù)據(jù)庫(kù)
結(jié)果:
成功
報(bào)錯(cuò) mysqli::real_connect(): Unexpected server response while doing caching_sha2 auth: 109 來(lái)源2:mysql_native_password 線上可以連,本地由php 7.3.x 更新為 php 7.4.x 之后連不了
分析:
php 7.4.x 之前 mysqli 擴(kuò)展連接默認(rèn)是用 mysql_native_password 密碼驗(yàn)證插件难咕,
php 7.4.x 之后 mysqli 擴(kuò)展連接默認(rèn)是用 caching_sha2_password 密碼驗(yàn)證插件课梳。
所以php 7.4 之后在 mysql 配置中不默認(rèn)驗(yàn)證插件為 mysql_native_password 時(shí), mysqli擴(kuò)展 會(huì)使用 caching_sha2_password 密碼驗(yàn)證插件連接, 故報(bào)錯(cuò)余佃。
解決:
1. 重裝 php 環(huán)境到 7.4.0 以下版本 【不推薦】
2. 根據(jù)上面來(lái)源進(jìn)行解決:
[mysqld]
default_authentication_plugin = mysql_native_password
重啟數(shù)據(jù)庫(kù)
報(bào)錯(cuò) Message: mysqli::real_connect(): (HY000/1045): Access denied for user 'root'@'114.254.211.9' (using password: YES) 來(lái)源:php 7.4.x 環(huán)境中暮刃,在剛使用 docker 創(chuàng)建的數(shù)據(jù)庫(kù)時(shí)默認(rèn)的密碼驗(yàn)證插件就是 caching_sha2_password ,而且可以連爆土;之后同樣密碼驗(yàn)證插件是 caching_sha2_password椭懊,使用 navicate 修改密碼后連不了。
分析-1:
php 7.4.x 的mysqli擴(kuò)展支持新的 caching_sha2_password步势,判斷 navicat 使用的密碼驗(yàn)證插件 caching_sha2_password 版本問(wèn)題
解決:
在 mysql 最新的客戶端使用密碼驗(yàn)證插件 caching_sha2_password 修改密碼
ALTER USER 'root'@'%' IDENTIFIED BY 'ffc' PASSWORD EXPIRE NEVER;
ALTER USER 'root'@'%' IDENTIFIED WITH caching_sha2_password BY 'ffc';
FLUSH PRIVILEGES;
結(jié)果:
失敗, 仍然報(bào)錯(cuò):Message: mysqli::real_connect(): (HY000/1045): Access denied for user 'root'@'114.254.211.9' (using password: YES).
結(jié)論:
與 navicate 的密碼驗(yàn)證插件無(wú)關(guān)
分析-2:
與操作執(zhí)行的順序有關(guān)氧猬。因?yàn)楫?dāng)前用戶在修改當(dāng)前用戶的密碼驗(yàn)證方式背犯,導(dǎo)致此連接不再可用,那么修改之后是否生效呢狂窑?
解決:
在 navicat 或者 mysql 客戶端中媳板, 在當(dāng)前用戶登錄連接后桑腮,
通過(guò)密碼驗(yàn)證插件 caching_sha2_password 修改當(dāng)前用戶的密碼泉哈,
然后使用當(dāng)前用戶的用戶名和新密碼重新連接成功后,
成功在 php 7.4.x 的 mysqli 擴(kuò)展中可以正常連接破讨。
思考:
如果修改的不是當(dāng)前用戶的密碼丛晦,是否需要斷開(kāi)重新連接呢?
答案:
是需要的提陶,并且要使用修改的用戶的用戶名和新密碼進(jìn)行重新登錄mysql服務(wù)器后烫沙,才可以在 php 7.4.x 的 mysqli 擴(kuò)展中連接。
在解決問(wèn)題之后隙笆,對(duì)密碼驗(yàn)證插件 caching_sha2_password 有些模糊锌蓄,果然在官網(wǎng)找到了相應(yīng)解釋
Cache clearing operations affect the authentication requirements for subsequent client connections. For each user account, the first client connection for the user after any of the following operations must use a secure connection (made using TCP using TLS credentials, a Unix socket file, or shared memory) or RSA key pair-based password exchange:
After account creation.
After a password change for the account.
After RENAME USER for the account.
After FLUSH PRIVILEGES.
FLUSH PRIVILEGES clears the entire cache and affects all accounts that use the caching_sha2_password plugin. The other operations clear specific cache entries and affect only accounts that are part of the operation.
Once the user authenticates successfully, the account is entered into the cache and subsequent connections do not require a secure connection or the RSA key pair, until another cache clearing event occurs that affects the account. (When the cache can be used, the server uses a challenge-response mechanism that does not use cleartext password transmission and does not require a secure connection.)
大概意思就是:
緩存清除操作會(huì)影響后續(xù)客戶端連接的身份驗(yàn)證要求。
對(duì)于每個(gè)用戶帳戶撑柔,在執(zhí)行以下任何操作之后瘸爽,
該用戶的第一個(gè)客戶端連接必須使用安全連接(使用TLS憑證,Unix套接字文件或共享內(nèi)存的TCP進(jìn)行安全連接)或基于RSA密鑰對(duì)的密碼交換:
創(chuàng)建帳戶后铅忿。
更改帳戶密碼后剪决。
為該帳戶 RENAME USER之后。
FLUSH PRIVILEGES 之后檀训。
FLUSH PRIVILEGES清除整個(gè)緩存并影響使用該caching_sha2_password插件的所有帳戶 柑潦。其他操作將清除特定的緩存條目,并且僅影響屬于該操作的帳戶峻凫。
總結(jié):
- php 7.4.x 的 mysqli 擴(kuò)展中默認(rèn)密碼驗(yàn)證插件為:caching_sha2_password
- 密碼驗(yàn)證插件為:caching_sha2_password 的用戶渗鬼,在修改密碼之后必須要重新使用客戶端成功連接后方可在php 7.4.x 的 mysqli 擴(kuò)展中連接。而密碼驗(yàn)證插件為:mysql_native_password 的用戶不需要重新通過(guò)客戶端連接成功即可在php 7.4.x 的 mysqli 擴(kuò)展中連接