【发布时间】:2020-11-21 19:18:05
【问题描述】:
很抱歉打扰您,但我一直在努力使用 Python 根据这条指令https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.Connecting.Python.html 连接到 AWS PostgreSQL 数据库,因为它总是显示超时错误。
我还在 ~/.aws 文件夹中设置了一个配置文件来配置 boto3,内容如下:
[default]
aws_access_key_id = X
aws_secret_access_key = X
import os
import boto3
import psycopg2
ENDPOINT="url"
PORT="5432"
USR="kaggle"
REGION="us-east-2a"
os.environ['LIBMYSQL_ENABLE_CLEARTEXT_PLUGIN'] = '1'
session = boto3.Session(profile_name='default')
client = boto3.client('rds',region_name=REGION)
token = client.generate_db_auth_token(DBHostname=ENDPOINT, Port=PORT, DBUsername=USR, Region=REGION)
try:
conn = psycopg2.connect(host=ENDPOINT, port=PORT, user=USR, password=token)
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))
错误是:
Database connection failed due to could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "url" (IP) and accepting
TCP/IP connections on port 5432?
【问题讨论】:
-
永远不要在任何地方泄露您的凭据!请立即在您的 IAM 控制台中禁用这些凭证!
-
这段代码在哪里运行(EC2,你自己的电脑)? Amazon RDS 数据库是否配置为
Publicly Accessible = Yes?您是否能够以任何其他方式(例如使用 SQL 客户端)连接到数据库? -
我在自己的电脑上运行这段代码。我已将其配置为可公开访问。我尝试使用此 SQL 服务器连接到数据库,但它也显示连接失败。 (aws.amazon.com/getting-started/hands-on/…)
-
您是否安装了 telnet。如果是,请尝试
telnet endpointUrl 5432。同样在 RDS 的安全组中,确保在 In Bound 规则中允许您的公共 IP 地址
标签: python postgresql amazon-web-services boto3 psycopg2