Node.js mysql客户端不支持认证协议怎么办 如何解决

作者:袖梨 2022-06-29

本篇文章小编给大家分享一下Node.js mysql客户端不支持认证协议解决方法,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

前言

mysql模块(项目地址为https://github.com/mysqljs/mysql)是一个开源的、JavaScript编写的MySQL驱动,可以在Node.js应用中来操作MySQL。但在使用过程中,出现了“ER_NOT_SUPPORTED_AUTH_MODE”问题。

本文介绍了出现该问题的原因及解决方案。

报错信息

当我试图使用mysql模块来连接MySQL 8时,出现了如下错误信息:

D:workspaceGithubnodejs-book-samplessamplesmysql-demoindex.js:17
throw error;
^
Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
at Handshake.Sequence._packetToError (D:workspaceGithubnodejs-book-samplessamplesmysql-demonode_modulesmysqllibprotocolsequencesSequence.js:47:14)
at Handshake.ErrorPacket (D:workspaceGithubnodejs-book-samplessamplesmysql-demonode_modulesmysqllibprotocolsequencesHandshake.js:123:18)
at Protocol._parsePacket (D:workspaceGithubnodejs-book-samplessamplesmysql-demonode_modulesmysqllibprotocolProtocol.js:291:23)
at Parser._parsePacket (D:workspaceGithubnodejs-book-samplessamplesmysql-demonode_modulesmysqllibprotocolParser.js:433:10)
at Parser.write (D:workspaceGithubnodejs-book-samplessamplesmysql-demonode_modulesmysqllibprotocolParser.js:43:10)
at Protocol.write (D:workspaceGithubnodejs-book-samplessamplesmysql-demonode_modulesmysqllibprotocolProtocol.js:38:16)
at Socket. (D:workspaceGithubnodejs-book-samplessamplesmysql-demonode_modulesmysqllibConnection.js:91:28)
at Socket. (D:workspaceGithubnodejs-book-samplessamplesmysql-demonode_modulesmysqllibConnection.js:525:10)
at Socket.emit (events.js:196:13)
at addChunk (_stream_readable.js:290:12)
--------------------
at Protocol._enqueue (D:workspaceGithubnodejs-book-samplessamplesmysql-demonode_modulesmysqllibprotocolProtocol.js:144:48)
at Protocol.handshake (D:workspaceGithubnodejs-book-samplessamplesmysql-demonode_modulesmysqllibprotocolProtocol.js:51:23)
at Connection.connect (D:workspaceGithubnodejs-book-samplessamplesmysql-demonode_modulesmysqllibConnection.js:119:18)
at Object. (D:workspaceGithubnodejs-book-samplessamplesmysql-demoindex.js:12:12)
at Module._compile (internal/modules/cjs/loader.js:759:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
at Module.load (internal/modules/cjs/loader.js:628:32)
at Function.Module._load (internal/modules/cjs/loader.js:555:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:826:10)
at internal/main/run_main_module.js:17:11

出错原因

导致这个错误的原因是,目前,最新的mysql模块并未完全支持MySQL 8的“caching_sha2_password”加密方式,而“caching_sha2_password”在MySQL 8中是默认的加密方式。因此,下面的方式命令是默认已经使用了“caching_sha2_password”加密方式,该账号、密码无法在mysql模块中使用。

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.12 sec)

解决方法

解决方法是从新修改用户root的密码,并指定mysql模块能够支持的加密方式:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.12 sec)

上述语句,显示指定了使用“mysql_native_password”的加密方式。这种方式是在mysql模块能够支持。

再此运行应用,可以看到如下的控制台输出信息:

$ node index.js
The result is: RowDataPacket { user_id: 1, username: '老卫' }

其中,“RowDataPacket { user_id: 1, username: ‘老卫' }”就是数据库查询的结果。

相关文章

精彩推荐