【发布时间】: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