节点JS和mysql未连接(客户端不支持服务器请求的身份验证协议,请考虑升级mysql客户端) [英] Node JS and mysql not connecting (client does not support authentication protocol requested by server consider upgrading mysql client)
问题描述
我试图将我的NodeJs应用程序与mysql数据库连接,但由于某种原因它没有连接。
I'm trying to connect my NodeJs application with a mysql database but its not connecting for some reason.
我确实像这样安装了mysql npm(键入在命令行中):
I did install the mysql npm like this (typed this in commandline):
npm install mysql
安装mysql npm后,我创建了一个index.js文件,并在该文件中尝试与数据库建立连接。
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.
这是我的index.js文件中与mysql建立连接的代码:
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));
});
然后我尝试运行代码,看看与mysql的连接是否有效。我在命令行中输入了以下内容:
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 :
有人知道我该如何解决该错误并在nodejs中获得与我的mysqldatabase的连接?
Does anyone know how I can fix this error and get a connection with my mysqldatabase in nodejs ?
感谢任何帮助
推荐答案
代替
mysqlConnection.connect((err) => {
使用
connection.connect((err) => {
if(!err)
console.log('Database is connected!');
else
console.log('Database not connected! : '+ JSON.stringify(err, undefined,2));
});
对于您的mysql错误:
For your mysql error:
在MYSQL Workbench中执行以下查询:
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.
这篇关于节点JS和mysql未连接(客户端不支持服务器请求的身份验证协议,请考虑升级mysql客户端)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!