如何從 Linux/macOS 裝置使用堡壘主機連接至 Amazon RDS 資料庫執行個體?

2 分的閱讀內容
0

我有一個無法公開存取的 Amazon Relational Database Service (Amazon RDS) 資料庫執行個體。我想從我的 Linux/macOS 機器連接至它。如何使用堡壘主機連接至我的 RDS 資料庫執行個體?

簡短描述

若要連接至私有 Amazon RDS 或 Amazon Aurora 資料庫執行個體,最佳實務是使用 VPNAWS Direct Connect。如果您無法使用 VPN 或 AWS Direct Connect,則偏好選項是使用堡壘主機。您也可以使用此方法從 VPC 外部連接至 Aurora Serverless 和 RDS Proxy。此範例說明如何設定堡壘主機,以從 Linux/macOS 機器連接至您的 RDS 資料庫執行個體,即使 RDS 資料庫執行個體為私有亦同。

解決方法

1.    修改資料庫執行個體,將 Amazon RDS 資料庫執行個體設定為私有。將可公開存取參數設為,而使用私有子網絡 (即,路由表中沒有網際網路閘道 – igw)。設定安全群組,以允許資料庫從所有 IP 連接 (54323306)。

2.    在與資料庫執行個體相同的 VPC 中啟動最小可用的 EC2 執行個體。將 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體設定為可透過網際網路存取,使用公有子網路 (即路由表中有網際網路閘道 – igw)。設定安全群組以允許您嘗試連線之 Linux/macOS 機器的 IP。

3.    從您的 Linux/macOS 機器執行下列命令,以建立從您的機器連線的通道:

Syntax 1:
ssh -i <identity_file> -f -l <bastion-host-username> -L
<local-port-you-connect-to>:<rds-endpoint>:<rds:listening-port>
<bastion-host-public-ip> -v

Example Command:
ssh -i "private_key.pem" -f -l ec2-user -L 5432:172.31.39.62:5432  3.133.141.189 -v

執行上述命令 (SSH 通道) 時,您可以設定下列設定:

  • 偵錯 1: 到 LOCALHOST: 5432 的本機連線轉送至遠端位址 172.31.39.62:5432
  • 偵錯 1: 在 127.0.0.1 連接埠 5432 上的本機轉送接聽。
  • 偵錯 1:通道 0:新的 [連接埠接聽程式]
  • 偵錯 1: 在 ::1 連接埠 5432 上的本機轉送接聽。
Syntax 2:
ssh -i "Private_key.pem" -f -N -L 5433:RDS_Instance_Endpoint:5432 ec2-user@EC2-Instance_Endpoint -v

Example Command:
ssh -i "private.pem" -f -N -L
5433:pg115.xxxx.us-east-2.rds.amazonaws.com:5432
ec2-user@ec2-xxxx-xxx9.us-east-2.compute.amazonaws.com -v

4.    現在 SSH 通道已經到位,您可以從本機 Linux/macOS 機器連接至資料庫執行個體。下列範例會連接至 PostgreSQL,但您也可以使用此方法連接至 MySQL 或您想要連線的任何其他引擎。

Syntax -

psql -hlocalhost -Upostgres -p<local-port-you-connect-to> -d postgres


-h = localhost

-U = the username present in the DB for connectivity

-p = 'local-port-you-connect-to' from the SSH Tunneling command

-d = Any DB, user wish to connect.

Example command -
a483e73d651f:.ssh rahul_saha$ psql -hlocalhost -Upostgres -p5432 -d postgres
Password for user postgres:
psql (12.1, server 11.5)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

postgres=> \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 rdsadmin  | rdsadmin | UTF8     | en_US.UTF-8 | en_US.UTF-8 | rdsadmin=CTc/rdsadmin
 template0 | rdsadmin | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/rdsadmin          +
           |          |          |             |             | rdsadmin=CTc/rdsadmin
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)

$ psql -hlocalhost -Upostgres -p -d postgres
$ mysql -h127.0.0.1 -uroot -p

**注意:**如果您在連接至資料庫執行個體時使用關鍵字 localhost,MySQL 會嘗試使用通訊端連線。在存取 MySQL 資料庫執行個體時,請務必使用主機名稱 127.0.0.1。如需詳細資訊,請參閱無法連接至 [本機] MySQL 伺服器的 MySQL 文件。


相關資訊

AWS 上的 Linux 堡壘主機

使用 Amazon EC2 執行個體作為堡壘主機,從本機電腦連接至私有 Amazon RDS 資料庫執行個體