heroku搭建mysql_在heroku上部署Flask应用程序并将其连接到颚数据库mysql数据库

heroku搭建mysql

By: Edward Krueger Data Scientist and Instructor and Douglas Franklin Teaching Assistant and Technical Writer.

作者: 爱德华·克鲁格(Edward Krueger)数据科学家和讲师, 道格拉斯·富兰克林 ( Douglas Franklin)教学助理和技术作家。

In this article, we’ll cover how to deploy an app with a Pipfile.lock to the cloud and connect the app to a cloud database. For more information on virtual environments or getting started with the environment and package manager Pipenv, check out this article!

在本文中,我们将介绍如何将具有Pipfile.lock的应用程序部署到云上以及如何将该应用程序连接到云数据库。 有关虚拟环境或环境和程序包管理器Pipenv入门的更多信息,请查看 本文

部署问题 (Deployment Issues)

Newer developers often install everything at the system level due to a lack of understanding of, or experience with, virtual environments. Python packages installed with pip are placed at the system level. Retrieving requirements this way for every project creates an unmanageable global Python environment on your machine. Virtual environments allow you to compartmentalize your software while keeping an inventory of dependencies.

由于缺乏对虚拟环境的了解或经验,较新的开发人员通常在系统级别安装所有内容。 与pip一起安装的Python软件包位于系统级别。 以这种方式为每个项目检索需求会在您的计算机上创建一个难以管理的全局Python环境。 虚拟环境使您可以划分软件,同时保留依赖性清单。

Pipenv, a tool for virtual environment and Python package management, allows developers to create isolated software products that are easier to deploy, build upon, and modify.

Pipenv是用于虚拟环境和Python软件包管理的工具,它使开发人员能够创建隔离的软件产品,这些产品更易于部署,构建和修改。

什么是Pipenv? (What is Pipenv?)

Pipenv combines package management and virtual environment control into one tool for installing, removing, tracking, and documenting your dependencies; and for creating, using, and managing your virtual environments. Pipenv is essentially pip and virtualenv wrapped together into a single product.

Pipenv将软件包管理和虚拟环境控制结合到一个工具中,用于安装,删除,跟踪和记录依赖项。 以及用于创建,使用和管理虚拟环境。 Pipenv本质上是将pip和virtualenv打包到一个产品中。

什么是Heroku? (What is Heroku?)

Heroku offers many software products, and we’ll need the Heroku cloud platform service to host an app and JawsDB to use a MySQL database. Don’t worry, creating an account and using these features is free!

Heroku提供了许多软件产品,我们将需要Heroku云平台服务来托管应用程序,而JawsDB需要使用MySQL数据库。 不用担心,创建帐户和使用这些功能是免费的!

We are going to use the Heroku GUI to deploy a database and a Python app.

我们将使用Heroku GUI部署数据库和Python应用程序。

云数据库的好处 (Benefits of cloud database)

In our previous deployment, we used an SQLite database. When using an SQLite database, every app redeployment will reset your database. Heroku’s JawsDB allows our data to persist through app updates. Additionally, hosting, configuring, patching, and managing the database is all done for you with JawsDB.

在之前的部署中,我们使用了SQLite数据库。 使用SQLite数据库时,每次重新部署应用程序都会重置您的数据库。 Heroku的JawsDB允许我们的数据通过应用程序更新保持不变。 此外,使用JawsDB可以为您完成托管,配置,修补和管理数据库。

准备部署 (Preparing for Deployment)

Heroku allows us to deploy an app from a GitHub branch. Once we have a working app with a Pipfile pushed to GitHub, we are ready to make some final changes to the repository to prepare for deployment. Be sure to have your Pipfile at the project’s root directory so Heroku can find it!

Heroku允许我们从GitHub分支部署应用程序。 将Pipfile推送到GitHub后,我们就可以正常工作了,我们准备对存储库进行一些最终更改,以准备进行部署。 确保将Pipfile放在项目的根目录下,以便Heroku可以找到它!

Note: These next changes allow our app to run on Unix systems. Gunicorn is not compatible with PCs, so you will not be able to test these changes locally if you are not using a Linux or Unix machine.

