客户端连接 mysql 报错

391 阅读1分钟

报错:

在做 egg.js 通过 egg-mysql 连接数据库时报以下错误。

nodejs.ER_NOT_SUPPORTED_AUTH_MODEError: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading 
MySQL client

原因:

mysql8 之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是caching_sha2_password。而 egg-mysql 客户端还是使用以前的方式。其实这个错误是客户端连接 mysql 报错,因为不仅仅是 egg-mysql,navicat 也是一样的。

image (11).png

我们先随便进一个库,可以看到确实是 caching_sha2_password

image (12).png

解决:

改为 mysql_native_password 加密规则。

先以管理员身份运行cmd,然后使用命令进入mysql

mysql -u root -p

mysql中先更改加密方式

alter user 'root'@'localhost' identified by '新密码' password expire never;

然后再更改密码,由于加密规则更改,所以需要重新设置密码;

alter user 'root'@'localhost' identified with mysql_native_password by '新密码';

最后再刷新一下数据库;

FLUSH PRIVILEGES;

image (13).png

然后我们退出重新登陆下。

我们再查看下,酱酱~这样就修改成功啦!

image (14).png