heroku搭建mysql_在heroku上部署Flask应用程序并将其连接到颚数据库mysql数据库
heroku搭建mysql
By: Edward Krueger Data Scientist and Instructor and Douglas Franklin Teaching Assistant and Technical Writer.
作者: 愛德華·克魯格(Edward Krueger)數(shù)據(jù)科學(xué)家和講師, 道格拉斯·富蘭克林 ( Douglas Franklin)教學(xué)助理和技術(shù)作家。
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的應(yīng)用程序部署到云上以及如何將該應(yīng)用程序連接到云數(shù)據(jù)庫。 有關(guān)虛擬環(huán)境或環(huán)境和程序包管理器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.
由于缺乏對虛擬環(huán)境的了解或經(jīng)驗,較新的開發(fā)人員通常在系統(tǒng)級別安裝所有內(nèi)容。 與pip一起安裝的Python軟件包位于系統(tǒng)級別。 以這種方式為每個項目檢索需求會在您的計算機(jī)上創(chuàng)建一個難以管理的全局Python環(huán)境。 虛擬環(huán)境使您可以劃分軟件,同時保留依賴性清單。
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是用于虛擬環(huán)境和Python軟件包管理的工具,它使開發(fā)人員能夠創(chuàng)建隔離的軟件產(chǎn)品,這些產(chǎn)品更易于部署,構(gòu)建和修改。
什么是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將軟件包管理和虛擬環(huán)境控制結(jié)合到一個工具中,用于安裝,刪除,跟蹤和記錄依賴項。 以及用于創(chuàng)建,使用和管理虛擬環(huán)境。 Pipenv本質(zhì)上是將pip和virtualenv打包到一個產(chǎn)品中。
什么是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提供了許多軟件產(chǎn)品,我們將需要Heroku云平臺服務(wù)來托管應(yīng)用程序,而JawsDB需要使用MySQL數(shù)據(jù)庫。 不用擔(dān)心,創(chuàng)建帳戶和使用這些功能是免費的!
We are going to use the Heroku GUI to deploy a database and a Python app.
我們將使用Heroku GUI部署數(shù)據(jù)庫和Python應(yīng)用程序。
云數(shù)據(jù)庫的好處 (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數(shù)據(jù)庫。 使用SQLite數(shù)據(jù)庫時,每次重新部署應(yīng)用程序都會重置您的數(shù)據(jù)庫。 Heroku的JawsDB允許我們的數(shù)據(jù)通過應(yīng)用程序更新保持不變。 此外,使用JawsDB可以為您完成托管,配置,修補(bǔ)和管理數(shù)據(jù)庫。
準(zhǔn)備部署 (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分支部署應(yīng)用程序。 將Pipfile推送到GitHub后,我們就可以正常工作了,我們準(zhǔn)備對存儲庫進(jìn)行一些最終更改,以準(zhǔn)備進(jìn)行部署。 確保將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.
注意:接下來的這些更改使我們的應(yīng)用程序可以在Unix系統(tǒng)上運行。 Gunicorn與PC不兼容,因此,如果您未使用Linux或Unix計算機(jī),則將無法在本地測試這些更改。
安裝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服務(wù)器,將在Heroku上為Flask應(yīng)用程序提供服務(wù)。 通過運行以下行,您可以將gunicorn添加到Pipfile中。
pipenv install gunicorn添加一個Procfile (Add a Procfile)
Create a Procfile in the project root folder and add the following line:
在項目根文件夾中創(chuàng)建一個Procfile并添加以下行:
web: gunicorn app:appThe 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代表運行您的應(yīng)用程序的python文件的名稱或該應(yīng)用程序所在的模塊的名稱。 第二個代表您的應(yīng)用名稱,即app.py。 該P(yáng)rocfile可與gunicorn和Heroku的Dynos一起使用,以遠(yuǎn)程服務(wù)您的應(yīng)用程序。
Heroku云數(shù)據(jù)庫的設(shè)置和部署 (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,請不要擔(dān)心,我們在此顯示的所有功能都是免費的!
Go to your app on Heroku.com and click resources. Then type “JawsDB MySQL” into the addons box, as seen below.
轉(zhuǎn)到Heroku.com上的應(yīng)用程序,然后單擊資源。 然后在插件框中鍵入“ 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.
選擇免費版本,然后單擊設(shè)置。 太好了,我們現(xiàn)在為我們的應(yīng)用程序部署了一個MySQL數(shù)據(jù)庫。 接下來,我們需要將此新數(shù)據(jù)庫集成到我們的應(yīng)用程序邏輯中。
First, let’s add Pymysql, a Python SQL library, to our Pipfile with the following.
首先,使用以下代碼將Pymysql(Python SQL庫)添加到我們的Pipfile中。
pipenv install pymysqlNow 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.
現(xiàn)在,讓我們獲取連接字符串并為pymysql對其進(jìn)行修改。 轉(zhuǎn)到設(shè)置,然后查看配置變量。 您會發(fā)現(xiàn)類似于以下內(nèi)容的連接字符串。
mysql://ael7qci22z1qwer:nn9keetiyertrwdf@c584asdfgjnm02sk.cbetxkdfhwsb.us-east-1.rds.amazonaws.com:3306/fq14casdf1rb3y3nWe need to make a change to the DB connection string so that it uses the Pymysql driver.
我們需要更改數(shù)據(jù)庫連接字符串,以便它使用Pymysql驅(qū)動程序。
In a text editor, remove the mysql and add in its place mysql+pymysql and then save the updated string.
在文本編輯器中,刪除mysql并在其位置添加mysql+pymysql ,然后保存更新的字符串。
mysql+pymysqlYou’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上的配置變量中。 為此,請轉(zhuǎn)到設(shè)置,然后配置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.
創(chuàng)建一個名為.env的新文件,并將云數(shù)據(jù)庫的連接字符串添加為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使我們可以訪問這些隱藏的環(huán)境變量。 同樣,我們可以使用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文件中,以便它可以連接到云!
應(yīng)用部署 (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.
一旦我們的應(yīng)用程序經(jīng)過測試并在本地工作,我們便將所有代碼推送到master分支。 然后在Heroku上,部署一個新的應(yīng)用程序以查看下面的頁面。
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,輸入存儲庫的名稱,然后點擊搜索。 用戶名和存儲庫出現(xiàn)后,單擊“連接”。 然后選擇所需的分支,然后單擊部署。
Select master and deploy the branch 選擇主節(jié)點并部署分支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.
構(gòu)建日志將開始在頁面上填充控制臺。 請注意,Heroku首先會查找require.txt文件,然后從Pipenv的Pipfile.lock安裝依賴項。 如果您不使用Pipenv,則需要具有requiremnts.txt才能進(jìn)行此構(gòu)建。 再一次將這些文件放在項目的根目錄。
Once your environment has been built from the Pipfile.lock and the build is successful, you will see the below message.
從Pipfile.lock構(gòu)建環(huán)境并且構(gòu)建成功后,您將看到以下消息。
Successful app deployment 成功部署應(yīng)用The app is successfully deployed! Click to view button to see the deployed app on Heroku.
該應(yīng)用已成功部署! 單擊查看按鈕以查看Heroku上已部署的應(yīng)用程序。
最初建立表格 (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.
在嘗試獲取或發(fā)布數(shù)據(jù)之前,可能尚未創(chuàng)建表的地方可能會遇到錯誤。 為了解決這個問題,我們在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主服務(wù)器的更改在推送時顯示在Heroku上。 如果使用此方法,則需要確保始終有一個正常的master分支。
Enable automatic deploys 啟用自動部署結(jié)論 (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.
編碼和構(gòu)建有用的軟件需要管理復(fù)雜性。 我們討論了Github作為版本控制工具,Pipenv作為環(huán)境和程序包管理器,以及讓云公司管理您的數(shù)據(jù)庫的一些好處。 這些工具有助于降低構(gòu)建軟件的復(fù)雜性,因此開發(fā)人員可以專注于構(gòu)建而不是管理。
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!
對于希望在自己的產(chǎn)品中部署,構(gòu)建或使用其代碼的數(shù)據(jù)科學(xué)家和開發(fā)人員而言,正確的環(huán)境和程序包管理至關(guān)重要。 使用諸如Pipenv之類的環(huán)境和程序包管理器,可以使許多過程(包括部署)更加舒適和高效!
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的服務(wù)器在很少的故障排除的情況下即可重建我們的應(yīng)用程序。 這使我們可以在幾分鐘內(nèi)將應(yīng)用程序從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
總結(jié)
以上是生活随笔為你收集整理的heroku搭建mysql_在heroku上部署Flask应用程序并将其连接到颚数据库mysql数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 家用空气净化器除甲醛什么品牌好 能除甲醛
- 下一篇: nginx proxy_pass转发规则