尽管设置了入站规则,但仍无法连接到 AWS RDS

Cannot connect to AWS RDS despite setting inbound rules

我正在使用 AWS RDS,我想远程连接到数据库。但是,我不断收到

Database connection failed due to connection to server at 
"mydb.12345.eu-central-1.rds.amazonaws.com" (x.xx.xxx.xx),
port 5432 failed: Connection timed out (0x0000274C/10060)
Is the server running on that host and accepting TCP/IP connections?

这些似乎是一个非常普遍的问题,所有解决方案都建议将入站规则设置为接受来自您自己 IP 的所有流量。

但是,对我来说,这并不能解决问题。

这是我的设置:

这些是安全组中的入站规则sg-650cbe0b

我还尝试添加入站规则,例如:

但是没有用。

我试过通过我的移动网络连接(看看是否是防火墙问题),但我遇到了同样的错误。

但是,我从 AWS Lambda 函数中访问这个数据库,并且没有问题。

这是我用来访问数据库的代码:

import psycopg2
import sys
import boto3
import os

ENDPOINT="mydb.12345.eu-central-1.rds.amazonaws.com"
PORT="5432"
USER="admin"
REGION="eu-central-1b"
DBNAME="mydb"

#gets the credentials from .aws/credentials
session = boto3.Session()
client = session.client('rds')

token = client.generate_db_auth_token(DBHostname=ENDPOINT, Port=PORT, DBUsername=USER, Region=REGION)

try:
    conn = psycopg2.connect(host=ENDPOINT, port=PORT, database=DBNAME, user=USER, password=token, sslrootcert="SSLCERTIFICATE")
    cur = conn.cursor()
    cur.execute("""SELECT now()""")
    query_results = cur.fetchall()
    print(query_results)
except Exception as e:
    print("Database connection failed due to {}".format(e))       

RDS 连接失败的原因有很多,包括:

  • RDS 实例未配置为 publicly accessible
  • 它是在私有子网中启动的,没有到 IGW 的路由

我推荐 RDS connectivity troubleshooter如何解决与使用 public 或 VPC 私有子网的 Amazon RDS 数据库实例的连接问题?