使用 Flask-SQLAlchemy 连接到 AWS RDS MySQL 数据库实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14850341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:33:58  来源:igfitidea点击:

Connect to AWS RDS MySQL database instance with Flask-SQLAlchemy

mysqlamazon-rdsflask-sqlalchemy

提问by Moni

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

对于我的 Flask 应用程序,我想使用 Flask-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) (2003, "无法连接到 MySQL 服务器上的 'xxxxxxxxxxxxxxx.xxxxxxxxxxxx.us-east-1.rds.amazonaws.com'(60")

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 documentationsays that the format of the SQLALCHEMY_DATABASE_URI for mysql database connections should look like this:

瓶-SQLAlchemy的文档说,SQLALCHEMY_DATABASE_URI对mysql数据库连接的格式应该是这样的:

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,那将完全解决问题。

采纳答案by sbose

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

转到您的 AWS RDS 控制台并选择您的实例 -> 数据库安全组 -> 单击默认

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,这样防火墙就不会阻止您从任何主机/网络访问它。

回答by Fábio Correia

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

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

Security groups

安全组

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

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

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

There you can name your security group and description.

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

VPC will probably be automatically created for you.

VPC 可能会自动为您创建。

Create security group window

Create security group window

After creating it, select it from the list.

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

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

Here you can configure inbound connections rules.

您可以在此处配置入站连接规则。

Inbound window

Inbound window

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

在编辑 -> 添加规则时,您有默认预设以便于配置,例如端口 80 上的 HTTP。

Edit inbound connection rules window

Edit inbound connection rules window

I hope this helps everyone from now on.

我希望从现在开始这对每个人都有帮助。

  • Due to security reasons, try to only allow trusted IPs and / or security groups needed.
  • 由于安全原因,尽量只允许受信任的 IP 和/或需要的安全组。

回答by Winston C Nwanne

make sure you're creating a database name in the initial setup of the database under addition configuration unless AWS RDS won't create a database.

确保在附加配置下的数据库初始设置中创建数据库名称,除非 AWS RDS 不会创建数据库。