简体   繁体   English

Node JS和mysql连接不上(客户端不支持服务器请求的认证协议考虑升级mysql客户端)

[英]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 :这是我在命令提示符中收到的错误消息:

error image错误图像

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.

相关问题 Node.js和MySQL - 错误:1251 - 客户端不支持服务器请求的身份验证协议;考虑升级MySQL客户端 - Node.js & MySQL - Error: 1251 - Client does not support authentication protocol requested by server; consider upgrading MySQL client 运行问题 MySQL >8.0 sqlMessage:“ '客户端不支持服务器请求的认证协议; 考虑升级 MySQL 客户端',” - Problem running MySQL >8.0 sqlMessage:“ 'Client does not support authentication protocol requested by server; consider upgrading MySQL client',” MySQL 8.0 - 客户端不支持服务器请求的认证协议; 考虑升级 MySQL 客户端 - MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client ER_NOT_SUPPORTED_AUTH_MODE:客户端不支持服务器请求的认证协议; 考虑升级 MySQL 客户端 - ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client 客户端不支持服务器请求的身份验证协议; 考虑升级MySQL客户端 - Client does not support authentication protocol requested by server; consider upgrading MySQL client 错误:客户端不支持服务器请求的认证协议; 考虑升级 MySQL 客户端 - Error: Client does not support authentication protocol requested by server; consider upgrading MySQL client 客户端不支持服务器请求的身份验证协议,请考虑升级mysql客户端 - client does not support authentication protocol requested by server consider upgrading mysql client errno: 1251, sqlMessage: '客户端不支持服务器请求的认证协议; 考虑升级MySQL客户端 - errno: 1251, sqlMessage: 'Client does not support authentication protocol requested by server; consider upgrading MySQL client Docker Flyway MySQL 8:客户端不支持服务器请求的身份验证协议。 考虑升级MariaDB客户端 - Docker Flyway MySQL 8 : Client does not support authentication protocol requested by server. Consider upgrading MariaDB client 得到“客户端不支持服务器请求的认证协议; 考虑升级赛普拉斯中的 MySQL 客户端 - Getting “Client does not support authentication protocol requested by server; consider upgrading MySQL client” in Cypress
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM