node-mysql error: connect ECONNREFUSED

Skoua picture Skoua · Apr 8, 2014 · Viewed 30.6k times · Source

I'm trying to setup a remote connection between my database server and a client node app using node-mysql.

When I try to connect to the remote db, I get this error:

node.js:201
    throw e; // process.nextTick error, or 'error' event on first tick
          ^
Error: connect ECONNREFUSED
    at errnoException (net.js:646:11)
    at Object.afterConnect [as oncomplete] (net.js:637:18)

Connecting to a local db works ok (with the socketPort parameter).

I can connect to this remote db with PHP from my computer localhost as well as another server I own so I don't think there's something wrong with mysql conf.

For info, nodejs is running with nginx and I've setup a proxy to make node work on port 80, maybe this is the issue?

How can I check that?

Thanks.

EDIT

Here's my code, just in case:

var express = require('express');
var mysql = require('mysql');
var app = express();
var connection = mysql.createConnection({
    debug: false,
    host: '12.34.56.67',
    user: 'user',
    password: 'pass'
});

Answer

Zaman-A-Piri Pasa picture Zaman-A-Piri Pasa · Jun 9, 2014

Try using mysql socket:

var connection = mysql.createConnection({
    user: 'user',
    password: 'pass',
    socketPath: 'mysql-socket-path', /*example: /Applications/MAMP/tmp/mysql/mysql.sock*/
    database: 'dbname'
});