在nodejs使用mysql库中连接mysql8的问题

由于mysql8.0的更新,mysql的默认认证方式从 mysql_native_password更换为caching_sha2_password所以导致 mysql 库用不了,报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
(node:11152) UnhandledPromiseRejectionWarning: Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
at Handshake.Sequence._packetToError (d:\workspace\gendanbao\htmlDemo\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Handshake.ErrorPacket (d:\workspace\gendanbao\htmlDemo\node_modules\mysql\lib\protocol\sequences\Handshake.js:123:18)
at Protocol._parsePacket (d:\workspace\gendanbao\htmlDemo\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (d:\workspace\gendanbao\htmlDemo\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (d:\workspace\gendanbao\htmlDemo\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (d:\workspace\gendanbao\htmlDemo\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (d:\workspace\gendanbao\htmlDemo\node_modules\mysql\lib\Connection.js:91:28)
at Socket.<anonymous> (d:\workspace\gendanbao\htmlDemo\node_modules\mysql\lib\Connection.js:525:10)
at Socket.emit (events.js:189:13)
at addChunk (_stream_readable.js:284:12)
--------------------
at Protocol._enqueue (d:\workspace\gendanbao\htmlDemo\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (d:\workspace\gendanbao\htmlDemo\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at PoolConnection.connect (d:\workspace\gendanbao\htmlDemo\node_modules\mysql\lib\Connection.js:119:18)
at Pool.getConnection (d:\workspace\gendanbao\htmlDemo\node_modules\mysql\lib\Pool.js:48:16)
at d:\workspace\gendanbao\htmlDemo\node_modules\typeorm\driver\mysql\MysqlDriver.js:764:18
at new Promise (<anonymous>)
at MysqlDriver.createPool (d:\workspace\gendanbao\htmlDemo\node_modules\typeorm\driver\mysql\MysqlDriver.js:761:16)
at MysqlDriver.<anonymous> (d:\workspace\gendanbao\htmlDemo\node_modules\typeorm\driver\mysql\MysqlDriver.js:274:51)
at step (d:\workspace\gendanbao\htmlDemo\node_modules\tslib\tslib.js:136:27)
at Object.next (d:\workspace\gendanbao\htmlDemo\node_modules\tslib\tslib.js:117:57)
warning.js:18
(node:11152) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
warning.js:18
(node:11152) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

截止目前(2019-07-12)为止,官方仅仅提出了修复的PR,但是还没有合并到项目里面去。

目前能在网上找到的解决方案有两个:

  1. 设置数据库,使用原来的认证方式

    1
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_new_password';
  1. 使用 X DevAPI 库替换现在的库

    X DevAPImysql 官方推出的库。地址

参考: