注意:

使用 OCI GoldenGate 将本地 MySQL Database 迁移到 Oracle HeatWave MySQL 托管实例

简介

Oracle Cloud Infrastructure GoldenGate (OCI GoldenGate) 是一项完全托管的服务,可帮助数据工程师实时、大规模地将数据从一个或多个数据管理系统迁移到 OCI 数据库。在单一界面中设计、运行、编排和监视数据复制任务,而无需分配或管理任何计算环境。OCI GoldenGate 支持多个源和目标,包括 MySQL 和 Oracle HeatWave MySQL 数据库服务。

在本教程中,我们将指导您如何使用 OCI GoldenGate 将内部部署 MySQL 数据库迁移到 Oracle HeatWave MySQL 托管实例。

目标

先决条件

限制

任务 1:创建源和目标 MySQL 实例

任务 2:测试堡垒、内部部署与 Oracle Heatwave MySQL 实例之间的连接

  1. 在 OCI 堡垒主机上安装 MySQL 客户端和 MySQL Shell 实用程序。

  2. 测试从 OCI 堡垒到源内部部署 MySQL 实例的连接。

    [root@bastion ~]#  mysql --host <sourceIP> -uadmin -p
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 15
    Server version: 5.7.44 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2025, Oracle and/or its affiliates.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    4 rows in set (0.00 sec)
    
    mysql> select @@version;
    +-----------+
    | @@version |
    +-----------+
    | 5.7.44    |
    +-----------+
    1 row in set (0.00 sec)
    
  3. 测试从源内部部署 MySQL 到 Oracle Heatwave MySQL 托管实例的连接。

    [root@mysqlci57 ~]# mysql --host <targetIP> -u admin -p
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 108
    Server version: 8.4.4-u5-cloud MySQL Enterprise - Cloud
    
    Copyright (c) 2000, 2023, Oracle and/or its affiliates.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> select @@version;
    +----------------+
    | @@version      |
    +----------------+
    | 8.4.4-u5-cloud |
    +----------------+
    1 row in set (0.01 sec)
    

任务 3:为 OCI 创建用户 GoldenGate

任务 4:在 OCI 的源数据库中设置必需参数 GoldenGate

如果目标是独立的(不是 HA),则不必在源上启用 GTID。但是,如果目标是 HA,则强烈建议在源上打开 GTID。必须启用 binlog 模式。

  1. 编辑 /etc/my.cnf 文件并添加以下行。

    server-id=1
    log-bin=/var/log/mysql/mysql-bin.log
    max_binlog_size=100M
    binlog_format=ROW
    expire_logs_days=10
    -- binlog_row_metadata=FULL <-- this is not supported in Version 5.7.44. So DDL replication will not be possible if source is on v5.7.44.
    gtid_mode=ON
    enforce_gtid_consistency=ON
    
  2. 重新启动 MySQL 服务器。

    Systemctl stop mysqld
    Systemctl start mysqld
    Systemctl status mysqld
    

    有关完整列表或参数,请查看事务处理日志设置和要求。有关详细信息,请参阅选项 B:在 OCI 计算上使用您自己的堡垒

  3. 运行以下命令可检查二进制日志是否已启用。

    mysql> show binary logs;
    +------------------+-----------+
    | Log_name         | File_size |
    +------------------+-----------+
    | mysql-bin.000001 |       154 |
    +------------------+-----------+
    1 row in set (0.00 sec)
    
    mysql> show variables like 'bin%';
    +--------------------------------------------+--------------+
    | Variable_name                              | Value        |
    +--------------------------------------------+--------------+
    | bind_address                               | *            |
    | binlog_cache_size                          | 32768        |
    | binlog_checksum                            | CRC32        |
    | binlog_direct_non_transactional_updates    | OFF          |
    | binlog_error_action                        | ABORT_SERVER |
    | binlog_format                              | ROW          |
    | binlog_group_commit_sync_delay             | 0            |
    | binlog_group_commit_sync_no_delay_count    | 0            |
    | binlog_gtid_simple_recovery                | ON           |
    | binlog_max_flush_queue_time                | 0            |
    | binlog_order_commits                       | ON           |
    | binlog_row_image                           | FULL         |
    | binlog_rows_query_log_events               | OFF          |
    | binlog_stmt_cache_size                     | 32768        |
    | binlog_transaction_dependency_history_size | 25000        |
    | binlog_transaction_dependency_tracking     | COMMIT_ORDER |
    +--------------------------------------------+--------------+
    16 rows in set (0.01 sec)
    
  4. 运行以下命令检查 GTID 模式 (gtid_mode) 是否已打开。

    mysql> show variables like 'gtid%';
    +----------------------------------+-----------+
    | Variable_name                    | Value     |
    +----------------------------------+-----------+
    | gtid_executed_compression_period | 1000      |
    | gtid_mode                        | ON        |   <--- it is now showing ON
    | gtid_next                        | AUTOMATIC |
    | gtid_owned                       |           |
    | gtid_purged                      |           |
    +----------------------------------+-----------+
    5 rows in set (0.00 sec)
    
    mysql> select @@gtid_executed, @@gtid_purged\G
    *************************** 1. row ***************************
    @@gtid_executed:
      @@gtid_purged:
    1 row in set, 1 warning (0.00 sec)
    
    mysql> select * from mysql.gtid_executed;
    Empty set (0.00 sec)   <--- this is empty because we just turned it ON. As transactions occur this will get populated
    

