首页 > 解决方案 > 如何以编程方式连接到我的 RDS MySQL 实例?

问题描述

问题

我启动了一个 MySQL RDS 实例,并能够使用 MySQL Workbench 成功连接到它。但是,我仍然无法使用以下 URI 从本地工作站连接到它:

'mysql+pymysql://user:password@db_identifier.XXXXXXXXXX.us-east-1.rds.amazonaws.com:3306/db_name'

或没有端口的相同 URI:

'mysql+pymysql://user:password@db_identifier.XXXXXXXXXX.us-east-1.rds.amazonaws.com/db_name'

当我将其指定为我的数据库 URI 并执行db.create_all()命令时收到的错误是:

sqlalchemy.exc.OperationalError: 
(pymysql.err.OperationalError) 
(2003, "Can't connect to MySQL server on 'db_identifier.XXXXXXXXXX.us-east-1.rds.amazonaws.com' 
([WinError 10060] A connection attempt failed because the connected party did not 
properly respond after a period of time, or established connection failed because 
connected host has failed to respond)")

问题

如何使用 pymysql 进行连接?为什么它会连接 MySQL Workbench 而不是通过这个 URI?

语境

我正在关注这里的教程。这使用 SQLAlchemy 在 Python 中执行 SQL 语句。

RDS 实例(及其关联的子网/VPC)具有以下内容:

标签: mysqlamazon-web-servicessqlalchemyamazon-rds

解决方案


检查我对这篇文章的回答,这可能与您的 rds“连接性和安全性”部分中的“公共可访问性”选项有关,如此处所述;

https://stackoverflow.com/a/63514997/2934184


推荐阅读