Resolve the problem caused by Node. js mysql client not supporting authentication protocol

  • 2021-12-05 07:43:25
  • OfStack

Preface

The mysql module (project address is https://github.com/mysqljs/mysql) is an open source MySQL driver written by JavaScript, which can operate MySQL in Node.js applications. However, the problem of "ER_NOT_SUPPORTED_AUTH_MODE" appears in the process of use.

This paper introduces the causes and solutions of this problem.

Error message

When I tried to connect MySQL 8 using the mysql module, the following error message appeared:


D:\workspaceGithub\nodejs-book-samples\samples\mysql-demo\index.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:\workspaceGithub\nodejs-book-samples\samples\mysql-demo\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Handshake.ErrorPacket (D:\workspaceGithub\nodejs-book-samples\samples\mysql-demo\node_modules\mysql\lib\protocol\sequences\Handshake.js:123:18)
at Protocol._parsePacket (D:\workspaceGithub\nodejs-book-samples\samples\mysql-demo\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (D:\workspaceGithub\nodejs-book-samples\samples\mysql-demo\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (D:\workspaceGithub\nodejs-book-samples\samples\mysql-demo\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (D:\workspaceGithub\nodejs-book-samples\samples\mysql-demo\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (D:\workspaceGithub\nodejs-book-samples\samples\mysql-demo\node_modules\mysql\lib\Connection.js:91:28)
at Socket.<anonymous> (D:\workspaceGithub\nodejs-book-samples\samples\mysql-demo\node_modules\mysql\lib\Connection.js:525:10)
at Socket.emit (events.js:196:13)
at addChunk (_stream_readable.js:290:12)
--------------------
at Protocol._enqueue (D:\workspaceGithub\nodejs-book-samples\samples\mysql-demo\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (D:\workspaceGithub\nodejs-book-samples\samples\mysql-demo\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (D:\workspaceGithub\nodejs-book-samples\samples\mysql-demo\node_modules\mysql\lib\Connection.js:119:18)
at Object.<anonymous> (D:\workspaceGithub\nodejs-book-samples\samples\mysql-demo\index.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

Error cause

The reason for this error is that at present, the latest mysql module does not fully support the "caching_sha2_password" encryption mode of MySQL 8, while "caching_sha2_password" is the default encryption mode in MySQL 8. Therefore, the following mode command has used the encryption mode of "caching_sha2_password" by default, and this account number and password cannot be used in mysql module.


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

Solution

The solution is to re-modify the password of the user root and specify the encryption methods that the mysql module can support:


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

The above statement shows that the encryption method using "mysql_native_password" is specified. This approach is supported in the mysql module.

When you run the application here, you can see the following console output information:


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

Among them, "RowDataPacket {user_id: 1, username: 'Lao Wei'}" is the result of database query.

Source code

Examples in this section can be found in the "mysql-demo" application of https://github.com/waylau/nodejs-book-samples.


Related articles: