Home > Database > Mysql Tutorial > body text

Why am I Getting 'ER_NOT_SUPPORTED_AUTH_MODE' Errors Connecting to MySQL from Node.js?

Mary-Kate Olsen
Release: 2024-11-10 02:14:02
Original
282 people have browsed it

Why am I Getting

Troubleshooting "ER_NOT_SUPPORTED_AUTH_MODE" Error in MySQL-Node.js Connection

The "ER_NOT_SUPPORTED_AUTH_MODE" error indicates that the Node.js client cannot establish a connection with the MySQL server due to an unsupported authentication protocol. This issue arises when the MySQL server requires a specific authentication method that the client does not support.

In your case, you have upgraded from MariaDB to MySQL. However, the JavaScript connection snippet you are using may still be using the MariaDB authentication protocol, which is not supported by MySQL 8.0 and above.

Solution:

To resolve this issue, you need to upgrade your Node.js client to support the MySQL native authentication protocol. This protocol is compatible with both MariaDB and MySQL servers.

For MySQL version 8.0 and above, use the following command to update the root user's authentication method:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Copy after login

Replace "root" with the username and "password" with the desired password.

Once the authentication method has been updated, execute the following command to activate the changes:

FLUSH PRIVILEGES;
Copy after login

Now, modify your JavaScript connection snippet to use the MySQL native protocol:

var mysql = require('mysql');

var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'password',
    database: 'foobarDb',
    authPlugin: 'mysql_native_password' // specify the MySQL native authentication protocol
});
Copy after login

This should resolve the "ER_NOT_SUPPORTED_AUTH_MODE" error and allow you to connect to the MySQL database from your Node.js server.

The above is the detailed content of Why am I Getting 'ER_NOT_SUPPORTED_AUTH_MODE' Errors Connecting to MySQL from Node.js?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template