注:第一个查询通常显示 GTID 的 NULL 值。这是因为在 MySQL versions 5.7 中,GTID 值仅存储在 mysql.gtid_executed 表中。

任务 5:在目标数据库中创建 airportdb 方案(仅限元数据)

  1. 从源数据库提取方案元数据。

    [root@bastion airport-db]# mysqldump --host <SourceIP> -u admin -p --no-data --routines --events airportdb > airportdb.sql
    mysqldump: [Warning] Using a password on the command line interface can be insecure.
    -- Warning: column statistics not supported by the server.
    
  2. 将方案元数据导入到目标数据库。

    [root@bastion opc]#  mysql --host <TargetIP> -u admin -p
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 581
    Server version: 8.4.4-u5-cloud MySQL Enterprise - Cloud
    
    Copyright (c) 2000, 2025, Oracle and/or its affiliates.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> create database airportdb;
    Query OK, 1 row affected (0.01 sec)
    
    mysql> use airportdb;
    Database changed
    mysql> source airportdb.sql
    

任务 6:设置 OCI GoldenGate 部署和添加连接

关于如何设置 OCI GoldenGate 部署,有几个博客和教程,因此我们将在此处显示几个步骤。

  1. 登录 OCI 控制台,然后选择 GoldenGate Deployments

  2. 单击创建部署并输入所需的信息,如下图中所示。

    图中显示了如何创建金门部署

  3. 创建部署页上添加源和目标连接详细信息。

    图中显示了如何为源创建 Golden Gate 连接

    图中显示了如何为目标创建金门连接

    图中显示了 GG 中添加的两个连接

  4. 将连接分配给 OCI GoldenGate 部署。单击连接名称,然后单击分配部署

    图中显示了如何将部署分配给连接

    图中显示了分配给部署的源连接

    图中显示了分配给部署的目标连接

  5. 使用 OCI 堡垒服务,将端口转发设置为端口 443,然后登录到 OCI GoldenGate 控制台。

    图中显示了 goldengate 主页 URL

  6. 从 OCI GoldenGate 控制台验证数据库连接。

    1. 单击“Hamburger(汉堡包)”菜单,您将在任务 6.4 中看到添加到部署的两个连接。

    2. 单击连接以验证连接。如果连接成功,您将看到通过单击 Checkpoint + 创建检查点表的选项。

      强烈建议使用 OCI GoldenGate 部署在 OCI 中添加连接,因为它将自动将 DNS 附加到 IP 地址,如果没有这些 DNS,测试连接将失败。

      图中显示了 goldengate 连接和测试连接

任务 7:创建提取和复制流程

  1. 在创建提取处理之前,请确保源数据库中的 gtid_modeON ,尤其是在目标为 HA 时。

    有两种方法可以添加提取:

    • 方法 1:您可以在初始加载(复制或转储实例)完成后添加提取并使用精确的实例化方法。有关更多信息,请参见 Precise Instantiation for MySQL to MySQL Using MySQL Shell Utilities and Oracle GoldenGate

      要添加 Extract 并启动它,请从该特定 GTID 或 binlog 进行数据捕获,如下图中所示。Oracle 建议使用此方法。

      图中显示了 goldengate Extract 设置

      注:此处 GTID 集取自 dumpInstance 的 JSON 文件或 copyInstance 命令的输出。

    • 方法 2:您甚至可以在开始初始数据加载之前添加提取,然后更改复制以从 dumpInstance() 的 JSON 文件或 copyInstance() 的输出中显示的特定 gtidexecuted/binlog# 和 binlog 位置开始。

      在此方法中使用 HANDLECOLLISION 参数以避免出现任何重复的数据问题。

      注:对于此 POC,使用了方法 2。

      编辑提取参数文件:

      EXTRACT ext1
      USERIDALIAS  MySQLCI57, DOMAIN OracleGoldenGate
      EXTTRAIL e1
      --DDL INCLUDE MAPPED  (needed for DDL replication, also need to set  binlog_row_metadata to FULL in mysql config file on source db). But not supported for MySQL V5.7
      TRANLOGOPTIONS FETCHPARTIALJSON       (for JSON replication, also need to set binlog_row_value_options to empty string in the mysql config)
      TABLE airportdb.*;
      

      注:如果将 DDL INCLUDE MAPPED 添加到“提取”参数文件,则会出现以下错误:MySQL version 5.7.4 不支持 DDL 复制。

  2. 启动“提取”处理。

    图中显示了正在运行的 Goldengate Extract

  3. 提取正在运行并生成线索文件,现在创建复制。

    1. 首先创建检查点表。转到 Configuration(配置)部分,选择目标数据库并单击 Checkpoint +(检查点 +)以创建检查点表。

      图中显示了 Goldengate 检查点表创建

    2. “创建复制”处理。由于这是单向复制,因此我们可以使用并行复制来提高性能。对于双向复制,仅支持经典复制。

      请勿启动复制进程,只需创建它。我们将在导入数据后将其启动。

      图中显示了 Goldengate 复制创建

    3. 编辑参数文件。您还可以添加 PARALLEL 参数,或者使用缺省并行。

      REPLICAT rep1
      USERIDALIAS  MySQLGG1, DOMAIN OracleGoldenGate
      MAP airportdb.*, TARGET airportdb.*;
      

