用ssh tunnel的方式设置 AWS DocumentDB 公网访问

AWS DocumentDB的设定是只允许VPC内进行访问的,同时官方文档给了步骤,通过ssh tunnel的方式,可以从公网,或者从VPC外的网络,对DocumentDB进行访问。

我阅读了AWS官方文档并测试了这个步骤,如下是详细的步骤说明:

1. 我们需要DocumentDB在同一个VPC内的,具有公网IP的EC2,来实现ssh tunnel的方式。我们要先确保在EC2上能够连结到DocumentDB ( DocumentDB的安全组也需要开放EC2的Private IP)

2. 下面是在本地创建ssh tunnel的测试步骤 :

2.1:

复制代码
ssh -i "C:\Users\xxxxxx\xxxxxx.pem" -L 27017:docdb.cluster-xxxxx.docdb.cn-northwest-1.amazonaws.com.cn:27017 ec2-user@<ec2 host name> -N

2.2

创建ssh tunnel以后,这个terminal的tab页面本身是没有反应的,就像是这个query hang住了一样,但是您可以在本地的这个terminal上,另起一个tab (我用的是mobaxterm工具)

2.3我的本地无法安装MongoDB客户端,在这里我用telnet测试了一下27017端口,来验证上一步中的ssh tunnel是否测试成功:

复制代码
 /home/mobaxterm  telnet localhost 27017
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.

2.4 这时候,我们断开了2.1时候创建的ssh tunnel

2.5 再次测试telnet,发现没有办法连接:

复制代码
 /home/mobaxterm  telnet localhost 27017
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused

如上测试便证明,本地和DocumentDB通过ssh tunnel是可以连接的,您如果在本地可以安装mongo client, 那么也是可以用Mongo client连接到documentdb的。

**3.**如果我们不用localhost来连接,而是通过本地的公网IP,让其他的机器通过这个tunnel来连接,那么测试步骤如下:

在下面的测试例子中,我们将DocumentDB所在VPC内的公网访问的EC2 跳板机标记为EC2 A,然后从EC2 B上去访问DocumentDB:EC2 B --> EC2 A --> DocumentDB

3.1确保EC2 A能够连结DocumentDB,确保EC2 B可以ssh 到EC2 A上。

3.2 在EC2 B上,建立如下ssh tunnel:

复制代码
[ec2-user@ip-EC2 B ~]$ ssh -i <EC2 B.pem> -L 0.0.0.0:27017:shirandoc.cluster-xxxxxxx.docdb.cn-north-1.amazonaws.com.cn:27017 ec2-user@<EC2 A public IP> -N

3.3. 确保 EC2 B的安全组,开放了允许自己的公网IP访问的Ingress Rule

**3.4.**在EC2 B上测试如下:

复制代码
[ec2-user@ip-xx ~]$ telnet 127.0.0.1 27017
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
^]
telnet> quit

[ec2-user@ip-xx ~]$ telnet <EC2 B public IP> 27017
Trying 69.230.254.208...
Connected to 69.230.254.208.
Escape character is '^]'.
quit
}
^]

[ec2-user@ip-10-0-0-60 ~]$ telnet localhost 27017
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
^]

我们也在EC2 B上安装了mongo client, 测试用EC2 B的公网IP连结也是成功的:

复制代码
[ec2-user@ip-xxx ~]$ mongo --host <EC2 B 公网IP>:27017 --username root --password xxxxx
                      MongoDB shell version v4.0.28
connecting to: mongodb://<EC2 B public IP>:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("4041eeb8-9bd4-4781-b20e-58f391f45e75") }
MongoDB server version: 4.0.0

Warning: Non-Genuine MongoDB Detected

This server or service appears to be an emulation of MongoDB rather than an official MongoDB product.

Some documented MongoDB features may work differently, be entirely missing or incomplete, or have unexpected performance characteristics.

To learn more please visit: https://dochub.mongodb.org/core/non-genuine-mongodb-server-warning.

rs0:PRIMARY> ^C
bye

参考文档:

https://docs.amazonaws.cn/documentdb/latest/developerguide/connect-from-outside-a-vpc.html

相关推荐
阿里云大数据AI技术13 分钟前
ODPS 15周年开发者活动|征文+动手实践双赛道开启,参与活动赢定制好礼!
大数据·人工智能·云计算
码农小站34 分钟前
ClickHouse 时间范围查询:精准筛选「本月数据」
数据库
paopaokaka_luck1 小时前
基于SpringBoot+Vue的非遗文化传承管理系统(websocket即时通讯、协同过滤算法、支付宝沙盒支付、可分享链接、功能量非常大)
java·数据库·vue.js·spring boot·后端·spring·小程序
小疯仔1 小时前
navicat导出数据库的表结构
数据库
TOSUN同星1 小时前
干货分享 | TSMaster DBC编辑器操作指南:功能详解+实战示例
数据库·oracle·编辑器·汽车·软件工程
慧星云2 小时前
ComfyUI工作流 :一键二次元角色转真人
人工智能·云计算·aigc
huihui4502 小时前
一天一道Sql题(day01)
数据库
~尼卡~2 小时前
软考(软件设计师)数据库原理:事务管理,备份恢复,并发控制
数据库·软件设计师-软考
八九燕来3 小时前
Django双下划线查询
数据库·django·sqlite
容器魔方3 小时前
开源之夏2025 | Karmada 社区中选学生名单公布!
云原生·容器·云计算