4

Im trying to connect to my mysql database with node-mysql. When i try the following code, I get this error:

ERROR: Error: connect ETIMEDOUT

does anyone know whats wrong? The database is up and running btw.

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

var mysql = require('mysql');

var connection = mysql.createConnection({
    host: "correct_host",
    user: "correct_user",
    password: "correct_password",
    database: "correct_database",
    debug: true, 
});

connection.connect(function(err) {
    if (err) {
        console.log('ERROR: ' + err);
    } else {
        console.log('Connected to ' + server.hostname + ' (' + server.version + ')');
    };
});

connection.query('SELECT 1', function(err, rows) {
  // connected! (unless `err` is set)
});

connection.query('SELECT * FROM Effect', function(err, rows, fields) {
  if (err) console.log('ERROR: ' + err);

  for (x in rows) {
    console.log('Row: ', x);
  }
});

// Handle disconnect

function handleDisconnect(connection) {
  connection.on('error', function(err) {
    if (!err.fatal) {
      return;
    }

    if (err.code !== 'PROTOCOL_CONNECTION_LOST') {
      throw err;
    }

    console.log('Re-connecting lost connection: ' + err.stack);

    connection = mysql.createConnection(connection.config);
    handleDisconnect(connection);
    connection.connect();
  });
}

handleDisconnect(connection);
Thomas Ingham
  • 1,082
  • 8
  • 16
Sindresvends
  • 180
  • 1
  • 1
  • 10
  • 1
    I believe the problem is with your `host` or `database` parameter. You said the db is up and running. - Is it running on the same machine? Have you verified connecting to the database with this information in a different manner? – Jay Walker Mar 04 '13 at 13:46
  • yes, i can log in to the database using phpmyadmin to administrate the database – Sindresvends Mar 05 '13 at 08:40
  • 1
    It might be that your mysql database is only listening for connections on '127.0.0.1' or 'localhost'. You should try both to make sure. – Niek Schmoller Mar 12 '13 at 16:43
  • 1
    Can you post the contents of your /etc/my.cnf please? – Thomas Ingham May 01 '13 at 01:31
  • It seems likely that your mysql isn't accepting connections from remote hosts. This is done with the "skip-networking" setting in the cnf file; which you can comment out thus: # skip-networking - You will also need to make sure that the user connecting has a remote profile (user@'%') – Thomas Ingham May 01 '13 at 01:37
  • Try binding to 0.0.0.0 instead of 127.0.0.1 – JPR May 07 '13 at 07:03

1 Answers1

1

it's probably not node. if it is node, then check host: "correct_host" is in fact the correct host. Otherwise, try connecting to your mysql server from command line, outside of node.

dansch
  • 6,059
  • 4
  • 43
  • 59