Migrating databases from MySQL® to ClickHouse® using Yandex Data Transfer
Data Transfer enables you to migrate your database from a MySQL® source cluster to ClickHouse®.
To transfer data:
- Prepare the source cluster.
- Set up and activate the transfer.
- Test your transfer.
- Query data in ClickHouse®.
If you no longer need the resources you created, delete them.
Required paid resources
- Managed Service for MySQL® cluster, which includes computing resources allocated to hosts, storage and backup size (see Managed Service for MySQL® pricing).
- Managed Service for ClickHouse® cluster, which includes the use of computing resources allocated to hosts, storage and backup size (see Managed Service for ClickHouse® pricing).
- Public IP addresses if public access is enabled for cluster hosts (see Virtual Private Cloud pricing).
- Each transfer, which includes the use of computing resources and the number of transferred data rows (see Data Transfer pricing).
Getting started
Set up your infrastructure:
-
Create a Managed Service for MySQL® source cluster of any suitable configuration. Enable public access to the cluster during creation so you can connect to it from your local machine. Connections from within the Yandex Cloud network are enabled by default.
-
Create a target Managed Service for ClickHouse® cluster with the following settings:
- Number of ClickHouse® hosts: Minimum of 2 to enable replication within the cluster.
- Database name: Must be identical to the database name in the source cluster.
- To be able to connect to the cluster not only from within the Yandex Cloud network but also from your local machine, enable public access when creating it.
-
If using security groups, configure them to allow internet access to your clusters:
-
If you do not have Terraform yet, install it.
-
Get the authentication credentials. You can add them to environment variables or specify them later in the provider configuration file.
-
Configure and initialize a provider. There is no need to create a provider configuration file manually, you can download it
. -
Place the configuration file in a separate working directory and specify the parameter values. If you did not add the authentication credentials to environment variables, specify them in the configuration file.
-
Download the data-transfer-mmy-mch.tf
configuration file to your current working directory.This file describes:
- Network.
- Subnets.
- Security group and the rule required for connecting to the Managed Service for MySQL® cluster.
- Managed Service for MySQL® source cluster.
- Managed Service for ClickHouse® target cluster.
- Source endpoint.
- Target endpoint.
- Transfer.
-
In the
data-transfer-mmy-mch.tffile, specify the following:-
Managed Service for MySQL® source cluster parameters to use for the source endpoint:
source_mysql_version: MySQL® version.source_db_name: MySQL® database name.source_userandsource_password: Database owner username and password.
-
Managed Service for ClickHouse® target cluster parameters to use for the target endpoint:
target_db_name: ClickHouse® database name.target_userandtarget_password: Database owner username and password.
-
-
Validate your Terraform configuration files using this command:
terraform validateTerraform will display any configuration errors detected in your files.
-
Create the required infrastructure:
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
All the required resources will be created in the specified folder. You can check resource availability and their settings in the management console
. -
Prepare the source cluster
-
If you created the infrastructure manually, set up your source cluster.
-
Populate the database with test data.
- Create a table named
x_tab:
CREATE TABLE x_tab ( id INT, name TEXT, PRIMARY KEY (id) );- Populate the table with data:
INSERT INTO x_tab (id, name) VALUES (40, 'User1'), (41, 'User2'), (42, 'User3'), (43, 'User4'), (44, 'User5'); - Create a table named
Set up and activate the transfer
-
-
Database type:
MySQL® -
Endpoint parameters → Connection settings:
Managed Service for MySQL clusterSelect your source cluster from the list and specify its connection settings.
-
-
-
Database type:
ClickHouse -
Endpoint parameters → Connection settings:
Managed clusterSelect your target cluster from the list and specify its connection settings.
-
-
Create a transfer of the Snapshot and replication-type that will use the endpoints you created.
-
Activate the transfer.
-
In the
data-transfer-mmy-mch.tffile, set thetransfer_enabledvariable to1. -
Validate your Terraform configuration files using this command:
terraform validateTerraform will display any configuration errors detected in your files.
-
Create the required infrastructure:
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
The transfer will be activated automatically upon creation.
-
Test your transfer
-
Wait for the transfer status to change to Replicating.
-
Make sure the data from the Managed Service for MySQL® source cluster has been transferred to the Managed Service for ClickHouse® database:
-
Connect to the cluster using
clickhouse-client. -
Run this query:
SELECT * FROM <ClickHouse®_database_name>.x_tabResult:
┌─id─┬─name──┬─__data_transfer_commit_time─┬─__data_transfer_delete_time─┐ │ 40 │ User1 │ 1661952756538347180 │ 0 │ │ 41 │ User2 │ 1661952756538347180 │ 0 │ │ 42 │ User3 │ 1661952756538347180 │ 0 │ │ 43 │ User4 │ 1661952756538347180 │ 0 │ │ 44 │ User5 │ 1661952756538347180 │ 0 │ └────┴───────┴─────────────────────────────┴─────────────────────────────┘The table also contains the following timestamp columns:
__data_transfer_commit_timeand__data_transfer_delete_time.
-
-
In the source MySQL® table
x_tab, delete the row withid=41and update the row withid=42:-
Run the following queries:
DELETE FROM x_tab WHERE id = 41; UPDATE x_tab SET name = 'Key3' WHERE id = 42;
-
Make sure the changes have been applied to the
x_tabtable on the ClickHouse® target:SELECT * FROM <ClickHouse®_database_name>.x_tab WHERE id in (41,42);Result:
┌─id─┬─name──┬─__data_transfer_commit_time─┬─__data_transfer_delete_time─┐ │ 41 │ User2 │ 1661952756538347180 │ 0 │ │ 42 │ User3 │ 1661952756538347180 │ 0 │ └────┴───────┴─────────────────────────────┴─────────────────────────────┘ ┌─id─┬─name─┬─__data_transfer_commit_time─┬─__data_transfer_delete_time─┐ │ 41 │ ᴺᵁᴸᴸ │ 1661953256000000000 │ 1661953256000000000 │ └────┴──────┴─────────────────────────────┴─────────────────────────────┘ ┌─id─┬─name─┬─__data_transfer_commit_time─┬─__data_transfer_delete_time─┐ │ 42 │ Key3 │ 1661953280000000000 │ 0 │ └────┴──────┴─────────────────────────────┴─────────────────────────────┘
Query data in ClickHouse®
For table recovery, ClickHouse® targets with replication use the ReplicatedReplacingMergeTree
-
__data_transfer_commit_time: Time the row was updated to this value, inTIMESTAMPformat. -
__data_transfer_delete_time: Time the row was deleted from the source, inTIMESTAMPformat. A value of0indicates that the row is still active.The
__data_transfer_commit_timecolumn is essential for the ReplicatedReplacedMergeTree engine. If a record is deleted or updated, a new row gets inserted with a value in this column. Querying by the primary key alone returns several records with different__data_transfer_commit_timevalues.
The source data can be added or deleted while the transfer is in Replicating status. For standard SQL command behavior, where the primary key returns a single record, add filtering by the __data_transfer_delete_time column when querying tables transferred to ClickHouse®. For example, to query the x_tab table, use the following syntax:
SELECT * FROM <ClickHouse®_database_name>.x_tab FINAL
WHERE __data_transfer_delete_time = 0;
To simplify the SELECT queries, create a view filtering rows by __data_transfer_delete_time. Use this view for all your queries. For example, to query the x_tab table, use the following syntax:
CREATE VIEW x_tab_view AS SELECT * FROM <ClickHouse®_database_name>.x_tab FINAL
WHERE __data_transfer_delete_time == 0;
Delete the resources you created
Note
Before deleting the created resources, deactivate the transfer.
To reduce the consumption of resources, delete those you do not need:
-
In the terminal window, go to the directory containing the infrastructure plan.
Warning
Make sure the directory has no Terraform manifests with the resources you want to keep. Terraform deletes all resources that were created using the manifests in the current directory.
-
Delete resources:
-
Run this command:
terraform destroy -
Confirm deleting the resources and wait for the operation to complete.
All the resources described in the Terraform manifests will be deleted.
-
ClickHouse® is a registered trademark of ClickHouse, Inc