node连接mysql:Client does not support authentication protocol requested by server; consider upgrading

今天使用node在连接mysql数据库的时候,我写了以下的代码;

const mysql = require('mysql');

const db = mysql.createPool({
    host: 'localhost', 
    user: 'root',  
    password: '123456', 
    database: 'test01' 
})

db.query('SELECT 1', (err, res) => {
    console.log(err)
    console.log(res)
})

然后运行的时候,报错了,错误信息:

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

 然后就小小的眼睛,大大的迷惑。。。一顿查找,找到问题原因;

mysql版本8.0以上更换了新的密码验证方式, node客户端不支持新的验证方式

解决办法:

mysql2 - npm

安装mysql2,运行命令:

npm install --save mysql2
const mysql = require('mysql2');

再次运行项目:

猜你喜欢

转载自blog.csdn.net/qq_42543244/article/details/126520858