> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-parallel-read-in-order-multi-part.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> 将您的 MySQL 或 MariaDB 数据库中的数据无缝摄取到 ClickHouse Cloud。

# 从 MySQL 向 ClickHouse 摄取数据（使用 CDC（变更数据捕获））

export const Image = ({img, alt, size = "lg", background}) => {
  const normalizedSize = ["sm", "md", "lg"].includes(size) ? size : "lg";
  const backgroundColor = background === "white" ? "white" : background === "black" ? "rgb(31 31 28)" : undefined;
  return <div className={`ch-image-${normalizedSize}`}>
      <Frame>
        <img src={img} alt={alt} style={{
    backgroundColor
  }} />
      </Frame>
    </div>;
};

MySQL ClickPipe 提供了一种全托管且可靠的方式，可将 MySQL 和 MariaDB 数据库中的数据摄取到 ClickHouse Cloud。它既支持用于一次性摄取的 **批量加载**，也支持用于持续摄取的 **CDC (变更数据捕获)  (变更数据捕获) **。

MySQL ClickPipes 既可通过 ClickPipes UI 手动部署和管理，也可通过 [OpenAPI](/zh/integrations/clickpipes/programmatic-access/openapi) 和 [Terraform](/zh/integrations/clickpipes/programmatic-access/terraform) 以编程方式部署和管理。

<h2 id="prerequisites">
  前置条件
</h2>

