【问题标题】:how to connect to private RDS from localhost如何从 localhost 连接到私有 RDS
【发布时间】:2022-01-06 10:09:52
【问题描述】:

我有一个带有私有子网的私有 VPC,一个私有 jumpbox 在 1 个私有子网中,我的私有 RDS aurora MySql 无服务器实例在另一个私有子网中。

我在本地笔记本电脑上执行了这些命令,以尝试通过端口转发连接到我的 RDS:

aws ssm start-session --target i-0d5470040e7541ab9 --document-name AWS-StartPortForwardingSession --parameters "portNumber"=["5901"],"localPortNumber"=["9000"] --profile myProfile

aws ssm start-session --target  i-0d5470040e7541ab9 --document-name AWS-StartPortForwardingSession --parameters "portNumber"=["22"],"localPortNumber"=["9999"] --profile myProfile 

aws ssm start-session --target  i-0d5470040e7541ab9 --document-name AWS-StartPortForwardingSession --parameters "portNumber"=["3306"],"localPortNumber"=["3306"] --profile myProfile 

与服务器的连接挂起。

我的本​​地笔记本电脑出现了这个错误:

Starting session with SessionId: myuser-09e5cd0206cc89542
Port 3306 opened for sessionId myuser-09e5cd0206cc89542.
Waiting for connections...

Connection accepted for session [myuser-09e5cd0206cc89542]

Connection to destination port failed, check SSM Agent logs.

以及/var/log/amazon/ssm/errors.log 中的那些错误:

2021-11-29 00:50:35 ERROR [handleServerConnections @ port_mux.go.278] [ssm-session-worker] [myuser-017cfa9edxxxx] [DataBackend] [pluginName=Port] Unable to dial connection to server: dial tcp :3306: connect: connection refused
2021-11-29 14:13:07 ERROR [transferDataToMgs @ port_mux.go.230] [ssm-session-worker] [myuser-09e5cdxxxxxx] [DataBackend] [pluginName=Port] Unable to read from connection: read unix @->/var/lib/amazon/ssm/session/3366606757_mux.sock: use of closed network connection

我尝试像这样连接到 RDS:

我什至尝试使用 ssh Tunnel 放置 RDS Endpoint,但它不起作用:

在远程服务器 ec2-instance 上是否需要执行任何其他步骤?

似乎连接被接受,但到目标端口的连接不起作用。

感谢您对此的帮助!

【问题讨论】:

    标签: amazon-ec2 nestjs portforwarding aws-ssm aws-aurora-serverless


    【解决方案1】:

    start-session 命令将端口从目标 EC2 实例隧道传输到 localhost。 RDS 实例位于另一台主机上,因此您必须使用 SSH 隧道。

    将您的公钥发送到 EC2 实例。填写区域和可用区参数。

    aws ec2-instance-connect send-ssh-public-key --region us-west-2 --instance-id i-0d5470040e7541ab9 --availability-zone us-west-2a --instance-os-user ec2-user --ssh-public-key file://~/.ssh/id_rsa.pub
    

    将 SSH 端口 22 从 EC2 实例转发到本地 9999。

    aws ssm start-session --target i-0d5470040e7541ab9 --document-name AWS-StartPortForwardingSession --parameters "portNumber"=["22"],"localPortNumber"=["9999"] --profile myProfile
    

    通过隧道 SSH 进入实例(在另一个终端中)。使用您的 RDS 实例的 DNS 填写 rds-instance-dns

    ssh ec2-user@localhost -L 6606:rds-instance-dns:3306  -i ~/.ssh/id_rsa -p 9999
    

    访问RDS

    mysql -h localhost -p 6606
    

    您还需要通过配置安全组来确保您的 EC2 实例具有访问 RDS 实例的正确权限。

    【讨论】:

      猜你喜欢
      • 2019-01-28
      • 1970-01-01
      • 2019-05-16
      • 2013-06-07
      • 2019-06-28
      • 2018-06-27
      • 1970-01-01
      • 2021-11-17
      • 2019-01-01
      相关资源
      最近更新 更多