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”,如下所示。

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上,部署一个新的应用程序以查看下面的页面。

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,输入存储库的名称,然后点击搜索。 用户名和存储库出现后,单击“连接”。 然后选择所需的分支,然后单击部署。

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才能进行此构建。 再一次将这些文件放在项目的根目录。

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

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

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_requestdef 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分支。

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


http://www.taodudu.cc/news/show-6005179.html

相关文章:

  • 动态规划之0-1背包问题(思路详解+表格演示过程+最优解打印方法+详细代码)
  • 《时间手账》读后感
  • 个人随心手账
  • 【Android】多彩手账——一个多媒体手账APP
  • chronodex怎么用_手账技巧| 时间饼Chronodex的用法经验、使用心得和灵感收集
  • 商业智能,数据仓库,ETL,数仓调度工具informatica介绍手账(一)
  • 商业智能,数据仓库,ETL,数仓调度工具informatica介绍手账(三)
  • 商业智能,数据仓库,ETL,数仓调度工具informatica介绍手账(二)
  • 2021-07-27 百度飞桨AI达人创造营手账
  • pythonfor循环功能手工皂_轻松领悟for循环,做一款Python版手账
  • 微信小程序开发手账从入门到部署【持续更新】
  • 康复期需注意什么?一组手账告诉你
  • 康宇的OJ愚人手账1
  • Notion为什么能让我放弃手账
  • tinode客户端安卓版编译手账
  • Minecraft Server 搭建手账
  • 国际汇率兑换接口
  • 调用百度汇率api 获取各国的汇率值
  • MM 固定汇率的使用在MIRO的问题
  • 【练习/Python】监测汇率脚本
  • 超强大的货币汇率实时查询工具
  • Mac 下使用curl解决迅雷不能使用离线下载的问题
  • 迅雷面试题: 模拟银行一天的工作, 统计vip客户和普通客户的办理业务的平均等待时间.
  • 怎么AI生成绘画图片?试试这几款软件吧
  • 三分钟告诉你怎么AI生成绘画图片
  • 图片的绘画器
  • qt绘画事件-设置背景图片
  • Android处理图片透明度并绘画图片
  • 路线图绘画系统
  • AI绘画绘图流量主小程序开发

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

  1. 怎么抓python程序的包_如何在AWS上部署python应用程序

    如何在AWS上部署python应用程序,学姐呕心沥血亲自总结,亲测有效,比网上看网上大把大把的文档要快得多! 作者:蕾学姐 亚马逊云计算服务(Amazon Web Services,缩写为AWS),由 ...

  2. Asp.Net Web Api 部署------在云服务器IIS上部署Web Api程序

    Asp.Net Web Api 部署------在云服务器IIS上部署Web Api程序 本人Web Api程序框架选择的是.Net 5.0 一.在服务器上安装运行时RunTime,.Net 5 运行 ...

  3. php和android和mysql_如何使用JSON连接Android和PHP Mysql数据库

    我们先来看一个简单的Android app例子(这里是一个商品存货清单项目),在Android程序中,我们可以访问(call)PHP脚本来执行简单的CRUD操作(创建,读取,更新,删除).为了使你对它 ...

  4. 在Ngnix上部署Flask应用

    <Flask项目支持https>这一篇介绍的是在Flask自带server上添加SSL,毕竟不适合用于生产环境.本篇介绍适用于生产环境的Flask+SSL部署方法,需要借助Ngnix. F ...

  5. 使用 uWSGI 和 Nginx 部署 Flask 应用程序

    自从我第一次浪漫地接触 Python 已经过去了大约十年.这部特别的浪漫喜剧不知何故完全错过了"一见钟情"的比喻--我发现学习 Python 的过程令人愤怒.我担心的不是语言本身, ...

  6. 入门实践丨如何在K3s上部署Web应用程序

    在本文中,我们将使用Flask和JavaScript编写的.带有MongoDB数据库的TODO应用程序,并学习如何将其部署到Kubernetes上.这篇文章是针对初学者的,如果你之前没有深度接触过Ku ...

  7. openshift java_在OpenShift上部署Java应用程序的快速指南

    在本文中,我将向您展示如何在OpenShift(Minishift)上部署应用程序,将它们与其中暴露的其他服务连接起来,还是使用由OpenShift提供的其他一些有趣的部署功能.OpenShift构建 ...

  8. 前端项目线上环境搭建(Nginx)和线上部署(推荐)

    本次主要讲解的是服务器上node环境的搭建,以及vue/react/等的项目的部署,作为一名前端,一定要学会哦. 购买云服务器/主机 云服务器:阿里云.百度云.新浪云.腾讯云 主机:不建议购买,共享的 ...

  9. k8s php mysql_在k8s上部署第一个php应用

    一.搭建nginx+php 1.站点配置文件 1.1创建nginx-configmap.yaml [root@master k8s]# cat nginx-configmap.yaml apiVers ...

最新文章

  1. 一文回顾深度学习发展史上最重要经典模型
  2. MySQLFabric概述
  3. SSH port forwarding: bind: Cannot assign requested address
  4. 处理数字_6_NULL值的列的个数
  5. 2014年江苏省计算机二级c语言考试大纲,「二级C语言」江苏省计算机二级VFP考试大纲...
  6. 远程网络安装RHEL5
  7. 第六届中国云计算大会详细日程
  8. .Net Discovery系列之三 深入理解.Net垃圾收集机制(上)
  9. 用金蝶kis记账王批量审核会计凭证的方法
  10. formatter function (value,row,index){} 参数的含义
  11. 使用 Clang Tools —— ClangFormat
  12. sqluldr2支持mysql吗_sqluldr2工具使用方法
  13. 【2019秋招】OPPO无线通信协议工程师笔试
  14. 关于Pyrene-PEG2/PEG3/PEG4/PEG5-azide化学式,分子量等相关对比总结
  15. python自学1.2-运算符和表达式
  16. WeChatTweak-微信小助手 v1.2.2 详细安装教程
  17. python 获取图像亮度和锐度,pytesseract 获取图片上字符串,及增加亮度,色感,对比度,锐度...
  18. 深入理解MVCC实现原理以及当前读和快照读存在的问题
  19. 计算机如何恢复桌面,如何恢复计算机桌面图标不见了
  20. 2019 年百度之星·程序设计大赛 - 初赛一 1003 Mindis

热门文章

  1. 微型计算机的英文术语,计算机常见英语词汇解释
  2. Hudi(12):Hudi集成Flink之sql-client方式
  3. VRRP主备网关原理
  4. 实战之从阿里云dataworks的maxcomputer中导出数据
  5. 关于地图开发的那些坑儿
  6. 准备情人节礼物比写代码难?来看看IT直男给女友们的礼物
  7. Linux下RTL8723BE无线网卡驱动问题解决方法
  8. SpringCloud禁用Eureka自我保护模式
  9. 最新最全论文合集——纵向联邦学习
  10. 怎么设置计算机桌面一键关机,电脑一键关机按钮创建 教你在桌面一点就自动关机...