Node JS and mysql not connecting (client does not support authentication protocol requested by server consider upgrading mysql client)

寵の児 提交于 2019-12-24 00:38:53

问题


I'm trying to connect my NodeJs application with a mysql database but its not connecting for some reason.

I did install the mysql npm like this (typed this in commandline):

npm install mysql

After I installed the mysql npm I made an index.js file and in that file I tried to make the connection with my database.

This is the code inside my index.js file to make a connection with mysql:

//Gebruik mysql npm module voor de mysql database
const mysql      = require('mysql');

//Geef connectie details aan
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    port     : '3306',
    password : 'tombal10',
    database : 'fys'
});


connection.connect((err) => {
//Als er geen error is print dan 'Database is geconnect!'
    if(!err)
        console.log('Database is geconnect!');
//Als er wel een error is print dan 'Database connectie niet gelukt!'
    else
        console.log('Database connectie niet gelukt!  : '+ JSON.stringify(err, undefined,2));
});

Then I tried to run the code and see if the connection with mysql worked. I typed this in the command line:

node index.js

This is the error message I got in the command prompt :

error image

Does anyone know how I can fix this error and get a connection with my mysqldatabase in nodejs ?

Any kind of help is appreciated


回答1:


Instead of

mysqlConnection.connect((err) => {

use

connection.connect((err) => {

if(!err)
    console.log('Database is connected!');
else
    console.log('Database not connected! : '+ JSON.stringify(err, undefined,2));
});

[Here the is tutorials reference for the code]1

For your mysql error:

Execute the following query in MYSQL Workbench:

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

Try connecting using node after you do so.




回答2:


ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourRootPassword';
-- or
CREATE USER 'foo'@'%' IDENTIFIED WITH mysql_native_password BY 'bar';
-- then
FLUSH PRIVILEGES;


来源:https://stackoverflow.com/questions/53283060/node-js-and-mysql-not-connecting-client-does-not-support-authentication-protoco

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!