gpt4 book ai didi

javascript - 错误 : connect ECONNREFUSED Node. js、Heroku、Mysql

转载 作者:太空宇宙 更新时间:2023-11-03 23:50:17 24 4
gpt4 key购买 nike

我收到此错误 -> 错误:连接 ECONNREFUSED但只有当我取消注释这些行时:

db.connect((err) => {
if(err){
throw err;
}
console.log("---MYSQL CONNECTED---");
});

这在本地主机中完美运行,但是当我将其上传到heroku时,这是一个 fatal error ,我不明白为什么。这里的类似问题都没有帮助。

完整 Node 代码如下:

const express = require ('express');
const socketio = require('socket.io');
const mysql = require('mysql');

//SETUP++++++
var app = express();
var server = require('http').Server(app);


app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html')
} );

app.use('/assets', express.static(__dirname + '/assets'));

var port = process.env.PORT || 3000;

server.listen(port);
console.log("---SERVER RUNNING---");

var io = require('socket.io') (server, {});
//SETUP------

//CONNECT MYSQL
var db = mysql.createConnection({
server : 'server',
user : 'root',
password : 'password',
database : 'database',
_socket : 'socketpath/mysql.sock'
});

db.connect((err) => {
if(err){
throw err;
}
console.log("---MYSQL CONNECTED---");
});

//GLOBALS++
var SOCKET_LIST = {};

//GLOBALS--

db.query("SELECT * FROM players", function (err, results){ //, fields) {
if (err) throw err;
console.log(results);
});

//SOCKET CONNECTED
io.sockets.on('connection', function(socket){//SOCKETS++++++
SOCKET_LIST[socket.id] = socket;

});

完整错误日志如下:

2020-01-13T18:57:47.946330+00:00 heroku[web.1]: State changed from starting to crashed
2020-01-13T18:57:47.549194+00:00 app[web.1]:
2020-01-13T18:57:47.840420+00:00 app[web.1]: /app/server.js:35
2020-01-13T18:57:47.840506+00:00 app[web.1]: throw err;
2020-01-13T18:57:47.840508+00:00 app[web.1]: ^
2020-01-13T18:57:47.840509+00:00 app[web.1]:
2020-01-13T18:57:47.840511+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:3306
2020-01-13T18:57:47.840512+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1134:16)
2020-01-13T18:57:47.840514+00:00 app[web.1]: --------------------
2020-01-13T18:57:47.840515+00:00 app[web.1]: at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
2020-01-13T18:57:47.840516+00:00 app[web.1]: at Protocol.handshake (/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
2020-01-13T18:57:47.840517+00:00 app[web.1]: at Connection.connect (/app/node_modules/mysql/lib/Connection.js:119:18)
2020-01-13T18:57:47.840519+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:33:4)
2020-01-13T18:57:47.840520+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:955:30)
2020-01-13T18:57:47.840522+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
2020-01-13T18:57:47.840523+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:811:32)
2020-01-13T18:57:47.840525+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:723:14)
2020-01-13T18:57:47.840526+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
2020-01-13T18:57:47.840527+00:00 app[web.1]: at internal/main/run_main_module.js:17:11 {
2020-01-13T18:57:47.840528+00:00 app[web.1]: errno: 'ECONNREFUSED',
2020-01-13T18:57:47.840529+00:00 app[web.1]: code: 'ECONNREFUSED',
2020-01-13T18:57:47.840530+00:00 app[web.1]: syscall: 'connect',
2020-01-13T18:57:47.840531+00:00 app[web.1]: address: '127.0.0.1',
2020-01-13T18:57:47.840532+00:00 app[web.1]: port: 3306,
2020-01-13T18:57:47.840533+00:00 app[web.1]: fatal: true
2020-01-13T18:57:47.840534+00:00 app[web.1]: }
2020-01-13T18:57:47.854933+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-01-13T18:57:47.855243+00:00 app[web.1]: npm ERR! errno 1
2020-01-13T18:57:47.856649+00:00 app[web.1]: npm ERR! adise19_tictactoe@1.0.0 start: `node server.js`
2020-01-13T18:57:47.856764+00:00 app[web.1]: npm ERR! Exit status 1
2020-01-13T18:57:47.856964+00:00 app[web.1]: npm ERR!
2020-01-13T18:57:47.857113+00:00 app[web.1]: npm ERR! Failed at the adise19_tictactoe@1.0.0 start script.
2020-01-13T18:57:47.857218+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-01-13T18:57:47.864372+00:00 app[web.1]:
2020-01-13T18:57:47.864551+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-01-13T18:57:47.864676+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2020-01-13T18_57_47_858Z-debug.log
2020-01-13T18:57:47.549213+00:00 app[web.1]: > adise19_tictactoe@1.0.0 start /app
2020-01-13T18:57:47.549214+00:00 app[web.1]: > node server.js
2020-01-13T18:57:47.549215+00:00 app[web.1]:
2020-01-13T18:57:47.733401+00:00 app[web.1]: ---SERVER RUNNING---
2020-01-13T18:57:47.936023+00:00 heroku[web.1]: Process exited with status 1
Disconnected from log stream. There may be events happening that you do not see here! Attempting to reconnect...
2020-01-13T18:57:47.855243+00:00 app[web.1]: npm ERR! errno 1
2020-01-13T18:57:47.856649+00:00 app[web.1]: npm ERR! adise19_tictactoe@1.0.0 start: `node server.js`
2020-01-13T18:57:47.856764+00:00 app[web.1]: npm ERR! Exit status 1
2020-01-13T18:57:47.856964+00:00 app[web.1]: npm ERR!
2020-01-13T18:57:47.857113+00:00 app[web.1]: npm ERR! Failed at the adise19_tictactoe@1.0.0 start script.
2020-01-13T18:57:47.857218+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-01-13T18:57:47.864372+00:00 app[web.1]:
2020-01-13T18:57:47.864551+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-01-13T18:57:47.864676+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2020-01-13T18_57_47_858Z-debug.log
2020-01-13T18:57:47.936023+00:00 heroku[web.1]: Process exited with status 1
2020-01-13T19:09:16.741219+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=adise19-tictactoe.herokuapp.com request_id=0f23b6fc-6cdc-40eb-811e-f73e29da8a4f fwd="2.85.238.198" dyno= connect= service= status=503 bytes= protocol=https
2020-01-13T19:09:18.440439+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=adise19-tictactoe.herokuapp.com request_id=18cf9049-2d07-4ee2-8f26-9ed44cb5fd2b fwd="2.85.238.198" dyno= connect= service= status=503 bytes= protocol=https

最佳答案

不知道

但据我记得在 Heroku 中你必须添加 ~addons 我认为它们被称为。

其中一个插件是数据库。

在那里获取数据库有很多限制。

我确信只有这样他们才会在您的托管应用程序的虚拟服务器的防火墙中打开一个端口供您使用。

好吧..当您添加数据库插件时 - 那么您将获得必须输入的凭据作为 mysql 连接的选项。

address: '<heroku-will-give-you-this-and-other-info>',
port: <heroku-will-provide>

但总的来说,我强烈建议您切换到 VPS 来学习服务器端。 Heroku 是一场噩梦,你什么也学不到。在 vultr.com 寻找他们的免费月份优惠,然后就可以开始了。

关于javascript - 错误 : connect ECONNREFUSED Node. js、Heroku、Mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59723066/

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