gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:16:09 27 4
gpt4 key购买 nike

我在 AWS 上有一个带有 MySQL 数据库的 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 隧道并运行 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);
})
}
})

最佳答案

通过根本不使用 ssh 隧道连接解决了该问题。我仅为我的家庭 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",
});

关于mysql - 通过tunnel-ssh使用sequelize访问远程数据库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54782259/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com