通过tunnel-ssh使用sequelize访问远程数据库时出错

时间:2019-02-20 08:53:09

标签: mysql node.js amazon-web-services sequelize.js ssh-tunnel

我在具有MySQL DB的AWS上有一个EC2 Ubuntu实例,可以通过HeidiSQL通过以下配置进行访问,并且一切正常。我的主要目标是通过sequelize连接到该数据库。我尝试使用npm软件包tunnel-ssh来做到这一点,但是不断出错。

设置

settings

SSH隧道设置

ssh tunnel settings

AWS EC2实例安全组设置:

enter image description here

enter image description here

错误消息:

{ SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306
    at Utils.Promise.tap.then.catch.err (G:\study\ssh-tunnel\sqlize-ssh\node_modules\sequelize\lib\dialects\mysql\connection-manager.js:139:19)
    at tryCatcher (G:\study\ssh-tunnel\sqlize-ssh\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (G:\study\ssh-tunnel\sqlize-ssh\node_modules\bluebird\js\release\promise.js:512:31)
    at Promise._settlePromise (G:\study\ssh-tunnel\sqlize-ssh\node_modules\bluebird\js\release\promise.js:569:18)
    at Promise._settlePromise0 (G:\study\ssh-tunnel\sqlize-ssh\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (G:\study\ssh-tunnel\sqlize-ssh\node_modules\bluebird\js\release\promise.js:690:18)
    at _drainQueueStep (G:\study\ssh-tunnel\sqlize-ssh\node_modules\bluebird\js\release\async.js:138:12)
    at _drainQueue (G:\study\ssh-tunnel\sqlize-ssh\node_modules\bluebird\js\release\async.js:131:9)
    at Async._drainQueues (G:\study\ssh-tunnel\sqlize-ssh\node_modules\bluebird\js\release\async.js:147:5)
    at Immediate.Async.drainQueues [as _onImmediate] (G:\study\ssh-tunnel\sqlize-ssh\node_modules\bluebird\js\release\async.js:17:14)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
  name: 'SequelizeConnectionRefusedError',
  parent:
   { Error: connect ECONNREFUSED 127.0.0.1:3306
       at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
     errno: 'ECONNREFUSED',
     code: 'ECONNREFUSED',
     syscall: 'connect',
     address: '127.0.0.1',
     port: 3306,
     fatal: true },
  original:
   { Error: connect ECONNREFUSED 127.0.0.1:3306
       at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
     errno: 'ECONNREFUSED',
     code: 'ECONNREFUSED',
     syscall: 'connect',
     address: '127.0.0.1',
     port: 3306,
     fatal: true } }

我使用的软件包:mysql2, sequelize, tunnel-ssh

我认为到目前为止,我已经能够打开ssh-tunnel并运行sequelize,但存在错误。这是我编写的代码:

const fs = require('fs');
const Sequelize = require("sequelize");
const tunnel = require('tunnel-ssh');
const ppk = fs.readFileSync('./sample.ppk', 'utf-8');

const sequelize = new Sequelize('test', 'login', 'password', {
  host: 'localhost',
  port: 3306,
  dialect: "mysql",
  pool: {
      max: 5,
      min: 0,
      acquire: 30000,
      idle: 10000
  }
}); 

const sshConfig = {
  user: 'ubuntu',
  host: 'sshServer',
  port: 22,
  dstHost: 'localhost',
  dstPort: 3306,
  localHost: 'localhost',
  localPort: 3307,
  privateKey: ppk
}

// initiate tunnel
tunnel(sshConfig, (error, server) => {
  if(error) {
    console.error(error);
  } else {
    console.log('server:', server);

    sequelize
        .authenticate()
        .then((db) => {
            console.log('CONNECTION ESTABLISHED! ', db);
        })
        .catch((err) => {
            console.error('UNABLE TO ESTABLISH CONNECTION: ', err);
        })
  }
})

2 个答案:

答案 0 :(得分:0)

对于mysql / aurora,如果您的脚本在该EC @上本地运行,则具有自定义IP地址,那么您将可以建立连接。

添加一个可以随处访问的规则,然后尝试一下。对于mysql

答案 1 :(得分:0)

通过完全不使用ssh-tunnel连接解决了该问题。我只为我的家庭IP打开mysql端口,并创建了一个授予所有权限的新用户。
修正了我的代码:添加了服务器的IP地址而不是本地主机。现在可以使用了。

const sequelize = new Sequelize('DBname', 'userName', 'password', {
  host: 'ec2-xx-xxx-xx-x.ap-xxxxxxxxxxxx.compute.amazonaws.com',
  port: 3306,
  dialect: "mysql",
});