使用flas - sqlalchemy连接到AWS RDS MySQL数据库实例

时间:2021-02-17 12:47:09

For my Flask app, I want to use the Flask-SQLAlchemy extension to connect to a database instance I created on AWS RDS.

对于Flask应用程序,我想使用flas - sqlalchemy扩展连接到我在AWS RDS上创建的一个数据库实例。

When I try to connect, the application times out and I get the following error:

当我尝试连接时,应用程序超时,我得到以下错误:

sqlalchemy.exc.OperationalError: (OperationalError) (2003, "Can't connect to MySQL server on 'xxxxxxxxxxxxxxx.xxxxxxxxxxxx.us-east-1.rds.amazonaws.com'(60")

sqlalchemy.exc。OperationalError: (OperationalError) (OperationalError)(2003),“无法连接MySQL服务器”。

My Code Looks Like This:

我的代码是这样的:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
application = Flask(__name__)

application.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://{master username}:{db password}@{endpoint}/{db instance name}'
db = SQLAlchemy(application)

@application.route('/')
def hello_world():
    return 'Hello World'

if __name__ == '__main__':
    application.run()

The Flask-SQLAlchemy documentation says that the format of the SQLALCHEMY_DATABASE_URI for mysql database connections should look like this:

flas - sqlalchemy文档说,mysql数据库连接的SQLALCHEMY_DATABASE_URI的格式应该如下所示:

mysql://username:password@server/db

Additionally, I can check the AWS RDS console for information on my database instance. The console looks like this.

此外,我还可以检查AWS RDS控制台以获取关于数据库实例的信息。控制台是这样的。

Right now I am assuming that "username" in SQLAlchemy refers to the "Master Username" in the AWS RDS console, "server" in SQLAlchemy refers to the "Endpoint" in the AWS RDS console, and "db" refers to the "Database Instance Name"

现在我假设SQLAlchemy中的“用户名”指的是AWS RDS控制台的“主用户名”,SQLAlchemy中的“服务器”指的是AWS RDS控制台的“端点”,“db”指的是“数据库实例名”

What am I doing wrong??

我做错了什么?

If someone could describe the SQLALCHEMY_DATABASE_URI for me with the AWS RDS console terms, that would completely solve the problem.

如果有人可以用AWS RDS控制台术语为我描述SQLALCHEMY_DATABASE_URI,这将完全解决问题。

2 个解决方案

#1


20  

Go to your AWS RDS console and choose your instance -> DB Security Groups -> Click Default

转到AWS RDS控制台并选择实例—> DB安全组—>单击Default

and add a new CIDR as per the recommended IP range.

并根据建议的IP范围添加一个新的CIDR。

This is basically a firewall configuration which determines who can connect to the database instance.

这基本上是一个防火墙配置,它确定谁可以连接到数据库实例。

You could set it as 0.0.0.0/0 so that firewall doesn't prevent you from accessing it from any host/network.

您可以将其设置为0.0.0.0/0,这样防火墙就不会阻止您从任何主机/网络访问它。

#2


1  

This is a valid path: EC2 dashboard -> Find network & security from left side panel -> Security groups.

这是一条有效的路径:EC2仪表板->从左侧面板找到网络和安全——>安全组。

使用flas - sqlalchemy连接到AWS RDS MySQL数据库实例

If you have a security group created, you may go to number 2.

如果您创建了一个安全组,您可以转到第2。

  1. Creating a new security group:
  2. 创建一个新的安全组:

There you can name your security group and description.

在那里,您可以命名您的安全组和描述。

VPC will probably be automatically created for you.

VPC可能会自动为您创建。

使用flas - sqlalchemy连接到AWS RDS MySQL数据库实例

After creating it, select it from the list.

创建它之后,从列表中选择它。

  1. Set up inbound and / or outbound connections:
  2. 设置入站和/或出站连接:

Here you can configure inbound connections rules.

在这里,您可以配置入站连接规则。

使用flas - sqlalchemy连接到AWS RDS MySQL数据库实例

On edit -> add rule you have default presets for easy configuration, for example HTTP on port 80.

在编辑->添加规则中,您有默认设置,例如端口80上的HTTP。

使用flas - sqlalchemy连接到AWS RDS MySQL数据库实例

I hope this helps everyone from now on.

我希望这能帮助大家从现在开始。

#1


20  

Go to your AWS RDS console and choose your instance -> DB Security Groups -> Click Default

转到AWS RDS控制台并选择实例—> DB安全组—>单击Default

and add a new CIDR as per the recommended IP range.

并根据建议的IP范围添加一个新的CIDR。

This is basically a firewall configuration which determines who can connect to the database instance.

这基本上是一个防火墙配置,它确定谁可以连接到数据库实例。

You could set it as 0.0.0.0/0 so that firewall doesn't prevent you from accessing it from any host/network.

您可以将其设置为0.0.0.0/0,这样防火墙就不会阻止您从任何主机/网络访问它。

#2


1  

This is a valid path: EC2 dashboard -> Find network & security from left side panel -> Security groups.

这是一条有效的路径:EC2仪表板->从左侧面板找到网络和安全——>安全组。

使用flas - sqlalchemy连接到AWS RDS MySQL数据库实例

If you have a security group created, you may go to number 2.

如果您创建了一个安全组,您可以转到第2。

  1. Creating a new security group:
  2. 创建一个新的安全组:

There you can name your security group and description.

在那里,您可以命名您的安全组和描述。

VPC will probably be automatically created for you.

VPC可能会自动为您创建。

使用flas - sqlalchemy连接到AWS RDS MySQL数据库实例

After creating it, select it from the list.

创建它之后,从列表中选择它。

  1. Set up inbound and / or outbound connections:
  2. 设置入站和/或出站连接:

Here you can configure inbound connections rules.

在这里,您可以配置入站连接规则。

使用flas - sqlalchemy连接到AWS RDS MySQL数据库实例

On edit -> add rule you have default presets for easy configuration, for example HTTP on port 80.

在编辑->添加规则中,您有默认设置,例如端口80上的HTTP。

使用flas - sqlalchemy连接到AWS RDS MySQL数据库实例

I hope this helps everyone from now on.

我希望这能帮助大家从现在开始。