gpt4 book ai didi

mysql - 如何通过 ssh(在 Mac 上)使用 Node 的 mysql 库?谷歌搜索后,我仍然得到 "channel 3: open failed: connect failed: Connection refused"

转载 作者:太空宇宙 更新时间:2023-11-03 11:44:29 26 4
gpt4 key购买 nike

从头开始,我在谷歌上搜索了如何使用 node.js 和 mysql 库通过 ssh 连接到 mysql 数据库,我遇到了这个:

Node.js connecting through ssh

所以我启动了一个“屏幕” session ,使用 ssh 命令连接,并在 Node 脚本中创建了连接。但是,我遇到了一个错误。接受的答案下方的评论有同样的问题:

I'm using a mac terminal, I typed 'screen', entered in the information you provided with my domain and password,and succesfully connected into my server via ssh. However, when I run my server.js node file the problem still persists. I'm receiving: { [Error: connect ECONNREFUSED] code: 'ECONNREFUSED', errno: 'ECONNREFUSED', syscall: 'connect', fatal: true } Is there a step here that I missed? I'm able to query successfully with this code on servers that don't require ssh.

响应将我带到某个地方,但没有完全解释我需要做什么:

After you connected via ssh you need to connect your node.ja app to localhost. Because with that ssh command in screen you make port 3306 from mysql server available on your local machine

究竟如何“将您的 node.js 应用程序连接到本地主机”?我看到在远程服务器端,我收到了 channel 3: open failed: connect failed: Connection refused。因此,某种请求已成功发送到我的远程服务器。但是,有些事情失败了。谷歌搜索让我得到了这个答案:

SSH -L connection successful, but localhost port forwarding not working "channel 3: open failed: connect failed: Connection refused"

The simplest explanation for the rejection is that, on server.com, there's nothing listening for connections on localhost port 8783. In other words, the server software that you were trying to tunnel to isn't running, or else it is running but it's not listening on that port.

所以现在我卡住了。如何使服务器“监听”以便 mysql 可以通过 ssh 工作?

谢谢!

最佳答案

FWIW 通过 ssh 隧道连接 mysql 可以在进程内完成 mysql2ssh2模块。例如:

var mysql = require('mysql2');
var Client = require('ssh2').Client;

var ssh = new Client();
ssh.on('ready', function() {
ssh.forwardOut(
// source address, this can usually be any valid address
'127.0.0.1',
// source port, this can be any valid port number
12345,
// destination address (localhost here refers to the SSH server)
'127.0.0.1',
// destination port
3306,
function (err, stream) {
if (err) throw err;
var sql = mysql.createConnection({
user: 'foo',
database: 'test',
stream: stream // <--- this is an important part
});
// use `sql` connection as usual
});
}).connect({
// ssh connection config ...
});

此外,由于创建 ssh 连接会产生开销,您可能希望创建一个 ssh 连接池以便更好地重用。

关于mysql - 如何通过 ssh(在 Mac 上)使用 Node 的 mysql 库?谷歌搜索后,我仍然得到 "channel 3: open failed: connect failed: Connection refused",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39626010/

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