注意:接下来的这些更改使我们的应用程序可以在Unix系统上运行。 Gunicorn与PC不兼容,因此,如果您未使用Linux或Unix计算机,则将无法在本地测试这些更改。

安装gunicorn (Install gunicorn)

Gunicorn is a Python WSGI HTTP server that will serve your Flask application on Heroku. By running the line below, you add gunicorn to your Pipfile.

Gunicorn是Python WSGI HTTP服务器,将在Heroku上为Flask应用程序提供服务。 通过运行以下行,您可以将gunicorn添加到Pipfile中。

pipenv install gunicorn

添加一个Procfile (Add a Procfile)

Create a Procfile in the project root folder and add the following line:

在项目根文件夹中创建一个Procfile并添加以下行:

web: gunicorn app:app

The first app represents the name of the python file that runs your application or the name of the module where the app is located. The second represents your app name, i.e., app.py. This Procfile works with gunicorn and Heroku's Dynos to serve your app remotely.

第一个app代表运行您的应用程序的python文件的名称或该应用程序所在的模块的名称。 第二个代表您的应用名称,即app.py。 该Procfile可与gunicorn和Heroku的Dynos一起使用,以远程服务您的应用程序。

Heroku云数据库的设置和部署 (Heroku Cloud Database Set up and Deployment)

Set up an account with Heroku if you haven’t already, don’t worry all the features we show here are free!

如果您尚未注册Heroku,请不要担心,我们在此显示的所有功能都是免费的!

Go to your app on Heroku.com and click resources. Then type “JawsDB MySQL” into the addons box, as seen below.

转到Heroku.com上的应用程序,然后单击资源。 然后在插件框中键入“ JawsDB MySQL”,如下所示。

Image for post

Select the free version and click provision. Great, we now have a MySQL database deployed for our app. Next, we need to integrate this new database into our app logic.

选择免费版本,然后单击设置。 太好了,我们现在为我们的应用程序部署了一个MySQL数据库。 接下来,我们需要将此新数据库集成到我们的应用程序逻辑中。

First, let’s add Pymysql, a Python SQL library, to our Pipfile with the following.

首先,使用以下代码将Pymysql(Python SQL库)添加到我们的Pipfile中。

pipenv install pymysql

Now let’s get our connection string and modify it for pymysql. Go to settings and look at the configuration variables. You’ll find a connection string that resembles the one below.

现在,让我们获取连接字符串并为pymysql对其进行修改。 转到设置,然后查看配置变量。 您会发现类似于以下内容的连接字符串。

mysql://ael7qci22z1qwer:nn9keetiyertrwdf@c584asdfgjnm02sk.cbetxkdfhwsb.us-east-1.rds.amazonaws.com:3306/fq14casdf1rb3y3n

We need to make a change to the DB connection string so that it uses the Pymysql driver.

我们需要更改数据库连接字符串,以便它使用Pymysql驱动程序。

In a text editor, remove the mysql and add in its place mysql+pymysql and then save the updated string.

在文本编辑器中,删除mysql并在其位置添加mysql+pymysql ,然后保存更新的字符串。

mysql+pymysql

You’ll need to add this to your configuration variables on Heroku. To do this, go to settings, then config vars and update the connection string.

您需要将此添加到Heroku上的配置变量中。 为此,请转到设置,然后配置vars并更新连接字符串。

用.env隐藏连接字符串 (Hiding connection strings with .env)

Create a new file called .env and add the connection string for your cloud database as DB_CONN,shown below.

创建一个名为.env的新文件,并将云数据库的连接字符串添加为DB_CONN,如下所示。

DB_CONN=”mysql+pymysql://root:PASSWORD@HOSTNAME:3306/records_db”

Note: Running pipenv shell gives us access to these hidden environmental variables. Similarly, we can access the hidden variables in Python with os.

注意:运行pipenv shell使我们可以访问这些隐藏的环境变量。 同样,我们可以使用os访问Python中的隐藏变量。

SQLALCHEMY_DB_URL = os.getenv(“DB_CONN”)

Be sure to add the above line to your database.py file so that it ready to connect to the cloud!

确保将以上行添加到您的database.py文件中,以便它可以连接到云!

应用部署 (App Deployment)

