软件版本
软件 | 版本 |
---|---|
Ubuntu | 20.04 LTS |
node.js | 12.18.3 LTS |
MySQL | 8.0.21 |
错误及原因
使用node的mysql模块连接MySQL,出现以下错误:
Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client.
错误详情如下所示:
{
"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",而node默认的密码验证模式是"mysql_native_password"。
解决方案
打开终端,输入以下命令和密码,连接MySQL。
mysql -u root -p
输入以下SQL语句,修改验证模式和密码。
ALTER USER "root"@"localhost" IDENTIFIED WITH mysql_native_password BY "eterfree";
输入以下SQL语句,刷新权限,使修改立即生效。
FLUSH PRIVILEGES;
退出MySQL。
exit;