New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: connect ECONNREFUSED #874

Closed
WellingGuzman opened this Issue Jul 19, 2014 · 5 comments

Comments

Projects
None yet
3 participants
@WellingGuzman

WellingGuzman commented Jul 19, 2014

Error: connect ECONNREFUSED is the error I get everything I try to connect to a mysql database using this code:

var mysql = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '123'
});

connection.connect(function(err) {
  if (err) {
    console.error('error connecting: ' + err.stack);
    return;
  }

  console.log('connected as id ' + connection.threadId);
});

The complete error output is:

error connecting: Error: connect ECONNREFUSED
    at errnoException (net.js:901:11)
    at Object.afterConnect [as oncomplete] (net.js:892:19)
    --------------------
    at Protocol._enqueue (/Users/wellingguzman/GitHub/Beers/beers/node_modules/mysql/lib/protocol/Protocol.js:135:48)
    at Protocol.handshake (/Users/wellingguzman/GitHub/Beers/beers/node_modules/mysql/lib/protocol/Protocol.js:52:41)
    at Connection.connect (/Users/wellingguzman/GitHub/Beers/beers/node_modules/mysql/lib/Connection.js:108:18)
    at Object.<anonymous> (/Users/wellingguzman/GitHub/Beers/beers/server.js:16:12)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)

This time is installed using:

$ npm install felixge/node-mysql

so the version is not specified.

Thanks

@dougwilson dougwilson added the question label Jul 19, 2014

@dougwilson

This comment has been minimized.

Show comment
Hide comment
@dougwilson

dougwilson Jul 19, 2014

Member

It is because your connection was refused trying to establish a TCP connection to port 3306 on localhost. The following is the mysql command you can use to verify connectivity (you must run it exactly as specified here):

$ mysql --protocol=TCP -u root -p

Your MySQL server is likely not setup to listen on a TCP socket, so you probably want to check your MySQL server configuration.

Member

dougwilson commented Jul 19, 2014

It is because your connection was refused trying to establish a TCP connection to port 3306 on localhost. The following is the mysql command you can use to verify connectivity (you must run it exactly as specified here):

$ mysql --protocol=TCP -u root -p

Your MySQL server is likely not setup to listen on a TCP socket, so you probably want to check your MySQL server configuration.

@dougwilson dougwilson closed this Jul 19, 2014

@WellingGuzman

This comment has been minimized.

Show comment
Hide comment
@WellingGuzman

WellingGuzman Jul 19, 2014

oh thanks, this still getting me error.

ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (61)

But at least I know it is a mysql thing in my end.

WellingGuzman commented Jul 19, 2014

oh thanks, this still getting me error.

ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (61)

But at least I know it is a mysql thing in my end.

@dougwilson

This comment has been minimized.

Show comment
Hide comment
@dougwilson

dougwilson Jul 19, 2014

Member

oh thanks, this still getting me error.

Right, I figured it would still give you an error, because the error is just a bad configuration of the MySQL server. I just wanted to illustrate it is not an issue with this library.

You can always connect to your MySQL server using a UNIX socket instead of TCP, though. Run the following command on the server:

$ mysql_config --socket
/tmp/mysql.sock

Then take the value it gave you (/tmp/mysql.sock in this example) and give it to the socketPath option for this module:

var connection = mysql.createConnection({
  socketPath : '/tmp/mysql.sock',
  user       : 'root',
  password   : '123'
});
Member

dougwilson commented Jul 19, 2014

oh thanks, this still getting me error.

Right, I figured it would still give you an error, because the error is just a bad configuration of the MySQL server. I just wanted to illustrate it is not an issue with this library.

You can always connect to your MySQL server using a UNIX socket instead of TCP, though. Run the following command on the server:

$ mysql_config --socket
/tmp/mysql.sock

Then take the value it gave you (/tmp/mysql.sock in this example) and give it to the socketPath option for this module:

var connection = mysql.createConnection({
  socketPath : '/tmp/mysql.sock',
  user       : 'root',
  password   : '123'
});
@WellingGuzman

This comment has been minimized.

Show comment
Hide comment
@WellingGuzman

WellingGuzman Jul 19, 2014

Perfect! Thanks @dougwilson 👍
it's weird, but also the solution is to uncheck on MAMP the option Allow local access only.

So to make myself sure of it, I removed the socketPath option /Applications/MAMP/tmp/mysql/mysql.sock) and still get connection.

But I'd stick with your solution seems like the right one.

Thanks @dougwilson everything it's working now.

WellingGuzman commented Jul 19, 2014

Perfect! Thanks @dougwilson 👍
it's weird, but also the solution is to uncheck on MAMP the option Allow local access only.

So to make myself sure of it, I removed the socketPath option /Applications/MAMP/tmp/mysql/mysql.sock) and still get connection.

But I'd stick with your solution seems like the right one.

Thanks @dougwilson everything it's working now.

@guday

This comment has been minimized.

Show comment
Hide comment
@guday

guday Nov 14, 2014

I get the same err,it toke me half day to google,thank for all this answers.

guday commented Nov 14, 2014

I get the same err,it toke me half day to google,thank for all this answers.

@mysqljs mysqljs locked and limited conversation to collaborators Nov 14, 2014

@dougwilson dougwilson self-assigned this Jan 19, 2015

@dougwilson dougwilson added the FAQ label Jan 19, 2015

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.