[Yandex Cloud documentation](../../index.md) > [Yandex Data Transfer](../index.md) > [Tutorials](index.md) > Migration > ClickHouse® cluster

# Migrating ClickHouse® clusters

# Migrating data using Yandex Data Transfer

With Data Transfer, you can transfer your database from a third-party ClickHouse® source cluster to a Managed Service for ClickHouse® target cluster.

This method enables you to:

* Copy the database without interrupting user service.
* Eliminate the need for an intermediate VM or public internet access to your Managed Service for ClickHouse® target cluster.

You can also use this method to transfer data between two Managed Service for ClickHouse® clusters. For a successful transfer, specify the listening ports `8443` and `9440` on the source endpoint and add an [SSL certificate](../../managed-clickhouse/operations/connect/index.md#get-ssl-cert).

For more information, see [Problems addressed by Yandex Data Transfer](../concepts/use-cases.md).


## Getting started {#before-you-begin}

[Enable inbound internet connections to the source cluster](../concepts/network.md#source-external).

### Required paid resources {#paid-resources}

* Managed Service for ClickHouse® cluster: Use of computing resources allocated to hosts, storage and backup size (see [Managed Service for ClickHouse® pricing](../../managed-clickhouse/pricing.md)).
* Public IP addresses if public access is enabled for cluster hosts (see [Virtual Private Cloud pricing](../../vpc/pricing.md)).
* Each transfer: Use of computing resources and number of transferred data rows (see [Data Transfer pricing](../pricing.md)).


## Transferring data {#data-transfer}

1. [Prepare the source cluster](../operations/prepare.md#source-ch).
1. Set up the infrastructure:

    {% list tabs group=instructions %}

    - Manually {#manual}

        1. [Create a security group](../../vpc/operations/security-group-create.md) and [configure it](../../managed-clickhouse/operations/connect/index.md#configuring-security-groups).

        1. [Create a Managed Service for ClickHouse® target cluster](../../managed-clickhouse/operations/cluster-create.md) with the computing and storage capacity matching the source database’s environment.

            When creating a cluster, specify the security group you created earlier.

            The source and target database names must be the same.

            To connect to the cluster via [Yandex WebSQL](../../websql/concepts/index.md), enable **WebSQL access** in the cluster settings.

        1. [Create a source endpoint](../operations/endpoint/index.md#create):

            * **Database type**: `ClickHouse`.
            * **Endpoint parameters** → **Connection settings**: `On-premise`.

                Specify the source cluster connection settings.

        1. [Create a target endpoint](../operations/endpoint/index.md#create):

            * **Database type**: `ClickHouse`.
            * **Endpoint parameters** → **Connection settings**: `Managed cluster`.

                Select your target cluster from the list and specify its connection settings.

        1. Create a _**Snapshot**_-type [transfer](../operations/transfer.md#create), configure it to use the previously created endpoints, then [activate](../operations/transfer.md#activate) it.

    - Using Terraform {#tf}

        1. If you do not have Terraform yet, [install it](../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
        1. [Get the authentication credentials](../../tutorials/infrastructure-management/terraform-quickstart.md#get-credentials). You can add them to environment variables or specify them later in the provider configuration file.
        1. [Configure and initialize a provider](../../tutorials/infrastructure-management/terraform-quickstart.md#configure-provider). There is no need to create a provider configuration file manually, you can [download it](https://github.com/yandex-cloud-examples/yc-terraform-provider-settings/blob/main/provider.tf).
        1. Place the configuration file in a separate working directory and [specify the parameter values](../../tutorials/infrastructure-management/terraform-quickstart.md#configure-provider). If you did not add the authentication credentials to environment variables, specify them in the configuration file.

        1. Download the [data-transfer-ch-mch.tf](https://github.com/yandex-cloud-examples/yc-data-transfer-from-on-premise-clickhouse-to-cloud/blob/main/data-transfer-ch-mch.tf) configuration file to the same working directory.

            This file describes:

            * [Network](../../vpc/concepts/network.md#network).
            * [Subnet](../../vpc/concepts/network.md#subnet).
            * [Security group](../../vpc/concepts/security-groups.md) and the rule required for connecting to a cluster.
            * Managed Service for ClickHouse® target cluster.
            * Source endpoint.
            * Target endpoint.
            * Transfer.

        1. In the `data-transfer-ch-mch.tf` file, specify the following:

            * [Source endpoint parameters](../operations/endpoint/source/clickhouse.md#on-premise):
                * `source_user` and `source_pwd`: Username and password to access the source database.
                * `source_db_name`: Database name.
                * `source_host`: ClickHouse® server IP address or FQDN.
                * `source_shard`: Shard name.
                * `source_http_port` and `source_native_port`: HTTP and ClickHouse® native interface ports.

            * Target cluster parameters used as [target endpoint parameters](../operations/endpoint/target/clickhouse.md#managed-service):

                * `target_clickhouse_version`: ClickHouse® version.
                * `target_user` and `target_password`: Database owner username and password.

            * [Yandex Cloud CLI](../../cli/index.md) parameters for automating cluster activation:

                * `profile_name`: Yandex Cloud CLI profile name.

                    If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

        1. Optionally, to enable connections via [Yandex WebSQL](../../websql/concepts/index.md), add the `access` section to the cluster resource:

            ```hcl
            access {
                web_sql = true
            }
            ```

        1. Make sure the Terraform configuration files are correct using this command:

            ```bash
            terraform validate
            ```

            Terraform will show any errors found in your configuration files.

        1. Create the required infrastructure:

            1. Run this command to view the planned changes:
            
               ```bash
               terraform plan
               ```
            
               If 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.
            
            1. If everything looks correct, apply the changes:
               1. Run this command:
            
                  ```bash
                  terraform apply
                  ```
            
               1. Confirm updating the resources.
               1. 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](https://console.yandex.cloud).

            Once created, your transfer will be activated automatically.

    {% endlist %}

## Test your transfer {#verify-transfer}

1. Wait for the transfer status to change to **Completed**.
1. Check that the data has been transferred from the source ClickHouse® cluster to the Managed Service for ClickHouse® database:

    {% list tabs group=instructions %}
    
    
    - Yandex WebSQL {#websql}

        1. [Create a connection](../../websql/operations/create-connection.md#connect-cluster) to the database in the Managed Service for ClickHouse® cluster.
        1. Check that the database contains all tables from the source cluster by [running the following query](../../websql/operations/query-executor.md#execute-query) via the connection you created:

            ```sql
            SHOW TABLES FROM <DB_name>;
            ```

        1. Check that the tables contain the same data as the source cluster tables by [running the following query](../../websql/operations/query-executor.md#execute-query) against them:

            ```sql
            SELECT * FROM <DB_name>.<table_name>;
            ```


    - CLI {#cli}

        1. [Get an SSL certificate](../../managed-clickhouse/operations/connect/index.md#get-ssl-cert) to connect to the Managed Service for ClickHouse® cluster.      
        1. [Connect to the database](../../managed-clickhouse/operations/connect/clients.md#clickhouse-client) in the Managed Service for ClickHouse® cluster via `clickhouse-client`.
        1. Check that the database contains all tables from the source cluster:

            ```sql
            SHOW TABLES FROM <DB_name>;
            ```
 
        1. Check that the tables contain the same data as the source cluster tables:

            ```sql
            SELECT * FROM <DB_name>.<table_name>;
            ```

    {% endlist %}

## Delete the resources you created {#clear-out}

Some resources are not free of charge. Delete the resources you no longer need to avoid paying for them:

{% list tabs group=instructions %}

- Manually {#manual}

  1. [Delete the Managed Service for ClickHouse®](../../managed-clickhouse/operations/cluster-delete.md) cluster.
  1. [Delete the completed transfer](../operations/transfer.md#delete).
  1. [Delete the source and target endpoints](../operations/endpoint/index.md#delete).

- Using Terraform {#tf}

  1. In the terminal window, go to the directory containing the infrastructure plan.
  
      {% note 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.
  
      {% endnote %}
  
  1. Delete resources:
  
      1. Run this command:
  
          ```bash
          terraform destroy
          ```
  
      1. Confirm deleting the resources and wait for the operation to complete.
  
      All the resources described in the Terraform manifests will be deleted.

{% endlist %}