Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js MySQL - Error: connect ECONNREFUSED

I use Node.js server side. I tried my code on localhost and everything works fine. I bought a server and installed Apache and node.js on it and test my web application there. I correctly changed the MySQL connection configurations from localhost to the server configurations.

I test my web application with this configuration:

var mysql      = require('mysql'); var mysqlConnection; function new_mysqlConnection() {     mysqlConnection = mysql.createConnection({       host     : 'myurl.at',       user     : 'myusername',       database : 'mydatabase',       password : 'mypassword'     }); } 

I start the node.js server with:

$ node server.js 

When I load the page, it's correctly displayed, but when Node.js try to connect to the database I always got the following error:

Error: connect ECONNREFUSED     at errnoException (net.js:905:11)     at Object.afterConnect [as oncomplete] (net.js:896:19)     --------------------     at Protocol._enqueue (/var/www/node_modules/mysql/lib/protocol/Protocol.js:135:48)     at Protocol.handshake (/var/www/node_modules/mysql/lib/protocol/Protocol.js:52:41)     at Connection.connect (/var/www/node_modules/mysql/lib/Connection.js:119:18)     at reconnectDb (/var/www/server.js:319:18)     at app.get.email (/var/www/server.js:109:2)     at Layer.handle [as handle_request] (/var/www/node_modules/express/lib/router/layer.js:82:5)     at trim_prefix (/var/www/node_modules/express/lib/router/index.js:302:13)     at /var/www/node_modules/express/lib/router/index.js:270:7     at Function.proto.process_params (/var/www/node_modules/express/lib/router/index.js:321:12)     at next (/var/www/node_modules/express/lib/router/index.js:261:10) 
like image 925
Nico Avatar asked May 15 '15 18:05

Nico


People also ask

What is connect Econnrefused?

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

Could not connect server may not be running in MySQL?

normally means that there is no MySQL server running on the system or that you are using an incorrect Unix socket file name or TCP/IP port number when trying to connect to the server. You should also check that the TCP/IP port you are using has not been blocked by a firewall or port blocking service.


1 Answers

You need to add the value of socket path to the config object:

socketPath: '/var/run/mysqld/mysqld.sock' 

In MAMP, you go to http://localhost:8888/MAMP, and you find:

/Applications/MAMP/tmp/mysql/mysql.sock 

At the end you have:

var connection = mysql.createConnection({   host     : config.host,   user     : config.user,   password : config.pass,   database : config.db,   socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock' }); 
like image 195
tdebroc Avatar answered Sep 21 '22 20:09

tdebroc