Once we have our app tested and working locally, we push all code to the master branch. Then on Heroku, go to deploy a new app to see the page below.

一旦我们的应用程序经过测试并在本地工作,我们便将所有代码推送到master分支。 然后在Heroku上,部署一个新的应用程序以查看下面的页面。

Image for post
Select Github and search for your repository
选择Github并搜索您的存储库

Next on Heroku, select GitHub, enter the name of the repository and hit search. Once your username and repository appear, click connect. Then select the desired branch and click deploy.

接下来在Heroku上,选择GitHub,输入存储库的名称,然后点击搜索。 用户名和存储库出现后,单击“连接”。 然后选择所需的分支,然后单击部署。

Image for post
Select master and deploy the branch
选择主节点并部署分支

Build logs will begin to populate a console on the page. Notice that Heroku looks for a requirements.txt file first then installs dependencies from Pipenv’s Pipfile.lock. If you don’t use Pipenv, you will need to have a requiremnts.txt for this build to occur. Once again, place these files at your project’s root.

构建日志将开始在页面上填充控制台。 请注意,Heroku首先会查找require.txt文件,然后从Pipenv的Pipfile.lock安装依赖项。 如果您不使用Pipenv,则需要具有requiremnts.txt才能进行此构建。 再一次将这些文件放在项目的根目录。

Image for post

Once your environment has been built from the Pipfile.lock and the build is successful, you will see the below message.

从Pipfile.lock构建环境并且构建成功后,您将看到以下消息。

Image for post
Successful app deployment
成功部署应用

The app is successfully deployed! Click to view button to see the deployed app on Heroku.

该应用已成功部署! 单击查看按钮以查看Heroku上已部署的应用程序。

最初建立表格 (Building tables initially)

You might run into an error where your tables have not been created before you attempt to get or post data. To solve this, we use the following line in our app.py.

在尝试获取或发布数据之前,可能尚未创建表的地方可能会遇到错误。 为了解决这个问题,我们在app.py中使用以下行。

@app.before_first_request
def setup():
db.create_all()

自动部署 (Automatic deploy)

We can enable automatic deployment to have changes to the Github master be displayed on Heroku as they are pushed. If you use this method, you’ll want to be sure that you always have a working master branch.

我们可以启用自动部署,以使对Github主服务器的更改在推送时显示在Heroku上。 如果使用此方法,则需要确保始终有一个正常的master分支。

Image for post
Enable automatic deploys
启用自动部署

结论 (Conclusion)

Coding and building useful software requires the management of complexity. We discussed Github as a version control tool, Pipenv as an environment and package manager and some benefits of having cloud companies manage your databases. These tools help reduce the complexity of building software so developers can focus on building rather than managing.

编码和构建有用的软件需要管理复杂性。 我们讨论了Github作为版本控制工具,Pipenv作为环境和程序包管理器,以及让云公司管理您的数据库的一些好处。 这些工具有助于降低构建软件的复杂性,因此开发人员可以专注于构建而不是管理。

Practicing proper environment and package management is crucial for data scientists and developers who want their code deployed, built upon, or used in production. Using an environment and package manager such as Pipenv makes many processes, including deployment, more comfortable and more efficient!

对于希望在自己的产品中部署,构建或使用其代码的数据科学家和开发人员而言,正确的环境和程序包管理至关重要。 使用诸如Pipenv之类的环境和程序包管理器,可以使许多过程(包括部署)更加舒适和高效!

Additionally, having a well managed GitHub master branch with a Pipfile allowed Heroku’s severs to rebuild our app with minimal troubleshooting. This lets us deploy an app from a project directory on GitHub to Heroku in minutes.

此外,拥有一个管理良好的GitHub master分支和一个Pipfile,使Heroku的服务器在很少的故障排除的情况下即可重建我们的应用程序。 这使我们可以在几分钟内将应用程序从GitHub上的项目目录部署到Heroku。

We hope this guide has been helpful and welcome comments and questions, thank you!

希望本指南对您有所帮助,并欢迎提出意见和问题,谢谢!

翻译自: https://towardsdatascience.com/deploy-a-flask-app-on-heroku-and-connect-it-to-a-jawsdb-mysql-database-10e762bc9160

heroku搭建mysql

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值