node.js中连接mysql报错errno: 1251

550 阅读1分钟

报错信息

node.js中尝试连接 mysql 8.0 时出现以下错误

code: 'ER_NOT_SUPPORTED_AUTH_MODE',
errno: 1251,
sqlMessage: 'Client does not support authentication protocol requested by server; 
consider upgrading MySQL client',
sqlState: '08004',
fatal: true

原因

MySQL 8.0 使用 caching_sha2_password 作为默认身份验证插件,不像 MySQL 5.7 默认 mysql_native_password,大多数客户端都支持

解决方案

打开mysql自带的命令行客户端,输入以下指令

ALTER USER 'root'@'localhost' IDENTIFIED BY 'your password'; 
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your password';

FLUSH PRIVILEGES;