Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js mysql error: ECONNREFUSED

Why can't I connect to the mysql server?

On the same server an Apache/PHP server is running and it connects without problems!?

var mysql_link = {
    host : 'localhost',
    port : 3308,
    database: 'nodetest',
    user : 'root',
    password : 'xxx'
};

var connection = mysql.createConnection(mysql_link);

connection.connect(function(err){
    console.log(err);
    if(err != null){
        response.write('Error connecting to mysql:' + err+'\n');
    }
});

connection.end();

error

{ [Error: connect ECONNREFUSED]
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  fatal: true }

update

root@dyntest-amd-6000-8gb /var/www/node/dyntest # ps ax | grep mysqld
 7928 pts/0    S+     0:00 grep mysqld
28942 ?        S      0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/run/mysqld/mysqld.pid
29800 ?        Sl    17:31 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/var/lib/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/lib/mysql/mysql-error.log --open-files-limit=65535 --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306
like image 807
clarkk Avatar asked Jan 18 '14 16:01

clarkk


People also ask

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.

How long does it take for nodejs error connect econnrefused to work?

Here it's my success story with incredible persistant Node.js error connect ECONNREFUSED . Day 1. I run this code, and should work fine. Usual errors, maybe 3-4 minutes. I changed host: 'localhost' to host : '127.0.0.1', port: 8080, or maybe 8000, oh yes, 3306 this is.

What does connection refused mean in MySQL?

I believe connection refused is a tcp/ip error message, rather than something from MySQL which suggests that it is either not running or is running on another port or with sockets. Could you try telnet'ing to port 3308? To see if the server is running on that port? Show activity on this post. For anyone else having this problem and is running mamp.

What does the error econnrefused 127 3306 mean?

The error ECONNREFUSED 127.0.0.1:3306 is being raised by Node.js itself, and not this module. This module passes the error through, though, from Node.js. Basically, this module is asking Node.js to create a TCP connection to 127.0.0.1 , port 3306 , but for whatever reason, Node.js cannot do this and raises the ECONNREFUSED error.

Why can't I connect to my MySQL database?

If this has worked before, my first guess would be that you've already got a copy of your node.js script running in the background which is holding the connection. I believe connection refused is a tcp/ip error message, rather than something from MySQL which suggests that it is either not running or is running on another port or with sockets.


4 Answers

I know this question has been answered, but for me the problem was that the mysql server listens on a Unix socket not on a tcp socket. So the solution was to add:

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

to the connection options.

like image 153
Victor Dodon Avatar answered Oct 12 '22 15:10

Victor Dodon


If this has worked before, my first guess would be that you've already got a copy of your node.js script running in the background which is holding the connection.

I believe connection refused is a tcp/ip error message, rather than something from MySQL which suggests that it is either not running or is running on another port or with sockets.

Could you try telnet'ing to port 3308? To see if the server is running on that port?

telnet localhost 3308

Can you also try:

mysql -hlocalhost -uroot -pxxx
like image 41
Pez Cuckow Avatar answered Oct 12 '22 16:10

Pez Cuckow


Overview

For anyone else having this problem and is running mamp. I suspected the problem had to do with the network and not MySQL or Node.js.

Solution

If you open MAMP and click MySQL in the left navigation panel it will pull up the MySQL options page. In the center of the page you will see a checkbox that says,

"Allow network access to MySQL".

Check this box and then restart your MAMP. At this point you can now test your connection to MySQL with telnet or a node.js script.

Hint

Remember you can check which port your MySQL is running on by opening MAMP and clicking the ports link on the left navigation panel.

Visual Aid

enter image description here

like image 10
wuno Avatar answered Oct 12 '22 15:10

wuno


For some very odd reason, my computer only allowed me to have port 3306 as default port for my connection in order for it to work.

Screenshot

like image 3
dhan30 Avatar answered Oct 12 '22 14:10

dhan30