任务 8:使用 copyInstance() 将数据从源复制到目标

  1. 使用 copyInstance()dryRun:"true" 选项执行预运行。

    Type '\help' or '\?' for help; '\quit' to exit.
    MySQL  SQL > \connect admin@<Source IP>
    Creating a session to 'admin@<Source IP>'
    Fetching global names for auto-completion... Press ^C to stop.
    Your MySQL connection id is 37
    Server version: 5.7.44-log MySQL Community Server (GPL)
    No default schema selected; type \use <schema> to set one.
     MySQL  <Source IP>:3306 ssl  SQL > \js
    Switching to JavaScript mode...
     MySQL  <Source IP>:3306 ssl  JS > util.copyInstance('mysql://admin@<Target IP>', {"compatibility":["skip_invalid_accounts","strip_definers","strip_restricted_grants","strip_tablespaces","ignore_wildcard_grants","strip_invalid_grants","create_invisible_pks"], users:"true", threads:2, dryRun:"true"});
    Please provide the password for 'admin@<target IP': *******************
    Save password for 'admin@<target IP>'? [Y]es/[N]o/Ne[v]er (default No):
    
  2. 如果没有错误,则删除 dryRun 选项并重新运行以执行数据加载。最终输出应如下所示:未报告错误

    SRC: Starting data dump
    100% (59.50M rows / ~59.36M rows), 142.25K rows/s, 9.68 MB/s
    SRC: Dump duration: 00:07:50s
    SRC: Total duration: 00:07:50s
    SRC: Schemas dumped: 2
    SRC: Tables dumped: 15
    SRC: Data size: 2.03 GB
    SRC: Rows written: 59502422
    SRC: Bytes written: 2.03 GB
    SRC: Average throughput: 4.33 MB/s
    1 thds indexing \ 100% (2.03 GB / 2.03 GB), 7.47 MB/s (161.06K rows/s), 15 / 15 tables done
    Building indexes - done
    Executing common postamble SQL - done
    TGT: 53 chunks (59.50M rows, 2.03 GB) for 15 tables in 2 schemas were loaded in 7 min 51 sec (avg throughput 4.32 MB/s, 126.29K rows/s)
    TGT: 17 DDL files were executed in 0 sec.
    TGT: 0 accounts were loaded, 3 accounts failed to load due to unsupported authentication plugin errors
    TGT: Data load duration: 7 min 51 sec
    TGT: 1 indexes were built in 0 sec.
    TGT: Total duration: 7 min 51 sec
    TGT: 0 warnings were reported during the load.
    ..
    ..
    Dump_metadata:
    Binlog_file: mysql-bin.000006
    Binlog_position: 626
    Executed_GTID_set: 7ee61c32-16eb-11f0-b3fc-02001702dcb5:1-3
    

注:记下 copyInstance() 输出中显示的 GTID,然后使用此 GTID 更改复制。

任务 9:更改复制并启动

使用日志文件编号 000006 和日志位置编号 626,如任务 8 中 copyInstance() 的输出所示。此日志文件和日志位置应采用格式来生成 CSN 000006:000000000000626

变更复制,然后单击开始以启动。 图中显示了如何更改复制

复制状态显示为正在运行

图中显示了复制状态

注:如果已使用方法 1 添加提取(如任务 7 中所示),则不需要更改复制。在此 POC 中,我们使用方法 2 添加提取。这两种方法都可以正常工作。

任务 10:运行 DML 测试

通过对源执行 DML 活动来测试复制。

确认

更多学习资源

通过 docs.oracle.com/learn 浏览其他实验室,或者通过 Oracle Learning YouTube 频道访问更多免费学习内容。此外,请访问 education.oracle.com/learning-explorer 以成为 Oracle Learning Explorer。

有关产品文档,请访问 Oracle 帮助中心