13

Problem in mysql used in nodejs

const mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '123456789',
  database : 'userdata'
});

connection.connect(function(err) {
  if (err) {
    console.error('error connecting: ' + err);
    return;
  }

 console.log('connected as id ' + connection.threadId);
});

Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client

Kumar
  • 179
  • 1
  • 1
  • 14
  • 4
    Possible duplicate of [MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client](https://stackoverflow.com/questions/50093144/mysql-8-0-client-does-not-support-authentication-protocol-requested-by-server) – Prashant Gupta Oct 15 '18 at 11:46
  • Right, if you are using MySQL 8.0 series, check [this](https://stackoverflow.com/questions/50373427/node-js-cant-authenticate-to-mysql-8-0/50377944#50377944) as well. – ruiquelhas Oct 15 '18 at 14:39
  • Prashant Gupta -- for me it working thank you so much man.. – Kumar Oct 17 '18 at 05:57

3 Answers3

29

Had the same problem.

Install MySQL Workbench

Execute this query: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456789'; in MySQL Workbench

Head back over to Node.js and try running the file again. It should work, at least it did for me.

Margaret Smith
  • 322
  • 4
  • 4
8

Best Solution try this it will resolve:

You installed mysql server using "mysql installer"

1)open -> "mysql intsaller"

2)press reconfig mysql server

3)select left side "authentication method tab"

4)select radio button -->use legacy authentication method

5)now stop and restart the db 

Thanks!

tamilselvan s
  • 825
  • 2
  • 8
  • 13
2

In any database software (mysql workbench,datagrip, dbeaver...):

First run this query:

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

Second, if your problem still persists, run this query:

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password'; 

Combining and running these two queries separately worked for me with dockorize mysql.

buddemat
  • 3,262
  • 10
  • 15
  • 39
joah
  • 21
  • 2