Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node-mysql error: connect ECONNREFUSED

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'
});
like image 474
Skoua Avatar asked Apr 07 '14 23:04

Skoua


People also ask

How do I fix Econnrefused connect?

If that's the cause of the Error: Connect econnrefused – connection refused by server error, simply disable the firewall and anti-virus software on your computer and try to reconnect.

What does error connect Econnrefused mean?

ECONNREFUSED means no process is listening at the given address and port.

Could not connect server may not be running in MySQL workbench?

Here are some reasons the Can't connect to local MySQL server error might occur: mysqld is not running on the local host. Check your operating system's process list to ensure the mysqld process is present. You're running a MySQL server on Windows with many TCP/IP connections to it.


3 Answers

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'
});
like image 133
Zaman-A-Piri Pasa Avatar answered Oct 15 '22 21:10

Zaman-A-Piri Pasa


Should one still look for an answer:

Check your MySql server's configuration.

In my case running netstat -ln | grep mysql(as per Troubleshooting Problems Connecting to MySQL) revealed that my server is listening on the socket /tmp/mysql.sock so in createConnection's options I used socketPath: '/tmp/mysql.sock' instead of host: and port:

like image 36
Lucian Avatar answered Oct 15 '22 21:10

Lucian


This error has to do with the MySQL client not being able to connect to the host:port given. As defined in errno.h, ECONNREFUSED is an error that is thrown when a connection is refused - this is possibly caused by:

  • a firewall such as iptables blocking the port
  • no such process running on the port
  • the process is running on a different host
  • the host or port were incorrect

In my case, my MySQL server was not correctly bound to 0.0.0.0 for external connections, instead being bound to 127.0.0.1, as it is by default; however you can change this.

The error does not originate from the node-mysql package, as doubly shown by the stacktrace, which only shows errors from net.js.

like image 3
Brendan Avatar answered Oct 15 '22 22:10

Brendan