简体   繁体   English

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

[英]Connect to AWS RDS MySQL database instance with Flask-SQLAlchemy

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 documentation says that the format of the SQLALCHEMY_DATABASE_URI for mysql database connections should look like this: Flask-SQLAlchemy 文档说 mysql 数据库连接的 SQLALCHEMY_DATABASE_URI 的格式应该如下所示:

mysql://username:password@server/db

Additionally, I can check the AWS RDS console for information on my database instance.此外,我可以查看 AWS RDS 控制台以获取有关我的数据库实例的信息。 The console looks like this .控制台看起来像这样

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

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

This is a valid path: EC2 dashboard -> Find network & security from left side panel -> Security groups.这是一个有效的路径:EC2 仪表板 -> 从左侧面板查找网络和安全 -> 安全组。

安全组

If you have a security group created, you may go to number 2.如果您创建了安全组,则可以转到第 2 步。

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

There you can name your security group and description.您可以在那里命名您的安全组和描述。

VPC will probably be automatically created for you. VPC 可能会自动为您创建。

创建安全组窗口

After creating it, select it from the list.创建后,从列表中选择它。

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

Here you can configure inbound connections rules.您可以在此处配置入站连接规则。

入站窗口

On edit -> add rule you have default presets for easy configuration, for example HTTP on port 80.在编辑 -> 添加规则时,您有默认预设以便于配置,例如端口 80 上的 HTTP。

编辑入站连接规则窗口

I hope this helps everyone from now on.我希望从现在开始这对每个人都有帮助。

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

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

Since AWS interface has changed a lot so reposting the answer with updated interface.由于AWS界面发生了很大变化,因此使用更新的界面重新发布答案。

  1. Go to your database instances , click on your DB identifier for your db.转到您的database instances ,单击您的DB identifier
  2. There you'll see Connectivity & security , in that click on VPC security groups under Security .在那里你会看到Connectivity & security ,点击Security下的VPC security groups There you'll see a list of security groups.在那里你会看到一个安全组列表。
  3. Now click on Inbound Rules , then on the right you'll see Edit Inbound Rules , click on that.现在点击Inbound Rules ,然后在右边你会看到Edit Inbound Rules ,点击它。
  4. Your Inbound Rules should look like the picture below.您的Inbound Rules应如下图所示。

图片

After adding these rules, just save them & try to connect, it should work.添加这些规则后,只需保存它们并尝试连接,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM