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

> 允许连接到远程 MySQL 服务器 上的数据库，并执行 `INSERT` 和 `SELECT` 查询，在 ClickHouse 与 MySQL 之间交换数据。

# MySQL

export const CloudNotSupportedBadge = () => {
  return <a href="https://clickhouse.com/docs/products/cloud/guides/cloud-compatibility#list-of-unsupported-features" 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 不支持此功能
        </a>;
};

<CloudNotSupportedBadge />

允许连接到远程 MySQL 服务器 上的数据库，并执行 `INSERT` 和 `SELECT` 查询，在 ClickHouse 与 MySQL 之间交换数据。

`MySQL` 数据库引擎会将查询转发到 MySQL 服务器，因此您可以执行 `SHOW TABLES` 或 `SHOW CREATE TABLE` 等操作。

您无法执行以下查询：

* `RENAME`
* `CREATE TABLE`
* `ALTER`

<div id="creating-a-database">
  ## 创建数据库
</div>

```sql theme={null}
CREATE DATABASE [IF NOT EXISTS] db_name [ON CLUSTER cluster]
ENGINE = MySQL('host:port', ['database' | database], 'user', 'password')
[SETTINGS enable_compression=0]
```

**引擎参数**

* `host:port` — MySQL 服务器地址。
* `database` — 远程数据库名称。
* `user` — MySQL 用户名。
* `password` — 用户密码。

**设置**

<div id="enable-compression">
  ### `enable_compression`
</div>

为 MySQL 协议连接启用 zlib 压缩。设置为 `1` 时，ClickHouse 会向 MySQL 服务器请求启用协议级压缩。

默认值：`0`。

示例：

```sql theme={null}
CREATE DATABASE mysql_db
ENGINE = MySQL('localhost:3306', 'test', 'my_user', 'user_password')
SETTINGS enable_compression = 1;
```

<div id="tls-ssl">
  ## TLS/SSL
</div>

与 MySQL 建立加密连接所需的凭据可作为[命名集合](/zh/concepts/features/configuration/server-config/named-collections)的键 (或键值参数) 传递：

| 参数             | 描述                         |
| -------------- | -------------------------- |
| `ssl_ca_pem`   | 用于验证 MySQL 服务器证书的 CA 证书内容。 |
| `ssl_cert_pem` | 用于基于证书的身份验证的客户端证书内容。       |
| `ssl_key_pem`  | 与 `ssl_cert_pem` 对应的私钥内容。  |

这些值是相应 PEM 文件的内容，可复制到命名集合或查询中。与密码一样，它们会在日志和 `SHOW` 查询中被隐藏。

也可以通过 `ssl_ca`、`ssl_cert` 和 `ssl_key` 指定服务器上文件的路径来提供相同的凭据——但**只能在服务器配置文件中定义的命名集合中使用**，且此类值无法在查询中覆盖。服务器会使用自身的特权打开这些文件；因此，若允许从 SQL 中传入路径，任何能够定义 MySQL source 的用户都能探查本地文件系统，并使用其自身无权读取的证书和密钥进行身份验证。

<a id="data_types-support" />

<div id="data-types-support">
  ## 数据类型支持
</div>

