问题描述
我有一个这样的 ssh 配置文件。 我有一个从 test2 到 host1 的代理跳转。
Host host1
Hostname xxxxxx.us-east-1.elb.amazonaws.com
Port 2222
User xxxx
IdentityFile ~/.ssh/cert
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
KeepAlive yes
ServerAliveInterval 30
ServerAliveCountMax 30
Host test2
Hostname xx.xxx.xx.xxx
ProxyCommand ssh.exe host1 -q -W %h:%p host1
User ubuntu
IdentityFile ~/.ssh/cert
KeepAlive yes
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
ServerAliveInterval 30
ServerAliveCountMax 30
我的 mongo 数据库主机:xxx-nonprod.cluster-xx.us-east-1.docdb.amazonaws.com
我必须使用主机 test2 使用 SSH 隧道到 Mongodb,但它使用 ProxyCommand
使用代理跳转我想使用带有 Mongo DB Compass 和 node js mongoose 的 SSH 隧道连接到 mongodb。
如何使用 Mongo DB Compass 进行连接?
这里我没有输入 ProxyCommand 详细信息的选项。
如何使用 node js 进行连接?
我正在使用 tunnel-ssh,我有一个参考代码,
var config = {
username:'ubuntu',host:'xx.xxx.xx.xxx',agent : process.env.SSH_AUTH_SOCK,privateKey:require('fs').readFileSync('~/.ssh/cert'),port:22,dstPort:27017
};
var server = tunnel(config,function (error,server) {
});
我如何在此处输入 ProxyCommand 详细信息?或者请推荐任何解决此问题的 node js 包。
解决方法
我现在可以连接到数据库了。
-
从终端建立一个通过堡垒到数据库的隧道。
ssh -L 27017:{mongodb-host}:27017 host1
-
我可以通过隧道从另一个终端的 localhost 连接到数据库。
mongo --host 127.0.0.1:27017 --username {username} --password {password}
所以我也可以使用连接字符串通过猫鼬进行连接。 mongodb://dbadmin:{username}:{password}@localhost:27017