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'
});
This error has to do with the MySQL client not being able to connect to the
host:port
given. As defined inerrno.h
, ECONNREFUSED is an error that is thrown when a connection is refused - this is possibly caused by:iptables
blocking the portIn my case, my MySQL server was not correctly bound to
0.0.0.0
for external connections, instead being bound to127.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 fromnet.js
.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.
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
Use the command: yarn run start, and DO NOT use: cd /folderX && yarn run start
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 increateConnection
's options I usedsocketPath: '/tmp/mysql.sock'
instead ofhost:
andport:
Try using mysql socket: