8

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'
});
Skoua
  • 3,373
  • 3
  • 38
  • 51
  • Can you get the error code that mysql sends to node-mysql ? – Erico Apr 07 '14 at 23:37
  • Yep it's: { [Error: connect ECONNREFUSED] code: 'ECONNREFUSED', errno: 'ECONNREFUSED', syscall: 'connect', fatal: true } – Skoua Apr 08 '14 at 15:31
  • I believe the plugin is not showing the errors properly. I'd send this to the plugin developers and see if they can show the Mysql error code and message. ECONNREFUSED doesn't say much, it could be several things. – Erico Apr 08 '14 at 20:20
  • I just did that, thanks for you help. I'll let you know if the plugin devs find something. – Skoua Apr 08 '14 at 23:20

7 Answers7

20

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'
});
9

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:

Lucian
  • 432
  • 1
  • 4
  • 9
3

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.

Brendan
  • 2,777
  • 1
  • 18
  • 33
  • 1
    Firewalls cause connect timeouts. There was a firewall that issued refusals twenty years ago but it was rapidly recognized that is was a security breach, as it reveals the existence of the host and port. Your fourth point encapsulates both your second and third points. – user207421 Apr 29 '17 at 02:22
2

I added port:3306, that solved the issue .

var connection = mysql.createConnection
    ({
        user: 'root',
        password: 'root',
        server: 'localhost',
    port:3306,
        database: 'abcd',
        insecureAuth: true,
        dialect: 'mysql',   
        multipleStatements: true,
        pool: {
            max: 5,
            min: 0,
            acquire: 30000,
            idle: 10000
        }
    });

    connection.connect();
arupjbasu
  • 351
  • 3
  • 4
1

Ok so I checked with node-mysql dev and it seems that it's not a node-mysql bug, but it was hard to investigate more.

Anyway, I found this lib which works, so I'll go with it.

https://github.com/mariano/node-db-mysql

Skoua
  • 3,373
  • 3
  • 38
  • 51
  • I was having this exact problem and never managed to solve it. Switching to db-mysql for db stuff solved it. – tripRev Apr 13 '14 at 02:39
  • 1
    Looks like node-mysql latest version fixed the issue. At least it works for me with v2.2.0 – Skoua May 14 '14 at 23:16
0

i had the same problem. solved it, by set the right port in the createConnection function options.

get your port by using following command: netstat -tlnp

search for program name mysqld or just named.

Marcel Ennix
  • 1,328
  • 1
  • 12
  • 16
0

Use the command: yarn run start, and DO NOT use: cd /folderX && yarn run start

Russo
  • 2,186
  • 2
  • 26
  • 42