【问题标题】:Cannot connect to AWS RDS despite setting inbound rules尽管设置了入站规则,但仍无法连接到 AWS RDS
【发布时间】:2022-02-24 02:56:46
【问题描述】:

我正在使用 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))       

【问题讨论】:

  • 数据库实例是publicly accessible吗?它在公共子网中吗? troubleshooting RDS connections 指南。
  • 感谢此链接!我列出了 3 个子网,但与之关联的只有xxx.xx.x.x/16 > local。我添加了0.0.0.0/0 > internet gateway,现在它可以工作了。

标签: amazon-web-services amazon-rds


【解决方案1】:

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

  • RDS 实例未配置为publicly accessible
  • 它被启动到一个私有子网并且没有到 IGW 的路由

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

【讨论】:

    猜你喜欢
    • 2021-04-13
    • 2021-05-30
    • 2016-10-13
    • 1970-01-01
    • 2013-03-29
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多