Trouble connecting Node-mysql

别等时光非礼了梦想. 提交于 2019-12-07 10:26:38

问题


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);

回答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.



来源:https://stackoverflow.com/questions/15202575/trouble-connecting-node-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!