[英]Node JS and mysql not connecting (client does not support authentication protocol requested by server consider upgrading mysql client)
I'm trying to connect my NodeJs application with a mysql database but its not connecting for some reason.我正在尝试将我的 NodeJs 应用程序与 mysql 数据库连接,但由于某种原因它没有连接。
I did install the mysql npm like this (typed this in commandline):我确实像这样安装了 mysql npm(在命令行中输入):
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.安装 mysql npm 后,我创建了一个 index.js 文件,并在该文件中尝试与我的数据库建立连接。
This is the code inside my index.js file to make a connection with mysql:这是我的 index.js 文件中与 mysql 建立连接的代码:
const mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
port : '3306',
password : 'test12310',
database : 'fys'
});
connection.connect((err) => {
if(!err)
console.log('Database is geconnect!');
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.然后我尝试运行代码,看看与 mysql 的连接是否有效。 I typed this in the command line:我在命令行中输入:
node index.js
This is the error message I got in the command prompt :这是我在命令提示符中收到的错误消息:
Does anyone know how I can fix this error and get a connection with my mysqldatabase in nodejs ?有谁知道我如何修复此错误并在 nodejs 中与我的 mysqldatabase 建立连接?
Any kind of help is appreciated任何形式的帮助表示赞赏
Use the new mysql2 library instead of mysql in order to use the new authentication method of MySQL 8.0使用新的mysql2库代替mysql以使用 MySQL 8.0 的新身份验证方法
~ Tested in node version 13.6.0 and MySQL 8.0.21 ~ 在节点版本 13.6.0 和 MySQL 8.0.21 中测试
I was having the same problem with mysql
library and I fixed that error by changing to mysql2
Library I recommend you to install mysql2
我在使用mysql
库时遇到了同样的问题,我通过更改为mysql2
库修复了该错误我建议您安装mysql2
Run跑
npm install mysql2
Then然后
import mysql from "mysql2";
OR或者
const mysql = require("mysql2")
I hope this will work for you!!我希望这对你有用!! Good Luck祝你好运
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 [这里是代码的教程参考] 1
For your mysql error:对于您的 mysql 错误:
Execute the following query in MYSQL Workbench:在 MYSQL Workbench 中执行以下查询:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'
Try connecting using node after you do so.这样做后尝试使用节点连接。
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.