| MySQL                            | ClickHouse                                                      |
| -------------------------------- | --------------------------------------------------------------- |
| UNSIGNED TINYINT                 | [UInt8](/zh/reference/data-types/int-uint)                      |
| TINYINT                          | [Int8](/zh/reference/data-types/int-uint)                       |
| UNSIGNED SMALLINT                | [UInt16](/zh/reference/data-types/int-uint)                     |
| SMALLINT                         | [Int16](/zh/reference/data-types/int-uint)                      |
| UNSIGNED INT, UNSIGNED MEDIUMINT | [UInt32](/zh/reference/data-types/int-uint)                     |
| INT, MEDIUMINT                   | [Int32](/zh/reference/data-types/int-uint)                      |
| UNSIGNED BIGINT                  | [UInt64](/zh/reference/data-types/int-uint)                     |
| BIGINT                           | [Int64](/zh/reference/data-types/int-uint)                      |
| FLOAT                            | [Float32](/zh/reference/data-types/float)                       |
| DOUBLE                           | [Float64](/zh/reference/data-types/float)                       |
| DATE                             | [Date](/zh/reference/data-types/date)                           |
| DATETIME, TIMESTAMP              | [DateTime](/zh/reference/data-types/datetime)                   |
| BINARY                           | [FixedString](/zh/reference/data-types/fixedstring)             |
| POINT                            | [Point](/zh/reference/data-types/geo#point)                     |
| LINESTRING                       | [LineString](/zh/reference/data-types/geo#linestring)           |
| POLYGON                          | [Polygon](/zh/reference/data-types/geo#polygon)                 |
| MULTILINESTRING                  | [MultiLineString](/zh/reference/data-types/geo#multilinestring) |
| MULTIPOLYGON                     | [MultiPolygon](/zh/reference/data-types/geo#multipolygon)       |
| MULTIPOINT                       | [MultiPoint](/zh/reference/data-types/geo#multipoint)           |
| GEOMETRY                         | [Geometry](/zh/reference/data-types/geo#geometry)               |

空间类型的转换 (始终会转换的 `POINT` 除外) 由 [`mysql_datatypes_support_level`](/zh/reference/settings/session-settings/mysql#mysql_datatypes_support_level) 设置中的 `geometry` 标志控制，该标志默认启用。通用 `GEOMETRY` 列类型会映射为总括性的 [`Geometry`](/zh/reference/data-types/geo#geometry) 类型 (即具体几何类型的 `Variant`) 。由于这类列可存储任意子类型的值，读取没有对应 ClickHouse 类型的子类型值 (`GEOMETRYCOLLECTION`) 时会引发异常；为获得恰当的几何类型，可接受这一不兼容性。声明为 `GEOMETRYCOLLECTION` 类型的列会与其他所有 MySQL 数据类型一样，转换为 [String](/zh/reference/data-types/string) 。

支持 [Nullable](/zh/reference/data-types/nullable)。在以下三种情况下，空间列会映射为 `String` (如果可为空，则为 `Nullable(String)`) ，而非几何类型：声明为 `GEOMETRYCOLLECTION`；`geometry` 标志已禁用且类型不是 `POINT`；或该列可为空且类型不是 `POINT`，因为 `Point` 是唯一可嵌套在 `Nullable` 中的几何类型。在这三种情况下，字符串会精确保留 MySQL 返回的值：一个 4 字节的 SRID 前缀，后接 WKB 载荷，因此在将其传递给 WKB 解码器之前，请去除前导的这 4 个字节。

<div id="global-variables-support">
  ## 支持全局变量
</div>

为获得更好的兼容性，你可以使用 MySQL 风格引用全局变量，即 `@@identifier`。

支持以下变量：

* `version`
* `max_allowed_packet`

<Note>
  目前这些变量只是占位符，并不对应任何实际内容。
</Note>

示例：

```sql theme={null}
SELECT @@version;
```

<div id="examples-of-use">
  ## 使用示例
</div>

MySQL 中的表：

```text theme={null}
mysql> USE test;
Database changed

mysql> CREATE TABLE `mysql_table` (
    ->   `int_id` INT NOT NULL AUTO_INCREMENT,
    ->   `float` FLOAT NOT NULL,
    ->   PRIMARY KEY (`int_id`));
Query OK, 0 rows affected (0,09 sec)

mysql> insert into mysql_table (`int_id`, `float`) VALUES (1,2);
Query OK, 1 row affected (0,00 sec)

mysql> select * from mysql_table;
+------+-----+
| int_id | value |
+------+-----+
|      1 |     2 |
+------+-----+
1 row in set (0,00 sec)
```

ClickHouse 中与 MySQL 服务器交换数据的数据库：

```sql theme={null}
CREATE DATABASE mysql_db ENGINE = MySQL('localhost:3306', 'test', 'my_user', 'user_password') SETTINGS read_write_timeout=10000, connect_timeout=100;
```

```sql theme={null}
SHOW DATABASES
```

```text theme={null}
┌─name─────┐
│ default  │
│ mysql_db │
│ system   │
└──────────┘
```

```sql theme={null}
SHOW TABLES FROM mysql_db
```

```text theme={null}
┌─name─────────┐
│  mysql_table │
└──────────────┘
```

```sql theme={null}
SELECT * FROM mysql_db.mysql_table
```

```text theme={null}
┌─int_id─┬─value─┐
│      1 │     2 │
└────────┴───────┘
```

```sql theme={null}
INSERT INTO mysql_db.mysql_table VALUES (3,4)
```

```sql theme={null}
SELECT * FROM mysql_db.mysql_table
```

```text theme={null}
┌─int_id─┬─value─┐
│      1 │     2 │
│      3 │     4 │
└────────┴───────┘
```
