gpt4 book ai didi

mysql - Node.js mySQL 无法创建连接

转载 作者:行者123 更新时间:2023-11-29 19:08:11 26 4
gpt4 key购买 nike

我刚刚开始,所以我对以上两者都不太了解。代码:

var mysql = require("mysql");
var express = require('express');
var app = express();


// First you need to create a connection to the db
var con = mysql.createConnection({
host: 'localhost',
user: 'root',
password: ''
});

con.connect(function(err){
if(err){
console.log('Error connecting to Db');
console.log(err);
return;
}
console.log('Connection established');
});

con.end(function(err) {
// The connection is terminated gracefully

尝试使用 node.js 运行它,但它说:

 Error connecting to Db
{ Error: connect ECONNREFUSED 127.0.0.1:3306
at Object.exports._errnoException (util.js:1018:11)
at exports._exceptionWithHostPort (util.js:1041:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
--------------------
at Protocol._enqueue (C:\Users\yeohc\Desktop\Newfolder\node_modules\mysql\lib\protocol\Protocol.js:141:48)
at Protocol.handshake (C:\Users\yeohc\Desktop\Newfolder\node_modules\mysql\lib\protocol\Protocol.js:52:41)
at Connection.connect (C:\Users\yeohc\Desktop\Newfolder\node_modules\mysql\lib\Connection.js:130:18)
at Object.<anonymous> (C:\Users\yeohc\Desktop\Newfolder\app.js:13:5)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true }

请帮助我并记住我才刚刚开始编程。感谢之前的帮助

最佳答案

我能够在我的机器上连接您的代码。

在您的场景中检查以下情况

1- Make sure your mysql server is up on port.

2- Check the password is required?

关于mysql - Node.js mySQL 无法创建连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43319556/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com