节点mysql connection.connect不工作

节点mysql connection.connect不工作,mysql,node.js,Mysql,Node.js,节点mysql有一个奇怪的问题。我在本地机器上用标准设置运行mysql容器(docker)。我能够通过mysql.createConnection,但我的代码在connection.connect时总是失败。容器上的MySQL错误日志显示没有尝试/失败的连接,我可以通过Workbench进行连接。有没有人有过通过node连接MySQL的经历 router.get('/page', function(req,res){ const msg = 'My Query from config, te

节点mysql有一个奇怪的问题。我在本地机器上用标准设置运行mysql容器(docker)。我能够通过mysql.createConnection,但我的代码在connection.connect时总是失败。容器上的MySQL错误日志显示没有尝试/失败的连接,我可以通过Workbench进行连接。有没有人有过通过node连接MySQL的经历

router.get('/page', function(req,res){
  const msg = 'My Query from config, tested and confirmed on Workbench';

  var connection = mysql.createConnection({
    host     : '127.0.0.1',
    user     : 'user',
    password : 'pw',
    database : 'db',
    port     : 3306,
    debug    : true
  });

  console.log('Connect to server', connection.state); //Doesn't get past this guy

  connection.connect(function(err) {
    if (err) {
      console.log('error connecting: ' + err.stack);
      return err;
    }
    else {
      console.log('connected as id ' + connection.threadId);
    }
  });

  middleware.logger.debug('Make Query');
  connection.query(msg, function(err, rows, fields) {
    if (!err) {
        console.log('The solution is: ', rows);
        res.json('We got some rows!');
      }
    else {
      console.log('Error while performing Query.', err);
      res.json(err);
      }
  });
  console.log('End Connection');
  connection.end();
});
另外,应该注意:无论docker容器是否正在运行,我都会收到相同的错误


编辑:好的,想修正我的问题,这实际上是express router的问题。上面的扩展代码

err.stack是否显示任何错误?既然您说可以连接Workbench,我假设您已经在docker容器上正确地公开了端口?该语句不会打印。运行的最后一个打印语句是“连接到服务器”。是的,端口在3306上公开,我将绑定地址设置为允许通信。作为一个快速测试,您是否可以打开更多的错误报告或关闭所有防火墙?err.stack是否显示任何错误?既然您说可以连接Workbench,我假设您已经在docker容器上正确地公开了端口?该语句不会打印。运行的最后一个打印语句是“连接到服务器”。是的,端口在3306上公开,我将绑定地址设置为允许通信。作为一个快速测试,您是否可以打开更多错误报告或关闭所有防火墙?