无法连接Node-mysql

时间:2013-03-04 13:23:29

标签: javascript mysql configuration node-mysql

我试图用node-mysql连接到我的mysql数据库。当我尝试以下代码时,我收到此错误:

错误:错误:连接ETIMEDOUT

有谁知道什么是错的?数据库启动并运行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);

1 个答案:

答案 0 :(得分:1)

它可能不是节点。如果它是节点,那么检查host: "correct_host"实际上是正确的主机。否则,尝试从节点外部的命令行连接到mysql服务器。