数据库
首页 > 数据库> > Node.js无法对MySQL 8.0进行身份验证

Node.js无法对MySQL 8.0进行身份验证

作者:互联网

我有一个Node.js程序,它使用root帐户连接到本地MySQL数据库(这不是生产设置).
这是创建连接的代码:

const mysql = require('mysql');

const dbConn = mysql.createConnection({
    host: 'localhost',
    port: 3306,
    user: 'root',
    password: 'myRootPassword',
    database: 'decldb'
});

dbConn.connect(err => {
    if (err) throw err;
    console.log('Connected!');
});

它适用于MySQL 5.7,但自从安装MySQL 8.0后,我在启动Node.js应用程序时遇到此错误:

> node .\api-server\main.js
[2018-05-16T13:53:53.153Z] Server launched on port 3000!
C:\Users\me\project\node_server\node_modules\mysql\lib\protocol\Parser.js:80
        throw err; // Rethrow non-MySQL errors
        ^

Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
    at Handshake.Sequence._packetToError (C:\Users\me\project\node_server\node_modules\mysql\lib\protocol\sequences\Sequence.js:52:14)
    at Handshake.ErrorPacket (C:\Users\me\project\node_server\node_modules\mysql\lib\protocol\sequences\Handshake.js:130:18)
    at Protocol._parsePacket (C:\Users\me\project\node_server\node_modules\mysql\lib\protocol\Protocol.js:279:23)
    at Parser.write (C:\Users\me\project\node_server\node_modules\mysql\lib\protocol\Parser.js:76:12)
    at Protocol.write (C:\Users\me\project\node_server\node_modules\mysql\lib\protocol\Protocol.js:39:16)
    at Socket.<anonymous> (C:\Users\me\project\node_server\node_modules\mysql\lib\Connection.js:103:28)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    --------------------
    at Protocol._enqueue (C:\Users\me\project\node_server\node_modules\mysql\lib\protocol\Protocol.js:145:48)
    at Protocol.handshake (C:\Users\me\project\node_server\node_modules\mysql\lib\protocol\Protocol.js:52:23)
    at Connection.connect (C:\Users\me\project\node_server\node_modules\mysql\lib\Connection.js:130:18)
    at Object.<anonymous> (C:\Users\me\project\node_server\main.js:27:8)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)

似乎root帐户使用新密码哈希方法:

> select User,Host,plugin from user where User="root";
+------+-----------+-----------------------+
| User | Host      | plugin                |
+------+-----------+-----------------------+
| root | localhost | caching_sha2_password |
+------+-----------+-----------------------+

…但我不知道为什么Node.js无法连接到它.我已经更新了所有的npm软件包,但仍然存在问题.

我想保留新密码哈希方法.我还可以使这个连接工作吗?我是否必须等待更新MySQL Node.js包,或更改我的设置?

解决方法:

MySQL 8.0使用新的默认身份验证插件 – caching_sha2_password – 而MySQL 5.7使用不同的 – mysql_native_password.目前,MySQL的社区Node.js驱动程序不支持新服务器插件的兼容客户端身份验证机制.

可能的解决方法是更改​​用户帐户的类型以使用旧的身份验证插件:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'MyNewPass';

或者创建一个使用相同插件的不同的插件:

CREATE USER 'foo'@'localhost' IDENTIFIED WITH mysql_native_password BY 'bar';

有一个pull request正在准备妥善解决这个问题.

另一种选择是使用official MySQL Node.js连接器(完全公开:我是主要开发人员),它基于X Protocol并且已经支持新的身份验证模式.

标签:mysql,node-js,authentication,mysql-8-0
来源: https://codeday.me/bug/20190916/1808275.html