8
votes

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

3
Right, if you are using MySQL 8.0 series, check this as well. - ruiquelhas
Prashant Gupta -- for me it working thank you so much man.. - Kumar

3 Answers

20
votes

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.

7
votes

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!

1
votes

in any database software mysql workbench,datagrip, dbeaver...

1st run this query:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

=---->if still your problem persist

2nd run this query:
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password';

combine and run this 2 queries seperated worked for me with dockorize mysql