如何使用代理跳转堡垒主机通过 ssh 隧道连接到 mongodb 服务器

问题描述

我有一个这样的 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 Compassnode js mongoose 的 SSH 隧道连接到 mongodb。

如何使用 Mongo DB Compass 进行连接?

enter image description here

这里我没有输入 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 包。

解决方法

我现在可以连接到数据库了。

  1. 从终端建立一个通过堡垒到数据库的隧道。

    ssh -L 27017:{mongodb-host}:27017 host1

  2. 我可以通过隧道从另一个终端的 localhost 连接到数据库。

    mongo --host 127.0.0.1:27017 --username {username} --password {password}

所以我也可以使用连接字符串通过猫鼬进行连接。 mongodb://dbadmin:{username}:{password}@localhost:27017

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...