Solve the problem that the Node.js mysql client does not support the authentication protocol

Solve the problem that the Node.js mysql client does not support the authentication protocol

Preface

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

This article describes the cause and solution of this problem.

Error message

When I try to connect to MySQL 8 using the mysql module, I get the following error message:

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

Cause of error

The reason for this error is that currently, the latest MySQL module does not fully support the "caching_sha2_password" encryption method of MySQL 8, and "caching_sha2_password" is the default encryption method in MySQL 8. Therefore, the following command uses the "caching_sha2_password" encryption method by default, and the account and password cannot be used in the mysql module.

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

Workaround

The solution is to modify the password of user root and specify the encryption method 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 method is supported by the mysql module.

Run the application again and you can see the following console output:

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

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

Source code

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

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Introduction to using the MySQL mysqladmin client
  • How does MySQL connect to the corresponding client process?
  • Solve the problem of MySql client exiting in seconds (my.ini not found)
  • PHP Swoole asynchronous MySQL client implementation example
  • Solution to the problem that the mysql8.0.11 client cannot log in
  • Detailed explanation of MySQL/Java server support for emoji and problem solving
  • Getting started with NodeJS server development (Express+MySQL)
  • MySQL connection pool for App server (supports high concurrency)
  • Interpreting MySQL client and server protocols

<<:  WeChat applet implements a simple handwritten signature component

>>:  Detailed explanation of browser negotiation cache process based on nginx

Recommend

Solution to the 404/503 problem when logging in to TeamCenter12

TeamCenter12 enters the account password and clic...

JavaScript imitates Jingdong magnifying glass special effects

This article shares the specific code of JavaScri...

Web page html special symbols html special characters comparison table

Special symbols Named Entities Decimal encoding S...

Explanation of the working mechanism of namenode and secondarynamenode in Hadoop

1) Process 2) FSImage and Edits Nodenode is the b...

What is ssh port forwarding? What's the use?

Table of contents Preface 1. Local port forwardin...

React concurrent function experience (front-end concurrent mode)

React is an open-source JavaScript library used b...

Data Structure - Tree (III): Multi-way Search Tree B-tree, B+ tree

Multi-way search tree Height of a complete binary...

Solve the problem that Navicat cannot connect to MySQL on the Linux server

At the beginning, I felt sad. The screenshots are...

Detailed description of the use of advanced configuration of Firewalld in Linux

IP masquerading and port forwarding Firewalld sup...

How to smoothly go online after MySQL table partitioning

Table of contents Purpose of the table For exampl...

Manually implement js SMS verification code input box

Preface This article records a common SMS verific...

4 ways to view processes in LINUX (summary)

A process is a program code that runs in the CPU ...

Use PSSH to batch manage Linux servers

pssh is an open source software implemented in Py...

Zabbix monitoring solution - the latest official version 4.4 [recommended]

Zabbix 2019/10/12 Chenxin refer to https://www.za...