通过 tunnel-ssh 使用 sequelize 访问远程数据库时出错
Error accessing remote DB with sequelize via tunnel-ssh
我在 AWS 上有一个带有 MySQL 数据库的 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 如果您的脚本 运行 在该 EC@ 本地,那么您将能够建立连接。
添加一条随处访问的规则并尝试一下。 mysql
问题已通过完全不使用 ssh 隧道连接得到解决。我只为我的家庭 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",
});
我在 AWS 上有一个带有 MySQL 数据库的 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 如果您的脚本 运行 在该 EC@ 本地,那么您将能够建立连接。
添加一条随处访问的规则并尝试一下。 mysql
问题已通过完全不使用 ssh 隧道连接得到解决。我只为我的家庭 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",
});