连接节点mysql的麻烦 [英] Trouble connecting Node-mysql

查看:118
本文介绍了连接节点mysql的麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图连接到我的mysql数据库与node-mysql。当我尝试下面的代码,我得到这个错误:

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

错误:错误:连接ETIMEDOUT

ERROR: Error: connect ETIMEDOUT

知道什么错了?数据库已启动并正在运行btw。

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


推荐答案

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

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.

这篇关于连接节点mysql的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