在 Azure 上现代化 Python 应用和数据(第二部分):迁移数据





5.00/5 (3投票s)
在本文中,我们将演示如何设置 Azure PostgreSQL 数据库,将应用数据迁移到 Azure,并确保应用程序仍然正常工作。
在本系列文章的第一篇文章中,我们学习了如何下载和运行一个基于 Python、Django REST Framework (DRF) 和 SQLite 数据库的 RealWorld 示例应用,以提供后端 REST API。我们准备了需求,然后本地安装并运行了 PostgreSQL 数据库。
然后,我们将应用修改为使用 PostgreSQL 而不是 SQLite,并通过运行迁移命令重新创建了数据库架构。最后,我们使用 Postman 填充了示例数据,并运行了一个基于 Vue 的 Web 应用来测试前端与 Django 应用的连接。
在本系列的结尾,我们将实现 Python 应用的现代化并在 Azure 上发布它。但在我们迁移应用程序之前,首先需要迁移其数据。
在本文中,我们将配置一个 Azure PostgreSQL 数据库,将本地 PostgreSQL 数据库迁移到 Azure Database for PostgreSQL,并将我们的本地应用连接到我们的云数据库。然后,我们将本地运行 Django 应用,同时连接到 Azure Postgres 数据库而不是本地数据库,以确保一切正常。
您可以按照本文中的步骤来运行您的应用程序,或者下载并打开此GitHub 存储库文件夹,查看第二部分结束时的代码。
配置 Azure Database for PostgreSQL
Azure Database for PostgreSQL 是 Microsoft 云中的一种关系数据库服务,基于PostgreSQL Community Edition数据库引擎。当您从本地 PostgreSQL 数据库迁移到 Azure Database for PostgreSQL 时,您可以获得许多优势,包括内置的高可用性、自动备份、自动化维护、快速弹性扩展、监控和支持。
在本系列文章中,我们将使用 Single Server:一种完全托管的 Azure Database for PostgreSQL 服务,对数据库自定义的需求最少。Single Server 平台只需最少льзова配置即可处理大多数数据库管理功能。
让我们回顾一下开始在 Azure 云中使用 PostgreSQL 所需的准备工作。
首先,请访问 Azure Portal 教程“设计 Azure Database for PostgreSQL - Single Server”,并执行以下步骤:
- 如果还没有订阅,请按照先决条件部分创建一个免费 Azure 帐户。
- 创建 Azure Database for PostgreSQL。本节将指导您完成创建新资源、选择 Single Server 部署选项,以及最终提供创建 Azure Database for PostgreSQL 所需的基本信息。
- 配置服务器级防火墙规则。本节至关重要,因为 Azure Database for PostgreSQL 服务使用服务器级防火墙。默认情况下,此防火墙会阻止所有外部应用程序和工具连接到服务器和任何数据库,除非您创建防火墙规则以允许特定 IP 地址范围访问防火墙。
- 获取连接信息。本节将帮助您提供主机信息,包括服务器名称、服务器管理员登录名以及您刚刚创建的 PostgreSQL 服务器的 Azure 数据库访问凭据。
- 使用 psql 连接到 PostgreSQL 数据库。如果您的客户端计算机已安装 PostgreSQL,您可以使用本地的 psql 实例或 Azure Cloud 控制台连接到 Azure PostgreSQL 服务器。本节将指导您使用本地的 psql CLI 连接到基于云的 PostgreSQL 服务器并发出 SQL 查询。
迁移应用程序数据到 Azure
我们需要两个 PostgreSQL 实用程序来从本地数据库迁移到云:psql 和 pg_dump。
psql 实用程序是 PostgreSQL 的终端模式客户端。它使我们能够交互式地输入命令,将其提交给 PostgreSQL,并查看结果。pg_dump 是一个用于执行 PostgreSQL 备份操作的实用程序。
要使用这些实用程序,请打开终端并将路径更改为本地 PostgreSQL 安装的 \bin 目录,其中包含二进制文件。在 Windows 系统中,通常可以在 C:\Program Files\PostgreSQL\14\bin 目录中找到它们。
首先,生成一个脚本以从本地数据库 PostgreSQL 提取架构。此架构包括表、列、索引、外键和其他数据库约束。
接下来,运行以下 pg_dump 命令。它连接到 127.0.0.1 主机上的本地 conduit_db
数据库,使用 conduit_user
用户。然后,它将架构生成到 conduit_db_schema.out
文件。
pg_dump -h 127.0.0.1 -U conduit_user -d conduit_db > conduit_db_schema.out
然后,仅将数据移动到一个新的纯文本 conduit_db_data.out 文件。您可以使用 --data-only
选项来转储数据而不包含架构(数据定义)。
pg_dump -h 127.0.0.1 -U conduit_user -d conduit_db --data-only > conduit_db_data.out
从现在开始,我们必须使用创建云数据库时提供的凭据。因此,请转到Azure 门户并导航到您的 Azure Database for PostgreSQL 服务器,以收集主机、用户和数据库信息。将此信息复制到记事本。
我们将使用基于终端的 psql 实用程序来交互式地发出查询和命令。在这种情况下,请连接到您 Azure 服务器主机上的远程 Postgres 数据库。
psql -h mydemoserver-********.postgres.database.azure.com -U myadmin@mydemoserver- ******** -d postgres
接下来,发出命令以删除(如果存在)conduit_db
数据库,并从头开始重新创建它。
postgres> DROP DATABASE IF EXISTS conduit_db;
postgres> CREATE DATABASE conduit_db;
然后,再次连接到 Azure 上的 PostgreSQL 数据库,以从 conduit_db_schema.out 导入架构。
psql -h mydemoserver-********.postgres.database.azure.com -U myadmin@mydemoserver- ******** -d conduit_db -s < conduit_db_schema.out
最后,我们连接到 Azure 数据库,以仅从 conduit_db.out 文件迁移数据。
psql -h mydemoserver-********.postgres.database.azure.com -U myadmin@mydemoserver- ******** -d conduit_db < conduit_db_data.out
此时,您的 Azure Database for Postgres 远程数据库将拥有与您本地 PostgreSQL 数据库完全相同的副本。
接下来,让我们配置我们的 Django API Web 应用程序,使其连接到 Azure Postgres 数据库而不是本地数据库。
切换到 Azure Database for PostgreSQL
要使用主机、数据库、用户和密码进行连接,请在 Django 应用的数据库配置中指定这些设置。您需要修改 \conduit\settings.py 文件中的 DATABASES
常量,使其指向 Azure Database for PostgreSQL 而不是您的本地数据库。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'conduit_db',
'USER': 'myadmin@mydemoserver-********',
'PASSWORD': '************',
'HOST': 'mydemoserver-********.postgres.database.azure.com',
'PORT': '5432'
}
确保应用程序与 Azure Database for PostgreSQL 正常工作
要确保应用程序与新数据库正常工作,请执行 runserver
命令来运行应用程序。
> python manage.py runserver
Django version 1.10.5, using settings 'conduit.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
然后,在浏览器中打开 http://127.0.0.1:8000/api/ URL,以确认后端工作正常。
最后,访问 http://127.0.0.1:8000/api/articles,以验证数据库架构和包含文章的数据已成功迁移到 Azure Database for PostgreSQL。
{
"articlesCount": 3,
"articles": [
{
"author": {
"username": "alice_smith",
"bio": "",
"image": "https://static.productionready.io/images/smiley-cyrus.jpg",
"following": false
},
"body": "Share your knowledge and empower the community by creating a new implementation",
"createdAt": "2022-01-15T15:43:40.112921+00:00",
"description": "join the community by creating a new implementation",
"favorited": false,
"favoritesCount": 0,
"slug": "create-a-new-implementation-ukwcnm",
"tagList": [
"implementations"
],
"title": "AZURE Create a new implementation",
"updatedAt": "2022-01-15T15:43:40.112921+00:00"
},
{
"author": {
"username": "alice_smith",
"bio": "",
"image": "https://static.productionready.io/images/smiley-cyrus.jpg",
"following": false
},
"body": "Over 100 implementations have been created using various languages, libraries, and frameworks.\n\nExplore them on CodebaseShow.",
"createdAt": "2022-01-15T15:43:39.369865+00:00",
"description": "discover the implementations created by the RealWorld community",
"favorited": false,
"favoritesCount": 0,
"slug": "explore-implementations-h9h4zn",
"tagList": [
"implementations",
"codebaseShow"
],
"title": "AZURE Explore implementations",
"updatedAt": "2022-01-15T15:43:39.369865+00:00"
},
{
"author": {
"username": "alice_smith",
"bio": "",
"image": "https://static.productionready.io/images/smiley-cyrus.jpg",
"following": false
},
"body": "See how the exact same Medium.com clone (called Conduit) is built using different frontends and backends. Yes, you can mix and match them, because they all adhere to the same API spec",
"createdAt": "2022-01-15T15:43:38.688173+00:00",
"description": "Exemplary fullstack Medium.com clone powered by React, Angular, Node, Django, and many more",
"favorited": false,
"favoritesCount": 0,
"slug": "welcome-to-realworld-project-dgroov",
"tagList": [
"introduction",
"welcome"
],
"title": "AZURE Welcome to RealWorld project",
"updatedAt": "2022-01-15T15:43:38.688173+00:00"
}
]
}
后续步骤
我们已经讨论了如何设置 Azure PostgreSQL 数据库、将应用程序数据迁移到 Azure,以及确保应用程序仍然正常工作。最后,我们演示了如何将应用程序配置为连接到新的数据源,以便应用程序在连接到 Azure Postgres 数据库的情况下本地运行。
下一篇文章将演示如何将我们的 Python 应用和数据的副本迁移到 Azure 云基础设施,仅进行最少的重新设计和修改。
要了解更多关于使用 Visual Studio Code 将容器映像从容器注册表部署到 Azure App Service 的信息,请参阅我们的教程:使用 Visual Studio Code 将 Docker 容器部署到 Azure App Service。