首页 > 解决方案 > 如何使用身份验证和 SSH 隧道连接到远程 MongoDB

问题描述

我正在尝试使用来自 ROBO3T 的以下凭据连接到远程 MongoDB,并且连接成功。

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

我正在尝试通过 .sh 文件(即 shell 脚本)复制相同的内容,但它不起作用。

1)这是我正在执行 frm mongo shell script 的命令,

mongo --username username --password password --authenticationDatabase admin --host ip-test --port 27017

但是我收到了 Socket Exception 和 COnnection Timed Out Exception 并且无法连接到远程 MongoDB。

2)所以,我在 mongo.cfg 文件中添加了以下 ip,

bindIp: ip-test,127.0.0.1,0.0.0.0

3)我也在windows主机文件中定义了ip-test。

4)在所有配置之后,我也尝试了以下命令,

mongo -u username -p password ip-test:27017/sample --authenticationDatabase admin

但仍然得到相同的套接字错误和连接超时。

5)然后我尝试在 mongo shell 命令本身中提供 ssh 隧道(配置),如下所示,

mongo ip-test:27017/sample -u username -p pasword -e 'SHOW DATABASES;' -h 127.0.0.1 | ssh -fN -l root -i "path for private key/id_rsa" -L 22:ip-test:27017 ip-test

这不是给出任何套接字异常,而是给出连接超时。

当我尝试独立于终端连接 ssh 时,它成功了。

但问题是我想将 mongo shell 命令与身份验证和 ssh 隧道结合在一个命令中。

谁能帮助我理解,如何通过 .SH 文件(Shell 脚本)通过身份验证和 SSH 配置连接到远程 MongoDB 服务器?

我对使用 shell 脚本的 ssh Tunneling 非常陌生,需要帮助。

非常感谢你。

标签: mongodbshellssh

解决方案


推荐阅读