node 连接 mysql 的时候遇到的报错
nodejs连接mysql的时候报这个错误
1 Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
代码:
const mysql = require("mysql") //导入mysql第三方模块
// 新建一个用于连接 mysql 数据库的对象
let connection = mysql.createConnection({
host: "localhost", //主机名
user: "root", //用户名
password: "****", //密码
database: "test", //数据库名称
})
// 连接数据库
connection.connect(function(err){
if(err){
console.log("error connection: " + err.stack);
return ;
}
console.log("connection as id" + connection.threadId) // 成功后返回线程id
})
原因:
导致这个错误的原因是,目前,最新的mysql模块并未完全支持MySQL 8的“caching_sha2_password”加密方式,而“caching_sha2_password”在MySQL 8中是默认的加密方式。
解决方法
mysql> ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘123456’; //成功后会出现这个 connection as id19

浙公网安备 33010602011771号