When you need to run real-time analytics on operational data, the mysql() table function lets you pull data from ApsaraDB RDS for MySQL directly into ApsaraDB for ClickHouse in a single SQL statement. This guide walks through the complete process: preparing tables on both sides, running the migration query, and verifying the result.
Prerequisites
Before you begin, ensure that you have:
An ApsaraDB for ClickHouse cluster. If you haven't created one, see Create a cluster.
A privileged database account for the ClickHouse cluster. See Create an account.
The ApsaraDB RDS for MySQL instance and the ApsaraDB for ClickHouse cluster in the same region and virtual private cloud (VPC).
The ClickHouse cluster IP address added to the RDS MySQL whitelist, and the RDS MySQL IP address added to the ClickHouse whitelist.
Run SELECT * FROM system.clusters; in ClickHouse to get the cluster's IP address. For whitelist setup, see Configure an IP address whitelist (ClickHouse) and Configure IP address whitelist (RDS MySQL). If connectivity fails, see the FAQ.
Step 1: Set up the source table in ApsaraDB RDS for MySQL
Skip this step if your RDS MySQL instance already has the data you want to migrate.
Connect to the ApsaraDB RDS for MySQL instance using Data Management (DMS). See Use DMS to log on to an ApsaraDB RDS for MySQL instance.
Create a database:
CREATE DATABASE testdb;Create the source table:
CREATE TABLE testdb.mysql_test_table ( v1 Int NOT NULL, v2 Int DEFAULT NULL, v3 Float DEFAULT NULL ) ENGINE=InnoDB;Insert sample data:
INSERT INTO testdb.mysql_test_table VALUES (4,4,4.0),(1,1,2.0),(1,1,0.0),(4,1,2.0),(7,1,3.0);
Step 2: Create a table in ApsaraDB for ClickHouse
Connect to the ApsaraDB for ClickHouse cluster using DMS. See Connect to a ClickHouse cluster using DMS.
Create a local table:
NoteMap each MySQL column type to the corresponding ClickHouse type. Columns defined without
NOT NULLin MySQL becomeNullablein ClickHouse. For the full mapping, see Data type mappings.CREATE TABLE default.clickhouse_test_table ON CLUSTER default ( v1 Int32, v2 Nullable(Int32), v3 Nullable(Float32) ) ENGINE = MergeTree ORDER BY v1;(Optional) Create a distributed table for multi-node clusters:
CREATE TABLE clickhouse_test_table_distributed ON CLUSTER default AS clickhouse_test_table ENGINE = Distributed(default, default, clickhouse_test_table, rand());Distributed tables spread data across all nodes in the cluster, enabling parallel reads and writes for higher throughput and availability. We recommend creating a distributed table for multi-node clusters.
Step 3: Import data from ApsaraDB RDS for MySQL
Connect to the ApsaraDB for ClickHouse cluster using DMS, then run an INSERT INTO ... SELECT statement using the mysql() table function:
INSERT INTO <clickhouse_table>
SELECT * FROM mysql('<rds_endpoint>:<port>', '<database>', '<table>', '<user>', '<password>');Parameters:
| Parameter | Description | Example |
|---|---|---|
<clickhouse_table> | Name of the destination ClickHouse table | clickhouse_test_table_distributed |
<rds_endpoint> | Internal endpoint of the RDS MySQL instance | rm-bp16t9h3999xb****.mysql.rds.aliyuncs.com |
<port> | Port of the RDS MySQL instance | 3306 |
<database> | Source database name | testdb |
<table> | Source table name | mysql_test_table |
<user> | RDS MySQL database account | test |
<password> | Password for the database account | 123456Aa |
To get the RDS MySQL endpoint and port, see View and manage instance endpoints and ports.
Full import example:
INSERT INTO clickhouse_test_table_distributed
SELECT * FROM mysql('rm-bp16t9h3999xb****.mysql.rds.aliyuncs.com:3306', 'testdb', 'mysql_test_table', 'test', '123456Aa');Step 4: Verify the imported data
Query the destination table to confirm the data was imported:
SELECT * FROM clickhouse_test_table_distributed;Click Execute(F8) in DMS to run the query. The result should match the data inserted in Step 1.
If you imported to a local table instead, replace clickhouse_test_table_distributed with clickhouse_test_table in the query.
Data type mappings
When creating the ClickHouse table, use the following type mappings. If the source and destination types don't match exactly, ClickHouse attempts automatic type conversion.
Columns that allow NULL in MySQL are mapped to Nullable in ClickHouse. Columns defined without NOT NULL in MySQL become Nullable in the ClickHouse table.
| ApsaraDB RDS for MySQL / PolarDB for MySQL type | ApsaraDB for ClickHouse type |
|---|---|
| Unsigned tinyint | UInt8 |
| Tinyint | Int8 |
| Unsigned smallint | UInt16 |
| Smallint | Int16 |
| Unsigned int, Unsigned mediumint | UInt32 |
| Int, Mediumint | Int32 |
| Unsigned bigint | UInt64 |
| Bigint | Int64 |
| Float | Float32 |
| Double | Float64 |
| Date | Date |
| Datetime, Timestamp | DateTime |
| Binary | FixedString |
| Others | String |