errno: 1251, sqlMessage: 'Client does not support authentication protocol requested by server; consider upgrading MySQL client
Asked Answered
L

12

17

I'm creating the simple api project in nodejs.

For that I have installed mySql workbench with version 8.0.11. I have created config file, dbConnection.js file and in the dbconnection.js file I have written the following code :

var mysql = require('mysql');
var path = require('path');
var config = require(path.resolve('./', 'config'))

var connection = mysql.createConnection({
    host: config.databaseHost,
    user: config.databaseUser,
    password: config.databasePassword,
    database: config.databaseDatabaseName,
    multipleStatements: true
});

connection.connect(function (err) {
    if (err) {
        console.log('Error connecting to Database',err);
        return;
    }
    console.log('Connection established');
});
module.exports = connection;

And I'm facing following error :

code: 'ER_NOT_SUPPORTED_AUTH_MODE', errno: 1251, sqlMessage: 'Client does not support authentication protocol requested by server; consider upgrading MySQL client', sqlState: '08004', fatal: true

Note : I kept my congif as simple as it can be, user = root , password = root, host = localhost.

Lillia answered 3/7, 2018 at 6:31 Comment(0)
B
14

Open terminal and login:

mysql -u root -p

(then type your password)

After that:

USE mysql;

UPDATE user SET authentication_string=password(''), plugin='mysql_native_password' WHERE user='root';

And the last important thing is to reset mysql service:

sudo service mysql restart
Bruckner answered 3/7, 2018 at 9:6 Comment(1)
When pasting in the UPDATE command I get the error: 1604 that there is an error with the syntaxAutopilot
M
27

once you establish your host, root, password &, etc inside your MySQL connection function.. paste the following inside of your database you are trying to connect within workbench with the following:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_current_password';

Go back to the terminal and run node <file name.js> again

Madaih answered 28/7, 2019 at 3:21 Comment(4)
This solution is the quickest and easiest way to solve the issueConvulse
Had absolutely no luck with this method. Same error continued to come up. I tried several other methods like creating a totally new user as well but the only thing that worked was using mysql2 as the library.Autopilot
Change the password valueWebfoot
worked for me in 2024 with workbench thanks!Thermoluminescent
C
19

Install mysql2 instead of mysql

npm install mysql2

Then add this to index.js

const mysql = require('mysql2')
Cnidoblast answered 21/1, 2022 at 7:36 Comment(0)
B
14

Open terminal and login:

mysql -u root -p

(then type your password)

After that:

USE mysql;

UPDATE user SET authentication_string=password(''), plugin='mysql_native_password' WHERE user='root';

And the last important thing is to reset mysql service:

sudo service mysql restart
Bruckner answered 3/7, 2018 at 9:6 Comment(1)
When pasting in the UPDATE command I get the error: 1604 that there is an error with the syntaxAutopilot
D
8

You have to reconfigure MySQL Server. for that follow these steps.

1) Open mysql intsaller.

2) Click on Reconfigure (to the left mysql server)

3) Go to Authentication method tab (by clicking on Next twice.)

4) Then select the radio button Use Legacy Authentication Method. Click Next.

5) Enter your password. Click on Check. Go to Apply Configuration (by clicking on Next twice).

6) Click on Execute. Then Finish.

That's it. Enjoy Hacking!!

Disunion answered 2/1, 2020 at 11:42 Comment(0)
C
7

Was getting ERROR 1251 in node v17 with mysql

❌ // didn't worked: errno 1251 ER_NOT_SUPPORTED_AUTH_MODE

import mysql from 'mysql';

Try to remove mysql from node and replace it for mysql2

yarn remove 'mysql'
yarn add 'mysql2'
✅  import mysql2 from 'mysql2';

This fixed the issue for me!

Chablis answered 21/3, 2022 at 4:16 Comment(0)
U
6
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'current password';
Urease answered 11/7, 2020 at 16:31 Comment(0)
H
0

I also had the same problem. It did not work for the root user. I created a new user (using mySQL workbench) and gave privileges to the new user. Then I tried with the new user and it worked fine. see workbench settings

Heteroecious answered 23/4, 2020 at 14:18 Comment(0)
H
0

Try to remove mysql

yarn remove mysql
yarn add mysql2
import mysql2 from "mysql2"

This fixed issue for me. code :

"ER_NOT_SUPPORTED_AUTH_MODE" errno : 1251 fatal : true sqlMessage : "Client does not support authentication protocol requested by server; consider upgrading MySQL client" sqlState : "08004"

Helfrich answered 28/11, 2022 at 1:38 Comment(1)
Please format your code properly using backticks.Gerhardt
R
0

Try this with your password and user name root if your password was not root try with your password.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
Regular answered 7/1, 2023 at 10:15 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Wormhole
M
0

I did not want to manipulate my "root" user because I'm using it in my Java applications, so that I decided to create a new User for NodeJS stuff.

I'm using MySQL 8.0.31, and Node's MySQL version "2.18.1" This solution really solved my problem:

first, log in to the MySQL server using root access:

mysql -u <MYSQL_USER> -p <MYSQL_PASSWORD>

once you entered mysql shell, run these commands:

create user '<YOUR_USERNAME>'@'localhost' IDENTIFIED WITH 'mysql_native_password' BY '<YOUR_PASSWORD>';

GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';

FLUSH PRIVILEGES;
Maximilianus answered 28/6, 2023 at 8:54 Comment(0)
D
0

1.Copy the code below

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'current password'

2.Paste in the Table you want to connect

3.Run the thunder icon

enter image description here

Distrustful answered 11/7, 2023 at 18:2 Comment(0)
R
0

This is because in your mysql2 version is outdated please update it to current LTS. I was also facing same issue then I just delete my node_modules file and package-lock.json file and change the version of my mysql2 version to current LTS version in my package.json file and again install all my files by running command npm install.

Royalroyalist answered 16/7, 2023 at 5:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.