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

# 将 MySQL 集成到 ClickHouse

> MySQL 集成说明页面

export const ExperimentalBadge = () => {
  return <div className="experimentalBadge">
            <div className="experimentalIcon">
            <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path strokeWidth="1.25" d="M5.5 2H10.5" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
                <path strokeWidth="1.25" d="M9.50015 2V6.19625L13.4283 12.7425C13.4738 12.8183 13.4985 12.9049 13.4996 12.9934C13.5008 13.0818 13.4785 13.169 13.435 13.246C13.3914 13.323 13.3283 13.3871 13.2519 13.4317C13.1755 13.4764 13.0886 13.4999 13.0002 13.5H3.00015C2.91164 13.5 2.8247 13.4766 2.74822 13.432C2.67174 13.3874 2.60847 13.3233 2.56487 13.2463C2.52126 13.1693 2.49889 13.082 2.50004 12.9935C2.50119 12.905 2.52582 12.8184 2.5714 12.7425L6.50015 6.19625V2" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
                <path strokeWidth="1.25" d="M4.47656 9.56754C5.30344 9.41254 6.47656 9.47942 7.99969 10.25C10.0153 11.2707 11.4216 11.0569 12.2184 10.7282" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
            </svg>
        </div>
            Experimental 功能。 <u><a href="/docs/beta-and-experimental-features#experimental-features">了解详情。</a></u>
        </div>;
};

export const CloudNotSupportedBadge = () => {
  return <div className="cloudNotSupportedBadge">
            <div className="cloudNotSupportedIcon">
            <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path strokeWidth="1.5" d="M6.33366 12.6666L12.3739 12.6667C13.6593 12.6667 14.7073 11.6187 14.7073 10.3334C14.7073 9.04804 13.6593 8.00003 12.3739 8.00003C12.3739 8.00003 12.3337 7.66659 12.0003 7.33325M10.667 5.33322C8.00033 2.33325 4.45395 4.78537 4.14195 6.68203C2.55728 6.7627 1.29395 8.06203 1.29395 9.6667C1.29395 11.3234 2.66699 12.6666 4.00033 12.6666" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
                <path strokeWidth="1.5" d="M2.66699 14L12.0003 4.66663" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
            </svg>

        </div>
            ClickHouse Cloud 不支持此功能
        </div>;
};

本页介绍如何使用 `MySQL` 表引擎从 MySQL 表中读取数据。

<Note>
  对于 ClickHouse Cloud，你还可以使用 [MySQL ClickPipe](/zh/integrations/clickpipes/mysql/index) (当前处于 Public Beta) 轻松将数据从 MySQL 表迁移到 ClickHouse。
</Note>

<div id="connecting-clickhouse-to-mysql-using-the-mysql-table-engine">
  ## 使用 MySQL 表引擎将 ClickHouse 连接到 MySQL
</div>

`MySQL` 表引擎可让您将 ClickHouse 连接到 MySQL。您可以在 ClickHouse 或 MySQL 表中执行 **SELECT** 和 **INSERT** 语句。本文介绍 `MySQL` 表引擎的基本用法。

<div id="1-configure-mysql">
  ### 1. 配置 MySQL
</div>

1. 在 MySQL 中创建数据库：

```sql theme={null}
  CREATE DATABASE db1;
```

2. 创建表：

```sql theme={null}
  CREATE TABLE db1.table1 (
    id INT,
    column1 VARCHAR(255)
  );
```

3. 插入示例数据行：

```sql theme={null}
  INSERT INTO db1.table1
    (id, column1)
  VALUES
    (1, 'abc'),
    (2, 'def'),
    (3, 'ghi');
```

4. 创建一个供 ClickHouse 连接使用的用户：

```sql theme={null}
  CREATE USER 'mysql_clickhouse'@'%' IDENTIFIED BY 'Password123!';
```

5. 按需授予权限。 (为便于演示，已向 `mysql_clickhouse` 用户授予管理员权限。)

```sql theme={null}
  GRANT ALL PRIVILEGES ON *.* TO 'mysql_clickhouse'@'%';
```

<Note>
  如果你在 ClickHouse Cloud 中使用此功能，可能需要允许 ClickHouse Cloud 的 IP 地址访问你的 MySQL 实例。
  有关出站流量的详细信息，请参阅 ClickHouse [Cloud 端点 API](/zh/products/cloud/guides/sql-console/query-endpoints)。
</Note>

<div id="2-define-a-table-in-clickhouse">
  ### 2. 在 ClickHouse 中定义表
</div>

1. 现在创建一个使用 `MySQL` 表引擎的 ClickHouse 表：

```sql theme={null}
  CREATE TABLE mysql_table1 (
    id UInt64,
    column1 String
  )
  ENGINE = MySQL('mysql-host.domain.com','db1','table1','mysql_clickhouse','Password123!')
```

至少需要以下参数：

| parameter | Description   | example               |
| --------- | ------------- | --------------------- |
| host      | 主机名或 IP 地址    | mysql-host.domain.com |
| database  | MySQL 数据库名称   | db1                   |
| table     | MySQL 表名称     | table1                |
| user      | 连接 MySQL 的用户名 | mysql\_clickhouse     |
| password  | 连接 MySQL 的密码  | Password123!          |

<Note>
  完整参数列表请参阅 [MySQL 表引擎](/zh/reference/engines/table-engines/integrations/mysql) 文档页面。
</Note>

<div id="3-test-the-integration">
  ### 3. 测试集成
</div>

1. 在 MySQL 中，插入一行示例数据：

```sql theme={null}
  INSERT INTO db1.table1
    (id, column1)
  VALUES
    (4, 'jkl');
```

2. 注意：MySQL 表中现有的行以及你刚刚添加的新行，都已出现在 ClickHouse 表中：

```sql theme={null}
  SELECT
      id,
      column1
  FROM mysql_table1
```

你应该会看到 4 行：

```response theme={null}
  Query id: 6d590083-841e-4e95-8715-ef37d3e95197

  ┌─id─┬─column1─┐
  │  1 │ abc     │
  │  2 │ def     │
  │  3 │ ghi     │
  │  4 │ jkl     │
  └────┴─────────┘

  4 rows in set. Elapsed: 0.044 sec.
```

3. 向 ClickHouse 表中添加一行数据：

```sql theme={null}
  INSERT INTO mysql_table1
    (id, column1)
  VALUES
    (5,'mno')
```

4. 注意，MySQL 中会出现新的一行：

```bash theme={null}
  mysql> select id,column1 from db1.table1;
```

你应该会看到新增的行：

```response theme={null}
  +------+---------+
  | id   | column1 |
  +------+---------+
  |    1 | abc     |
  |    2 | def     |
  |    3 | ghi     |
  |    4 | jkl     |
  |    5 | mno     |
  +------+---------+
  5 rows in set (0.01 sec)
```

<div id="summary">
  ### 摘要
</div>

`MySQL` 表引擎可让你将 ClickHouse 连接到 MySQL，在两者之间双向交换数据。更多详情请参阅 [MySQL 表引擎](/zh/reference/functions/table-functions/mysql) 的文档页面。
