Client does not support authentication protocol requested by server; consider upgrading MySQL client

1. 問(wèn)題描述:

我電腦安裝了MySQL版本是:


MySQL版本號(hào)8.0.12

我在學(xué)習(xí)使用mpvue開(kāi)發(fā)微信小程序前后端時(shí),使用的是騰訊云提供的node服務(wù),在server->config.js中:

mysql: {
    host: 'localhost',
    port: 3306,
    user: 'root',
    db: 'cAuth',
    pass: 'MySQL密碼',
    char: 'utf8mb4'
  },

配置好 config.js 之后,就要開(kāi)始初始化環(huán)境,初始化環(huán)境分為兩步

# 切換到服務(wù)端代碼目錄
cd server

# 安裝依賴
npm install

# 安裝全局依賴
npm install -g nodemon

初始化數(shù)據(jù)庫(kù) - 打開(kāi) CMD 輸入如下命令:

node tools/initdb.js

此時(shí)報(bào)錯(cuò):

?  node tools/initdb.js 

======================================
開(kāi)始初始化數(shù)據(jù)庫(kù)...
準(zhǔn)備讀取 SQL 文件:/Users/zhangxi/Documents/study/小程序/my-project/server/tools/cAuth.sql
開(kāi)始執(zhí)行 SQL 文件...
Unhandled rejection Error: Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
    at DB.raw.then.err (/Users/zhangxi/Documents/study/小程序/my-project/server/tools/initdb.js:40:11)
    at tryCatcher (/Users/zhangxi/Documents/study/小程序/my-project/server/node_modules/_bluebird@3.5.2@bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/zhangxi/Documents/study/小程序/my-project/server/node_modules/_bluebird@3.5.2@bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/Users/zhangxi/Documents/study/小程序/my-project/server/node_modules/_bluebird@3.5.2@bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/Users/zhangxi/Documents/study/小程序/my-project/server/node_modules/_bluebird@3.5.2@bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/Users/zhangxi/Documents/study/小程序/my-project/server/node_modules/_bluebird@3.5.2@bluebird/js/release/promise.js:690:18)
    at _drainQueueStep (/Users/zhangxi/Documents/study/小程序/my-project/server/node_modules/_bluebird@3.5.2@bluebird/js/release/async.js:138:12)
    at _drainQueue (/Users/zhangxi/Documents/study/小程序/my-project/server/node_modules/_bluebird@3.5.2@bluebird/js/release/async.js:131:9)
    at Async._drainQueues (/Users/zhangxi/Documents/study/小程序/my-project/server/node_modules/_bluebird@3.5.2@bluebird/js/release/async.js:147:5)
    at Immediate.Async.drainQueues [as _onImmediate] (/Users/zhangxi/Documents/study/小程序/my-project/server/node_modules/_bluebird@3.5.2@bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:696:18)
    at tryOnImmediate (timers.js:667:5)

2. 原因解釋:

主要原因是mysql服務(wù)器要求的認(rèn)證插件版本與客戶端不一致造成的。
打開(kāi)mysql命令行輸入如下命令查看,系統(tǒng)用戶對(duì)應(yīng)的認(rèn)證插件:
命令進(jìn)入MySQL內(nèi):

select user,plugin from mysql. user;
進(jìn)去

可以看到root用戶使用的plugin是caching_sha2_password,mysql官方網(wǎng)站有如下說(shuō)明:


image.png

意思是說(shuō)caching_sha2_password是8.0默認(rèn)的認(rèn)證插件,必須使用支持此插件的客戶端版本。
plugin的作用之一就是處理后的密碼格式和長(zhǎng)度是不一樣的,類似于使用MD5加密和使用base64加密一樣對(duì)于同一個(gè)密碼處理后的格式是不一樣的。

3. 解決方法:

我不希望更新本地的客戶端版本,想直接使用原來(lái)的環(huán)境來(lái)鏈接。
解決方法是將root的plugin改成mysql_native_password。相當(dāng)于降了一級(jí)。
mysql官方網(wǎng)站提供了從mysql_old_password升級(jí)到mysql_native_password,我們可以仿照這個(gè)。官方原文如下:


image.png

這里改成:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你想設(shè)置的MySQL登錄密碼';

這行代碼有兩層含義,第一:修改root的密碼為'你想設(shè)置的MySQL登錄密碼',摒棄原來(lái)的舊密碼。第二:使用mysql_native_password對(duì)新密碼進(jìn)行編碼。


image.png

我這里將密碼設(shè)置為'',意味著不設(shè)置密碼

修改完之后再到server里面執(zhí)行連接數(shù)據(jù)庫(kù)

node tools/initdb.js
image.png

4. 補(bǔ)充:

如果在修改插件的時(shí)候出現(xiàn)錯(cuò)誤,可現(xiàn)將插件改為 mysql_old_password,然后再升級(jí)成mysql_native_password,方法:


image.png

參考:navicat for mysql 鏈接時(shí)報(bào)錯(cuò):1251-Client does not support authentication protocol requested by server

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容