我在AWS上有一个带有MySQL DB的EC2 Ubuntu实例,我可以通过HeidiSQL使用以下配置访问它,一切正常。我的主要目标是通过sequelize
连接到这个数据库。我试过用npm包tunnel-ssh
做这件事,但经常出错。
设置
SSH隧道设置
AWS EC2实例安全组设置:
错误信息:
{ 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);
})
}
})
对于mysql / aurora有自定义IP地址,如果您的脚本在EC @本地运行,那么您将能够建立连接。
添加一个规则随处访问并尝试。对于mysql
通过不使用ssh-tunnel连接解决了该问题。我只为我的家庭ip打开了mysql端口,并创建了一个具有所有权限的新用户。 修正了我的代码:添加了服务器的IP地址而不是localhost。它现在有效。
const sequelize = new Sequelize('DBname', 'userName', 'password', {
host: 'ec2-xx-xxx-xx-x.ap-xxxxxxxxxxxx.compute.amazonaws.com',
port: 3306,
dialect: "mysql",
});