今天使用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 install --save mysql2
const mysql = require('mysql2');
再次运行项目: