问题描述
我有一个 Node.js 程序,它使用 root 帐户连接到本地 MySQL 数据库(这不是生产设置).这是创建连接的代码:
I have a Node.js program that connects to a local MySQL database with the root account (this is not a production setup). This is the code that creates the connection:
它适用于 MySQL 5.7,但自从安装 MySQL 8.0 后,我在启动 Node.js 应用程序时出现此错误:
It worked with MySQL 5.7, but since installing MySQL 8.0 I get this error when starting the Node.js app:
似乎root账户使用了一种新的密码散列方法:
It seems that the root account uses a new password hashing method:
...但我不知道为什么 Node.js 无法连接到它.我已经更新了所有 npm 包,但它仍然是一个问题.
...but I don't know why Node.js is unable to connect to it. I have updated all the npm packages and it's still an issue.
我想保留新的密码散列方法.我还能让这个连接工作吗?我是否必须等待 MySQL Node.js 包的更新,或者更改我这边的设置?
I would like to keep the new password hashing method. Can I still make this connection work? Do I have to wait for an update of the MySQL Node.js package, or change a setting on my side?
推荐答案
MySQL 8.0 使用新的默认身份验证插件 - caching_sha2_password - 而 MySQL 5.7 使用了不同的 - mysql_native_password.目前,MySQL 的社区 Node.js 驱动程序不支持新服务器插件的兼容客户端身份验证机制.
MySQL 8.0 uses a new default authentication plugin - caching_sha2_password - whereas MySQL 5.7 used a different one - mysql_native_password. Currently, the community Node.js drivers for MySQL don't support compatible client-side authentication mechanisms for the new server plugin.
一种可能的解决方法是更改用户帐户的类型以使用旧的身份验证插件:
A possible workaround is to alter the type of user account to use the old authentication plugin:
或者创建一个使用相同插件的不同插件:
Or create a different one that uses that same plugin:
管道中有一个拉取请求以正确解决该问题.
There's a pull request in pipeline to properly address the issue.
另一种选择是使用官方 MySQL Node.js 连接器(完全披露:我是首席开发者),它基于 X协议并且已经支持新的认证模式.
Another option is to use the official MySQL Node.js connector (full disclosure: I'm the lead dev), which is based on the X Protocol and already supports the new authentication mode.
这篇关于Node.js 无法对 MySQL 8.0 进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!