使用 PostgreSQL 预配 OCI 数据库,并使用 OCI GoldenGate 从内部部署进行迁移,尽可能缩短停机时间
简介
Oracle Cloud Infrastructure Database with PostgreSQL(OCI Database with PostgreSQL) 是完全托管的 PostgreSQL 兼容服务,通过数据库优化存储提供 3 倍的性能,与 Amazon Web Service (AWS) 相比成本高出一半,可提供自动备份、高可用性、调度维护、动态存储扩展、垂直扩展等功能。
示例:业务连续性
OCI Database with PostgreSQL 服务提供 99.99% 的正常运行时间服务级别协议 (Service Level Agreement,SLA)、多节点数据库系统的恢复时间目标 (Recovery Time Objective,RTO) 小于 2 分钟,恢复点目标 (Recovery Point Objective,RPO) 为 0,且不会丢失数据。对于单可用性域 (AD) 区域中的单节点数据库系统,该服务可提供 99.9% 的正常运行时间 SLA、不到 20 分钟的 RTO 和 0 的 RPO。
OCI Database with PostgreSQL 利用 OCI Block Volumes 来存储用户数据。因此,适用相同的持久性、安全性和性能保证。有关详细信息,请参阅 OCI Database with PostgreSQL 和 First Principles:Optimizing PostgreSQL for the Cloud 。
本教程重点介绍如何使用 OCI GoldenGate 将本地 PostgreSQL 数据库迁移到 OCI Database(使用 PostgreSQL),从而尽可能减少停机时间。
环境详细信息:
- OCI GoldenGate 控制台用户名:
oggadmin
。 - OCI GoldenGate 版本:
23ai
。 - OCI 数据库,具有 PostgreSQL 管理员名称:
admin
。
源内部部署 | 目标 OCI | |
---|---|---|
数据库名称 | product_inventory | 目标数据库 |
User | 订单经理 | admin |
方案 | data_analyze | data_analyze |
数据库版本 | 15.12 | 15.6 |
目标
-
使用 PostgreSQL 数据库系统创建 OCI 数据库。
-
预配 OCI GoldenGate 部署。
-
在部署时创建内部部署数据库和 OCI 数据库连接并分配连接。
-
准备源数据库服务器和数据库。
-
创建初始装入提取。
-
使用
pg_dump
实用程序备份源数据库元数据。 -
使用 PostgreSQL 将元数据导入 OCI 数据库。
-
使用线索文件创建初始加载复制。
-
创建联机或更改数据捕获 (Change Data Capture,CDC) 提取,以更改为要捕获的起始日志序列号 (Log Sequence Number,LSN)。
-
创建在线复制捐赠 CDC 提取线索文件。
任务 1:使用 PostgreSQL 数据库系统预配 OCI 数据库
此任务说明如何在预配之前创建数据库系统以及需要以下策略。有关更多信息,请参见 Managing a Database System 。
-
添加以下 Oracle Cloud Infrastructure Identity and Access Management (IAM) 策略。
Allow group <postgresql-admin-group> to read compartments in tenancy Allow group <postgresql-admin-group> to manage postgres-db-systems in [ tenancy | compartment <compartment_name> | compartment id <compartment_OCID> ] Allow group <postgresql-admin-group> to manage postgres-backups in [ tenancy | compartment <compartment_name> | compartment id <compartment_OCID> ] Allow group <postgresql-admin-group> to read postgres-work-requests in [ tenancy | compartment <compartment_name> | compartment id <compartment_OCID> ] Allow group <postgresql-admin-group> to manage postgres-configuration in [ tenancy | compartment <compartment_name> | compartment id <compartment_OCID> ] Allow group <postgresql-admin-group> to manage virtual-network-family in [ tenancy | compartment <compartment_name> | compartment id <compartment_OCID> ] Allow group <postgresql-admin-group> to read secret-family in [ tenancy | compartment <compartment_name> | compartment id <compartment_OCID> ] Allow group <postgresql-admin-group> to read vaults in [ tenancy | compartment <compartment_name> | compartment id <compartment_OCID> ] Allow group <postgresql-admin-group> to read metrics in [ tenancy | compartment <compartment_name> | compartment id <compartment_OCID> ] Allow group <postgresql-admin-group> to use tag-namespaces in tenancy
-
为数据库管理员密码创建或选择 Vault。有关更多信息,请参见 Managing Vaults 。
-
为数据库密码创建密钥。有关更多信息,请参见 Managing Keys 。
-
为数据库密码创建密钥。有关详细信息,请参阅管理 Vault 密钥。
-
登录 OCI 控制台,导航到 Database(数据库)、PostgreSQL 、 DB systems(数据库系统)、 Create new database system(创建新数据库系统)并单击 Next(下一步)。
-
选择 Hardware configuration 。
-
选择 Virtual Cloud Network(虚拟云网络)和 Subnet(子网),然后输入 Database system administrator Credentials(数据库系统管理员身份证明)。在此示例中, Username 将是 admin 。
创建完成后,数据库系统将显示 Active 状态。
-
运行以下命令以验证从计算节点使用 PostgreSQL 与 OCI 数据库的连接。
-bash-4.2$ psql "sslmode=verify-full sslrootcert=c.pub host=<OCI DB server ip address> user=admin dbname=postgres" Password for user admin: psql (15.12, server 15.6) SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, compression: off) Type "help" for help. postgres=>
任务 2:创建 OCI GoldenGate 部署以进行迁移
-
转到 OCI 控制台,导航到 Oracle Database 、GoldenGate 、 Deployments(部署),然后单击创建部署。
-
输入在以下映像中指定的必需信息,然后选择 PostgreSQL 作为选择技术。
-
在配置部署中,使用
oggadmin
作为用户名以管理员身份登录到 OCI GoldenGate 控制台。 -
单击创建以开始创建和部署。成功完成后,它将显示 Active 状态。
任务 3:在部署和分配连接时创建内部部署数据库连接、OCI 数据库连接
OCI GoldenGate 需要源数据库和目标数据库连接,并将这两个连接都分配给部署。
-
创建源数据库连接。
-
单击部署名称 (
PG_Mig_deployment
),选择连接,然后单击创建连接。输入必需的信息,如以下图像所示。 -
选择 Type(类型)作为 PostgreSQL Server(服务器),选择 Security Protocol(安全协议)作为 Plain(普通),然后单击 Create(创建)。
-
-
创建目标数据库连接。
-
单击部署名称,选择连接,然后单击创建连接。输入必需的信息,如以下图像所示。
-
选择 Type(类型)作为 OCI PostgreSQL 。
-
选择 Security Protocol 作为 TLS ,选择 SSL Mode 作为 Require 。
-
-
将数据库连接分配给部署。
-
转到部署,单击部署名称 (
PG_Mig_deployment
),选择分配连接,然后单击分配连接。 -
输入必需的信息,如以下图像所示。
分配连接完成后,您将看到 Active 状态。
-
任务 4:准备源数据库服务器和数据库
-
安装
postgresql15-contrib
软件包。OCI GoldenGate 需要源数据库服务器上的
postgresql14-contrib
程序包才能从 PostgreSQL 服务器数据库提取数据。可以使用sudo yum install postgresql15-contrib
命令安装软件包(源数据库版本为 15)。注:建议客户机库的版本应与 PostgreSQL 数据库版本匹配。不支持低于 10 的客户端版本。
-
准备在源数据库上创建数据库用户以及所需的权限。
product_inventory=# create user ggreplication with password 'abc1234'; ERROR: role "ggreplication" already exists product_inventory=# product_inventory=# select current_database(); current_database ------------------- product_inventory (1 row) product_inventory=# GRANT CONNECT ON DATABASE product_inventory TO ggreplication; GRANT product_inventory=# ALTER USER ggreplication WITH REPLICATION; ALTER ROLE product_inventory=# ALTER USER ggreplication WITH SUPERUSER; ALTER ROLE product_inventory=# GRANT USAGE ON SCHEMA ordermanager TO ggreplication; GRANT product_inventory=# GRANT USAGE ON SCHEMA data_analyze TO ggreplication; GRANT product_inventory=# GRANT SELECT ON ALL TABLES IN SCHEMA ordermanager TO ggreplication; GRANT product_inventory=# GRANT SELECT ON ALL TABLES IN SCHEMA data_analyze TO ggreplication; GRANT product_inventory=# GRANT CREATE ON DATABASE product_inventory TO ggreplication; GRANT product_inventory=# GRANT CREATE, USAGE ON SCHEMA ordermanager TO ggreplication; GRANT product_inventory=# GRANT CREATE, USAGE ON SCHEMA data_analyze TO ggreplication; GRANT product_inventory=# GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA ordermanager TO ggreplication; GRANT product_inventory=# GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA data_analyze TO ggreplication; GRANT product_inventory=# GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA ordermanager TO ggreplication; GRANT product_inventory=# GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA data_analyze TO ggreplication; GRANT product_inventory=#
有关每个特权的特定用途的更多信息,请参见 Prepare Database Users and Privileges for PostgreSQL 。
-
下面是 PostgreSQL 数据库配置文件中的参数。配置文件的位置为
$PGDATA/postgresql.conf
。wal_level = logical **# set to logical for Capture** max_replication_slots = 1 **# max number of replication and one slot for Extract/Client** max_wal_senders = 1 **# one sender per max repl slot** track_commit_timestamp = on **# optional, correlates tx commit time** with begin tx log record (useful for # timestamp- based positioning)
注:
- 进行上述任何更改后,重新启动数据库。
- OCI GoldenGate 复制支持 PostgreSQL 数据类型和受支持的数据类型、受支持对象和操作限制,可从文档中验证。有关更多信息,请参阅准备数据库。
任务 5:创建初始加载提取
我们需要创建初始加载提取,这将提供 OCI GoldenGate 提取报告部分上的 LSN。
-
转到 OCI 控制台,导航到 Database(数据库)、GoldenGate 、 Deployments(部署)并单击在任务 2 中创建的部署名称。
-
单击启动控制台并输入登录身份证明。
-
转到 DB Connections(DB 连接),这将显示分配的源数据库和具有 PostgreSQL 数据库连接的 OCI 数据库,并验证连接检查。
成功的连接有助于在此阶段开始初始加载。
-
转到 OCI GoldenGate 控制台,导航到提取,然后单击 + 。
初始加载提取停止后,验证 OCI GoldenGate 报告文件并记下 LSN 详细信息,并使其在任务 9 中易于使用。
任务 6:使用 pg_dump
命令备份源数据库元数据
pg_dump
命令仅用于转储源数据库 product_inventory
元数据。
pg_dump -h localhost -U ggreplication -d product_inventory -F c -v -s --file=/tmp/exp_source_db.dump
任务 7:使用 PostgreSQL 将元数据导入 OCI 数据库
使用 pg_restore 二进制文件通过 PostgreSQL 导入到目标 OCI 数据库,此处的 OCI 数据库名称为 targetdb
。
从 OCI 计算节点运行还原命令,在此节点中,我们在任务 1.8 中成功执行了连接命令。
pg_restore -h <OCI IP address> -U admin -d targetdb -v exp_source_db.dump
postgres=> \c targetdb
psql (15.12, server 15.6)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, compression: off)
You are now connected to database "targetdb" as user "admin".
targetdb=> \dn
List of schemas
Name | Owner
--------------+-------------------
data_analyze | admin
ordermanager | admin
public | pg_database_owner
(3 rows)
注:在目标上导入元数据后,使用
select table_name,table_schema from information_schema.tables where table_type='BASE TABLE' and table_schema in ('data_analyze','ordermanager') order by table_schema,table_name;
查询验证表名。
任务 8:使用线索文件创建初始加载复制
-
转到 OCI GoldenGate 控制台,导航到 Replicats ,然后单击 + 创建复制。
-
单击创建并运行以启动初始加载复制,完成后,验证表行计数。
-
转到 Reports(报表)并验证初始装入的 "Replicate"(复制)报表。验证两个数据库中的少量表行计数。
任务 9:创建用于捕获的 CDC 提取变更到起始 LSN
CDC Extract 是用于捕获现有数据库持续更改的联机提取,CDC Extract 从收集的 LSN 编号开始捕获。
-
转到 OCI GoldenGate 控制台,导航到提取,单击 + 并输入所需的信息,如以下映像所示。
-
注册并创建 CDC 后,单击开始使用选项以指示从任务 5 中收集的特定 LSN 进行读取。
我们可以在一段时间后看到 CDC 提取物的零秒滞后。
任务 10:创建联机复制,提供 CDC 提取线索文件
-
转到 OCI GoldenGate 控制台,导航到 Replicats ,单击 + 并输入所需的信息,如以下映像所示。
-
修改
MAP
命令后,单击 Create and Run 。我们可以在一段时间后在联机复制上看到零秒的滞后。
验证
运行以下查询以插入到源表 data_analyze.table3
复制到目标数据库 data_analyze.table3
表中。
目标端验证表计数和数据。
相关链接
-
使用 OCI GoldenGate 通过 PostgreSQL 将本地 PostgreSQL 数据库无缝迁移到 OCI 数据库
-
使用 OCI GoldenGate 通过 PostgreSQL 将本地 PostgreSQL 数据库迁移到 OCI 数据库
确认
- 作者 — Sivakrishna Burle(Oracle 北美云服务— NACIE 高级云工程师)
更多学习资源
通过 docs.oracle.com/learn 浏览其他实验室,或者通过 Oracle Learning YouTube 频道访问更多免费学习内容。此外,请访问 education.oracle.com/learning-explorer 以成为 Oracle Learning Explorer。
有关产品文档,请访问 Oracle 帮助中心。
Provision OCI Database with PostgreSQL and Migrate from On-Premises with Minimum Downtime using OCI GoldenGate
G39916-02
Copyright ©2025, Oracle and/or its affiliates.