Nick
Nick

Reputation: 77

Error: connect ECONNREFUSED Node.js,Heroku,Mysql

I get this error -> Error: connect ECONNREFUSED but only when i uncomment these lines:

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

This runs perfectly in localhost but when i upload it to heroku its a fatal error and i dont get why. None of the similar questions here helped.

Full node code below:

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;

});

Full error log below:

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! [email protected] 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 [email protected] 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]: > [email protected] 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! [email protected] 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 [email protected] 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

Upvotes: 0

Views: 2835

Answers (1)

Nils Riga
Nils Riga

Reputation: 110

IDK

But as I remember in heroku you had to add ~addons I think they were called.

One of the addons is a database.

There were a bunch of limitations for getting a database there.

I'm kind of sure that only then they open a port on their side in the firewall of your hosted app's virtual server for you to use.

Well.. and when you do add a database addon - then you will get credentials that you will have to input as options for your mysql connection.

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

But in general I strongly advise you to switch to a VPS for learning server side. Heroku is a nightmare and you don't learn anything. At vultr.com look for their free month offer and your're good to go.

Upvotes: 1

Related Questions