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!!

5
  • It would seem that for some reason the page thinks the database is up and running. It is up and running on localhost, right? Commented Jan 11, 2012 at 19:26
  • 4
    obvious question: is mysql running on the same machine, on port 3306? can you telnet localhost 3306? Commented Jan 11, 2012 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? Commented Jan 11, 2012 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. Commented Jan 11, 2012 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. Commented Jan 11, 2012 at 22:41

3 Answers 3

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',});
    
Sign up to request clarification or add additional context in comments.

4 Comments

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!
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
I don't know why but only this worked for me: stackoverflow.com/a/20486467/2413469
In version 2.1.0 you should use socketPath instead of _socket
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.