[//]: # "TODO 一次性摄取管道不需要配置 binlog 复制。这一点过去经常让人困惑，因此我们还应提供批量加载所需的最低要求，以免把用户吓跑。"

开始之前，您首先需要确保 MySQL 数据库已正确配置为支持 binlog 复制。具体配置步骤取决于您部署 MySQL 的方式，因此请按照下方对应的指南进行操作：

<h3 id="supported-data-sources">
  支持的数据源
</h3>

| 名称 | 标志 | 详情 |
| - | - | - |
| **Amazon RDS MySQL** <br /> *一次性加载，CDC* | | 请参阅 [Amazon RDS MySQL](/zh/integrations/clickpipes/mysql/source/rds) 配置指南。 |
| **Amazon Aurora MySQL** <br /> *一次性加载，CDC* | | 请参阅 [Amazon Aurora MySQL](/zh/integrations/clickpipes/mysql/source/aurora) 配置指南。 |
| **Cloud SQL for MySQL** <br /> *一次性加载，CDC* | | 请参阅 [Cloud SQL for MySQL](/zh/integrations/clickpipes/mysql/source/gcp) 配置指南。 |
| **Azure Database for MySQL 灵活服务器** <br /> *一次性加载* | | 请参阅 [Azure Database for MySQL 灵活服务器](/zh/integrations/clickpipes/mysql/source/azure-flexible-server-mysql) 配置指南。 |
| **自托管 MySQL** <br /> *一次性加载，CDC* | | 请参阅 [Generic MySQL](/zh/integrations/clickpipes/mysql/source/generic) 配置指南。 |
| **Amazon RDS MariaDB** <br /> *一次性加载，CDC* | | 请参阅 [Amazon RDS MariaDB](/zh/integrations/clickpipes/mysql/source/rds-maria) 配置指南。 |
| **自托管 MariaDB** <br /> *一次性加载，CDC* | | 请参阅 [Generic MariaDB](/zh/integrations/clickpipes/mysql/source/generic-maria) 配置指南。 |

设置好源 MySQL 数据库后，您就可以继续创建 ClickPipe。

<h2 id="create-your-clickpipe">
  创建你的 ClickPipe
</h2>

请确保你已登录到 ClickHouse Cloud 账户。如果你还没有账户，可以在[这里](https://cloud.clickhouse.com/)注册。

[//]: # "   TODO 在此更新图片"

1. 在 ClickHouse Cloud 控制台中，前往你的 ClickHouse Cloud 服务。

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/MiCB1is-Av7QztGF/images/integrations/data-ingestion/clickpipes/cp_service.webp?fit=max&auto=format&n=MiCB1is-Av7QztGF&q=85&s=5d65f3fd8e67f7942506fb6b3a317280" alt="ClickPipes 服务" size="lg" border width="1184" height="482" data-path="images/integrations/data-ingestion/clickpipes/cp_service.webp" />

2. 在左侧菜单中选择 `Data Sources` 按钮，然后点击“Set up a ClickPipe”

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/MiCB1is-Av7QztGF/images/integrations/data-ingestion/clickpipes/cp_step0.webp?fit=max&auto=format&n=MiCB1is-Av7QztGF&q=85&s=6c32efedf2f71424ffa85131206a1ddd" alt="选择导入" size="lg" border width="2606" height="790" data-path="images/integrations/data-ingestion/clickpipes/cp_step0.webp" />

3. 选择 `MySQL CDC` 卡片

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/MiCB1is-Av7QztGF/images/integrations/data-ingestion/clickpipes/mysql/mysql-tile.webp?fit=max&auto=format&n=MiCB1is-Av7QztGF&q=85&s=35c67527c2776e661e76bc2519acd46b" alt="选择 MySQL" size="lg" border width="2612" height="892" data-path="images/integrations/data-ingestion/clickpipes/mysql/mysql-tile.webp" />

<h3 id="add-your-source-mysql-database-connection">
  添加源 MySQL 数据库连接
</h3>

4. 填写你在前置条件步骤中配置的源 MySQL 数据库连接信息。

<Info>
  在开始添加连接信息之前，请确保你已在防火墙规则中将 ClickPipes IP 地址加入白名单。你可以在以下页面查看 [ClickPipes IP 地址列表](/zh/integrations/clickpipes/networking/static-ips)。
  更多信息请参阅本页顶部链接的源 MySQL 设置指南 [本页顶部](#prerequisites)。
</Info>

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/MiCB1is-Av7QztGF/images/integrations/data-ingestion/clickpipes/mysql/mysql-connection-details.webp?fit=max&auto=format&n=MiCB1is-Av7QztGF&q=85&s=bdbb76dbff6f8cf45ee7b9e584666dab" alt="填写连接信息" size="lg" border width="1842" height="1556" data-path="images/integrations/data-ingestion/clickpipes/mysql/mysql-connection-details.webp" />

<h4 id="optional-changing-tls-settings">
  &#x20;(可选) 修改 TLS 设置
</h4>

默认情况下，您的 ClickPipe 在创建时会启用 TLS 和证书验证。您可以在创建 ClickPipe 时修改这些默认设置：

<Frame>
  <img src="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/K228OGf-egI2RGpt/images/integrations/data-ingestion/clickpipes/postgres/tls-settings.webp?fit=max&auto=format&n=K228OGf-egI2RGpt&q=85&s=0b8acd91adc7855bf88171fc1ccce556" alt="TLS 设置" width="1113" height="688" data-path="images/integrations/data-ingestion/clickpipes/postgres/tls-settings.webp" />
</Frame>

或者在已暂停的 ClickPipe 的 *Settings* 选项卡中的 *Connection settings* 部分进行编辑：

<Frame>
  <img src="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/K228OGf-egI2RGpt/images/integrations/data-ingestion/clickpipes/postgres/pipe-connection-settings.webp?fit=max&auto=format&n=K228OGf-egI2RGpt&q=85&s=c767591c757aae5122a2d4d0dd422238" alt="连接设置 -> 编辑连接" data-og-width="1035" width="1035" data-og-height="312" height="312" data-path="images/integrations/data-ingestion/clickpipes/postgres/pipe-connection-settings.webp" data-optimize="true" data-opv="3" srcset="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/K228OGf-egI2RGpt/images/integrations/data-ingestion/clickpipes/postgres/pipe-connection-settings.webp?w=280&fit=max&auto=format&n=K228OGf-egI2RGpt&q=85&s=586dcf1d4f588865d7f0516a666c2072 280w, https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/K228OGf-egI2RGpt/images/integrations/data-ingestion/clickpipes/postgres/pipe-connection-settings.webp?w=560&fit=max&auto=format&n=K228OGf-egI2RGpt&q=85&s=fddde54161526962dddaad2a38e02be3 560w, https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/K228OGf-egI2RGpt/images/integrations/data-ingestion/clickpipes/postgres/pipe-connection-settings.webp?w=840&fit=max&auto=format&n=K228OGf-egI2RGpt&q=85&s=39f7df37a6910e91ad1ebff1c88c9a11 840w, https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/K228OGf-egI2RGpt/images/integrations/data-ingestion/clickpipes/postgres/pipe-connection-settings.webp?w=1100&fit=max&auto=format&n=K228OGf-egI2RGpt&q=85&s=a4c78060865848c7bb4f3952d5425e31 1100w, https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/K228OGf-egI2RGpt/images/integrations/data-ingestion/clickpipes/postgres/pipe-connection-settings.webp?w=1650&fit=max&auto=format&n=K228OGf-egI2RGpt&q=85&s=38c388c9fd3e1e9555c9a545346f3488 1650w, https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/K228OGf-egI2RGpt/images/integrations/data-ingestion/clickpipes/postgres/pipe-connection-settings.webp?w=2500&fit=max&auto=format&n=K228OGf-egI2RGpt&q=85&s=b60e04b1af20123601c8fd553a32f0e0 2500w" />
</Frame>

<Frame>
  <img src="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/K228OGf-egI2RGpt/images/integrations/data-ingestion/clickpipes/postgres/pipe-edit-connection.webp?fit=max&auto=format&n=K228OGf-egI2RGpt&q=85&s=96e5f77a983130d092ba37e1d73c61e5" alt="编辑连接" width="703" height="542" data-path="images/integrations/data-ingestion/clickpipes/postgres/pipe-edit-connection.webp" />
</Frame>

说明如下：

* `Disable TLS` 用于为该连接启用或禁用 TLS。关闭 TLS 意味着数据会以明文形式通过网络传输，其中可能包含 secrets 和敏感数据。
* `Skip certificate verification` 用于启用或禁用对源数据库所提供证书的验证。请务必考虑跳过证书验证带来的安全影响。
* `TLS Host` (可选，默认为源 *Host*) 是在启用证书验证时，证书的 CN 必须匹配的 hostname。
* `Upload CA` 可用于提供在启用证书验证时使用的 CA。

<h4 id="optional-set-up-ssh-tunneling">
  &#x20;(可选) 设置 SSH 隧道
</h4>

如果源 MySQL 数据库无法从公网访问，您可以指定 SSH 隧道连接信息。

1. 启用“使用 SSH 隧道”开关。

2. 填写 SSH 连接信息。

   <Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/K228OGf-egI2RGpt/images/integrations/data-ingestion/clickpipes/postgres/ssh-tunnel.jpg?fit=max&auto=format&n=K228OGf-egI2RGpt&q=85&s=3d1ecf1b77f1ce2704be390c07f7f441" alt="SSH 隧道" size="lg" border width="1780" height="1342" data-path="images/integrations/data-ingestion/clickpipes/postgres/ssh-tunnel.jpg" />

3. 如需使用基于密钥的身份验证，请点击“撤销并生成密钥对”生成新的密钥对，并将生成的公钥复制到 SSH 服务器上的 `~/.ssh/authorized_keys`。

4. 点击“验证连接”以检查连接是否可用。

<Note>
  请确保在 SSH 堡垒机主机的防火墙规则中将 [ClickPipes IP 地址](/zh/integrations/clickpipes/networking/static-ips) 加入白名单，以便 ClickPipes 能够建立 SSH 隧道。
</Note>

填写完连接信息后，点击 `Next`。

<h4 id="advanced-settings">
  配置高级设置
</h4>

如有需要，您可以配置高级设置。以下是各项设置的简要说明：

* **同步间隔**：指 ClickPipes 轮询源数据库变更的时间间隔。这会影响目标端 ClickHouse 服务；对于对成本较敏感的用户，建议将该值设高一些 (大于 `3600`) 。
* **初始加载并行线程数**：指用于拉取初始快照的并行工作线程数。当您有大量表，并希望控制用于拉取初始快照的并行工作线程数时，此设置会很有用。此设置按表生效。
* **拉取批次大小**：单个批次中拉取的行数。这是一个尽力而为的设置，因此在某些情况下可能不会严格生效。
* **快照每个分区的行数**：指初始快照期间每个分区中将拉取的行数。当您的表中有大量行，并希望控制每个分区拉取的行数时，此设置会很有用。
* **快照并行表数量**：指初始快照期间并行拉取的表数量。当您有大量表，并希望控制并行拉取的表数量时，此设置会很有用。
* **Server ID**：用于设置 MySQL binlog 复制的服务器 ID 的可选参数。若未设置，ClickPipes 将生成一个随机服务器 ID，并且该 ID 在 ClickPipe 生命周期内可能发生变化。您可以使用它在源端 MySQL 服务器上以确定性方式标识您的 ClickPipe。

<h3 id="configure-the-tables">
  配置表
</h3>

5. 在此，您可以为 ClickPipe 选择目标数据库。您既可以选择现有数据库，也可以新建数据库。

   <Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/MiCB1is-Av7QztGF/images/integrations/data-ingestion/clickpipes/mysql/select-destination-db.webp?fit=max&auto=format&n=MiCB1is-Av7QztGF&q=85&s=6a154730d1c6064e4c04e303e0f6c348" alt="选择目标数据库" size="lg" border width="2368" height="614" data-path="images/integrations/data-ingestion/clickpipes/mysql/select-destination-db.webp" />

6. 您可以选择要从源 MySQL 数据库复制的表。选择表时，您还可以重命名目标 ClickHouse 数据库中的表，并排除特定列。

7. 此外，您可以提供自定义的 `PARTITION BY <expr>` 表达式，以控制目标端 [ClickHouse 表的分区方式](/zh/concepts/core-concepts/partitions)。
   <Frame>
     <img src="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/K228OGf-egI2RGpt/images/integrations/data-ingestion/clickpipes/postgres/partition-by.png?fit=max&auto=format&n=K228OGf-egI2RGpt&q=85&s=3cc9df43450c2fb73489422f8fdfafaf" alt="按分区" width="897" height="635" data-path="images/integrations/data-ingestion/clickpipes/postgres/partition-by.png" />
   </Frame>

<h3 id="review-permissions-and-start-the-clickpipe">
  查看权限并启动 ClickPipe
</h3>

8. 在权限下拉菜单中选择“完全访问”角色，然后点击“完成设置”。

   <Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/K228OGf-egI2RGpt/images/integrations/data-ingestion/clickpipes/postgres/ch-permissions.jpg?fit=max&auto=format&n=K228OGf-egI2RGpt&q=85&s=efa280b36d7248dbaf6e33e5a7bfd10b" alt="查看权限" size="lg" border width="1844" height="716" data-path="images/integrations/data-ingestion/clickpipes/postgres/ch-permissions.jpg" />

最后，请参阅 ["ClickPipes for MySQL 常见问题"](/zh/integrations/clickpipes/mysql/faq) 页面，了解常见问题及其解决方法的更多信息。

<h2 id="whats-next">
  后续步骤？
</h2>

[//]: # "TODO 编写一份 MySQL 专用迁移指南和最佳实践，类似于现有的 PostgreSQL 指南。当前迁移指南指向 MySQL 表引擎，这并不理想。"

完成 ClickPipe 配置，将数据从 MySQL 复制到 ClickHouse Cloud 后，你就可以重点考虑如何查询和建模数据，以获得最佳性能。有关 MySQL CDC 和故障排查的常见问题，请参阅 [MySQL 常见问题页面](/zh/integrations/clickpipes/mysql/faq)。
