13

I have the next code in a js file:

var mysql = require('mysql');
var TEST_DATABASE = 'nodejs_mysql_test';
var TEST_TABLE = 'test';
var client = mysql.createClient({
  user: 'root',
  password: 'root',
});

client.query('CREATE DATABASE '+TEST_DATABASE, function(err) {
  if (err && err.number != mysql.ERROR_DB_CREATE_EXISTS) {
    throw err;
  }
});

But 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:632:11)
    at Object.afterConnect [as oncomplete] (net.js:623:18)

As I understand it, this is a connection problem - but how do I solve it?

( I'm working on windows 7)

Thanks!!

kakush
  • 3,334
  • 14
  • 47
  • 68
  • It would seem that for some reason the page thinks the database is up and running. It is up and running on localhost, right? – Joachim Isaksson Jan 11 '12 at 19:26
  • 4
    obvious question: is mysql running on the same machine, on port 3306? can you `telnet localhost 3306`? – seppo0010 Jan 11 '12 at 19:26
  • when I write in CMD the line telnet localhost 3306 I get: 'telnet' is not recognized as an internal or external command, operable program or batch file. Is that what you mean? – kakush Jan 11 '12 at 19:39
  • @user1087995: what seppo0010 is getting at is are you absolutely sure there is a MySQL server running on the local machine and listening on port 3306, per the connection settings that you are using? This error indicates that you are trying to connect to a server which is not listening. – maerics Jan 11 '12 at 20:58
  • Even if it's running on the local machine, you need to be sure that MySQL is listening on localhost/127.0.0.1 and not just the external IP. – Ryan Olds Jan 11 '12 at 22:41

3 Answers3

19

I know two ways to solve it:

  1. In mysql.conf, comment skip-networking.

  2. Try to set the socket like this:

    var client = mysql.createClient({
    user: uuuu,
    password: pppp,
    host: '127.0.0.1',
    port: '3306',
    _socket: '/var/run/mysqld/mysqld.sock',});
    
limon
  • 907
  • 1
  • 9
  • 19
  • 1
    Thanks, commenting skip-networking worked for me, but the key did not, that's weird. On production I'd rather not have that option commented! – nak Jun 30 '12 at 07:29
  • This was a rejected edit by an anonymous user: I had the same problem, so i just inserted how i solved my problem. Make sure you have the correct bind-address in the mysql config file (`/etc/mysql/ my.cnf` or `mysql.conf`). Below you have an example of what you should write in this case: `bind-address = 127.0.0.1` – Matsemann Dec 09 '13 at 18:50
  • I don't know why but only this worked for me: http://stackoverflow.com/a/20486467/2413469 – Alex Jan 08 '14 at 10:28
  • 9
    In version 2.1.0 you should use ```socketPath``` instead of ```_socket``` – gorodezkiy Feb 21 '14 at 14:02
0

I got this error when MySQL Server was not running.

I changed my configuration via Initialize Database in MySQL.PrefPane, the System Preferences tool for MySQL on OS X - to Use Legacy Password Encryption - to fix ER_NOT_SUPPORTED_AUTH_MODE. This config change stopped MySQL Server and then I got ECONNREFUSED when I tried to connect to MySQL from node.js.

Fixed by restarting MySQL Server from the MySQL System Preferences tool.

Dave Deasy
  • 316
  • 2
  • 11
0

Try to fix your defined port in mysql and your Node.js script. You can define the mysqld port in the *.cnf file inside the mysql directory, and you can define that port when you connect to MySQL in your Node.js script.

Something like this in the cnf file

port = 3306
ow3n
  • 5,974
  • 4
  • 53
  • 51
b.andarzian
  • 53
  • 1
  • 1
  • 7