> ## 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.

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

# MySQL 数据库引擎

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>
            Not supported in ClickHouse Cloud
        </div>;
};

允许连接到远程 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')
```

**引擎参数**

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

<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) |

其他所有 MySQL 数据类型都会被转换为 [String](/zh/reference/data-types/string)。

支持 [Nullable](/zh/reference/data-types/nullable)。

<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 │
└────────┴───────┘
```
