> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> 将 Amazon RDS MySQL 配置为 ClickPipes 源的分步指南

# RDS MySQL 源设置指南

export const Image = ({img, alt, size}) => {
  return <Frame>
      <img src={img} alt={alt} />
    </Frame>;
};

本分步指南将介绍如何配置 Amazon RDS MySQL，使用 [MySQL ClickPipe](/zh/integrations/clickpipes/mysql) 将数据复制到 ClickHouse Cloud。有关 MySQL CDC 的常见问题，请参阅 [MySQL 常见问题页面](/zh/integrations/clickpipes/mysql/faq)。

<div id="enable-binlog-retention-rds">
  ## 启用二进制日志保留
</div>

二进制日志是一组日志文件，记录了对 MySQL 服务器实例所做的数据更改；而进行复制则需要这些二进制日志文件。要在 RDS MySQL 中配置二进制日志保留，您必须先[启用二进制日志记录](#enable-binlog-logging)，并[延长 binlog 保留时间间隔](#binlog-retention-interval)。

<div id="enable-binlog-logging">
  ### 1. 通过自动备份启用二进制日志
</div>

自动备份功能决定 MySQL 是否启用二进制日志。你可以在 RDS 控制台中依次进入 **Modify** > **Additional configuration** > **Backup**，然后选中 **Enable automated backups** 复选框 (如果尚未选中) ，为实例配置自动备份。

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/8xU-7NRzcVe16bmG/images/integrations/data-ingestion/clickpipes/mysql/source/rds/rds-backups.png?fit=max&auto=format&n=8xU-7NRzcVe16bmG&q=85&s=90254433b20a2c148eff941e000a09c7" alt="在 RDS 中启用自动备份" size="lg" border width="3230" height="530" data-path="images/integrations/data-ingestion/clickpipes/mysql/source/rds/rds-backups.png" />

我们建议根据复制使用场景，将 **备份保留期** 设置为一个相对较长的值。

<div id="binlog-retention-interval">
  ### 2. 增加 binlog 保留时间间隔
</div>

<Warning>
  如果 ClickPipes 尝试恢复复制，而由于配置的 binlog 保留值，所需的 二进制日志 文件已被清除，则 ClickPipe 将进入错误状态，并且需要重新同步。
</Warning>

默认情况下，Amazon RDS 会尽快清除 二进制日志 (即 *延迟清除*) 。我们建议将 binlog 保留时间增加到至少 **72 小时**，以确保在发生故障时，复制所需的 二进制日志 文件仍然可用。要设置 二进制日志 的保留时间 ([`binlog retention hours`](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/mysql-stored-proc-configuring.html#mysql_rds_set_configuration-usage-notes.binlog-retention-hours)) ，请使用 [`mysql.rds_set_configuration`](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/mysql-stored-proc-configuring.html#mysql_rds_set_configuration) 存储过程：

[//]: # "NOTE 大多数 CDC 提供商建议使用 RDS 支持的最长保留期（7 天/168 小时）。由于这会影响磁盘使用量，我们保守地建议至少设置为 3 天/72 小时。"

```text theme={null}
mysql=> call mysql.rds_set_configuration('binlog retention hours', 72);
```

如果未设置此配置，或将其间隔设得过低，可能会导致二进制日志出现断档，从而影响 ClickPipes 恢复复制。

<div id="binlog-settings">
  ## 配置 binlog 设置
</div>

在 RDS 控制台中点击你的 MySQL 实例，然后进入 **Configuration** 选项卡，即可找到参数组。

<Tip>
  如果你使用的是 MySQL cluster，则下列参数位于 [DB cluster](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/USER_WorkingWithParamGroups.CreatingCluster.html) 参数组中，而不是 DB 实例组中。
</Tip>

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/lGskH5qUgz9Vtlav/images/integrations/data-ingestion/clickpipes/mysql/parameter_group/rds_config.png?fit=max&auto=format&n=lGskH5qUgz9Vtlav&q=85&s=43680fa1238b0257129bd71baeabe863" alt="在 RDS 中查找参数组的位置" size="lg" border width="708" height="853" data-path="images/integrations/data-ingestion/clickpipes/mysql/parameter_group/rds_config.png" />

<br />

点击参数组链接，进入其详情页面。你应当会在右上角看到 **Edit** 按钮。

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/lGskH5qUgz9Vtlav/images/integrations/data-ingestion/clickpipes/mysql/parameter_group/edit_button.png?fit=max&auto=format&n=lGskH5qUgz9Vtlav&q=85&s=cc384751cd424f089daf4fcec3e1c993" alt="编辑参数组" size="lg" border width="1662" height="292" data-path="images/integrations/data-ingestion/clickpipes/mysql/parameter_group/edit_button.png" />

将以下参数设置为：

1. 将 `binlog_format` 设为 `ROW`。

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/lGskH5qUgz9Vtlav/images/integrations/data-ingestion/clickpipes/mysql/parameter_group/binlog_format.png?fit=max&auto=format&n=lGskH5qUgz9Vtlav&q=85&s=577d8354fff2ec7786d7bf858841b375" alt="将 binlog format 设为 ROW" size="lg" border width="960" height="232" data-path="images/integrations/data-ingestion/clickpipes/mysql/parameter_group/binlog_format.png" />

2. 将 `binlog_row_metadata` 设为 `FULL`

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/lGskH5qUgz9Vtlav/images/integrations/data-ingestion/clickpipes/mysql/parameter_group/binlog_row_metadata.png?fit=max&auto=format&n=lGskH5qUgz9Vtlav&q=85&s=ea2ca2f04e8241d31bface4647a36199" alt="将 binlog row metadata 设为 FULL" size="lg" border width="934" height="234" data-path="images/integrations/data-ingestion/clickpipes/mysql/parameter_group/binlog_row_metadata.png" />

3. 将 `binlog_row_image` 设为 `FULL`

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/lGskH5qUgz9Vtlav/images/integrations/data-ingestion/clickpipes/mysql/parameter_group/binlog_row_image.png?fit=max&auto=format&n=lGskH5qUgz9Vtlav&q=85&s=0d6367589076ab5fb1875645fd69a87c" alt="将 binlog row image 设为 FULL" size="lg" border width="934" height="234" data-path="images/integrations/data-ingestion/clickpipes/mysql/parameter_group/binlog_row_image.png" />

<br />

然后，点击右上角的 **Save Changes**。你可能需要重启实例，更改才会生效——一个判断方法是：在 RDS 实例的 **Configuration** 选项卡中，如果参数组链接旁边显示 `Pending reboot`，就说明需要重启。

<div id="gtid-mode">
  ## 启用 GTID 模式
</div>

<Tip>
  MySQL ClickPipe 也支持在不启用 GTID 模式的情况下进行复制。不过，建议启用 GTID 模式，以获得更好的性能并简化故障排查。
</Tip>

[全局事务标识符 (GTID) ](https://dev.mysql.com/doc/refman/8.0/en/replication-gtids.html)是 MySQL 中为每个已提交事务分配的唯一 ID。它们可简化 binlog 复制，并让故障排查更加容易。我们**建议**启用 GTID 模式，以便 MySQL ClickPipe 使用基于 GTID 的复制。

Amazon RDS for MySQL 5.7、8.0 和 8.4 版本支持基于 GTID 的复制。要为您的 Aurora MySQL 实例启用 GTID 模式，请按以下步骤操作：

1. 在 RDS 控制台中，点击您的 MySQL 实例。
2. 点击 **Configuration** 选项卡。
3. 点击参数组链接。
4. 点击右上角的 **Edit** 按钮。
5. 将 `enforce_gtid_consistency` 设置为 `ON`。
6. 将 `gtid-mode` 设置为 `ON`。
7. 点击右上角的 **Save Changes**。
8. 重启您的实例，使更改生效。

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/lGskH5qUgz9Vtlav/images/integrations/data-ingestion/clickpipes/mysql/enable_gtid.png?fit=max&auto=format&n=lGskH5qUgz9Vtlav&q=85&s=01c4dfcab1fb24600af79c3c33cdb5ee" alt="GTID 已启用" size="lg" border width="1650" height="469" data-path="images/integrations/data-ingestion/clickpipes/mysql/enable_gtid.png" />

<br />

<Tip>
  MySQL ClickPipe 也支持在不启用 GTID 模式的情况下进行复制。不过，建议启用 GTID 模式，以获得更好的性能并简化故障排查。
</Tip>

<div id="configure-database-user">
  ## 配置数据库用户
</div>

以管理员身份连接到你的 RDS MySQL 实例，并执行以下命令：

1. 为 ClickPipes 创建一个专用用户：

   ```sql theme={null}
   CREATE USER 'clickpipes_user'@'host' IDENTIFIED BY 'some-password';
   ```

2. 授予 schema 权限。以下示例展示了 `mysql` 数据库的权限。对于你要复制的每个数据库和主机，请重复执行这些命令：

   ```sql theme={null}
   GRANT SELECT ON `mysql`.* TO 'clickpipes_user'@'host';
   ```

3. 向该用户授予复制权限：

   ```sql theme={null}
   GRANT REPLICATION CLIENT ON *.* TO 'clickpipes_user'@'%';
   GRANT REPLICATION SLAVE ON *.* TO 'clickpipes_user'@'%';
   ```

<div id="configure-network-access">
  ## 配置网络访问
</div>

<div id="ip-based-access-control">
  ### 基于 IP 的访问控制
</div>

要限制发往 Aurora MySQL 实例的流量，请将[文档中列出的静态 NAT IP 地址](/zh/integrations/clickpipes/home#list-of-static-ips)添加到 RDS 安全组 (Security Group) 的 **Inbound rules** 中。

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/8xU-7NRzcVe16bmG/images/integrations/data-ingestion/clickpipes/mysql/source/rds/security-group-in-rds-mysql.png?fit=max&auto=format&n=8xU-7NRzcVe16bmG&q=85&s=a3aa579333d3437ccec8476951ec5cc8" alt="在 RDS MySQL 中在哪里可以找到安全组？" size="lg" border width="2850" height="994" data-path="images/integrations/data-ingestion/clickpipes/mysql/source/rds/security-group-in-rds-mysql.png" />

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/8xU-7NRzcVe16bmG/images/integrations/data-ingestion/clickpipes/postgres/source/rds/edit_inbound_rules.png?fit=max&auto=format&n=8xU-7NRzcVe16bmG&q=85&s=d8d14ebac6077992b5cd0e060b81c315" alt="编辑上述安全组的入站规则" size="lg" border width="1800" height="935" data-path="images/integrations/data-ingestion/clickpipes/postgres/source/rds/edit_inbound_rules.png" />

<div id="private-access-via-aws-privatelink">
  ### 通过 AWS PrivateLink 实现私有访问
</div>

若要通过私有网络连接到您的 RDS 实例，可以使用 AWS PrivateLink。请按照 [ClickPipes 的 AWS PrivateLink 设置指南](/zh/resources/support-center/knowledge-base/cloud-services/aws-privatelink-setup-for-clickpipes) 配置连接。

<div id="next-steps">
  ## 后续步骤
</div>

现在，您的 Amazon RDS MySQL 实例已完成 binlog 复制配置，并已安全连接到 ClickHouse Cloud，您可以[创建您的第一个 MySQL ClickPipe](/zh/integrations/clickpipes/mysql#create-your-clickpipe)。如需了解 MySQL CDC 的常见问题，请参阅 [MySQL 常见问题页面](/zh/integrations/clickpipes/mysql/faq)。
