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

# Migrating MySQL® clusters

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

This method allows you to migrate the entire database without interrupting user service. To use it, allow connections to the source cluster from the internet.


# Transferring data using Yandex Data Transfer {#data-transfer}

To transfer a database from MySQL® to Managed Service for MySQL®:

1. [Start the data transfer](#start-transfer).
1. [Complete your data transfer](#finish-transfer).

If you no longer need the resources you created, [delete them](#clear-out).


## Required paid resources {#paid-resources}

* Managed Service for MySQL® cluster: Computing resources allocated to hosts, storage and backup size (see [Managed Service for MySQL® pricing](../../managed-mysql/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)).


## Start the data transfer {#start-transfer}

1. [Set up the source cluster](../operations/prepare.md#source-my).
1. Set up your infrastructure and start the data transfer:

    {% list tabs group=instructions %}

    - Manually {#manual}

        1. Create a [target Managed Service for MySQL® cluster](../../managed-mysql/operations/cluster-create.md) with any suitable configuration. In this case, the following applies:

            * The MySQL® version must be the same as or higher than in the source cluster.

                Data transfer with a major MySQL® version upgrade is possible but not guaranteed. For more information, see [this MySQL® article](https://dev.mysql.com/doc/refman/8.0/en/faqs-migration.html).

                Migration to an earlier MySQL® version is [not supported](https://dev.mysql.com/doc/refman/8.0/en/downgrading.html).

            * [SQL mode](../../managed-mysql/concepts/settings-list.md#setting-sql-mode) must be the same as in the source cluster.

        1. [Set up the target cluster](../operations/prepare.md#target-my).

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

            * **Database type**: `MySQL`.
            * **Endpoint parameters** → **Connection settings**: `Custom installation`.

                Configure the source cluster connection settings.

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

            * **Database type**: `MySQL`.
            * **Endpoint parameters** → **Connection settings**: `Managed Service for MySQL cluster`.

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

        1. [Create a transfer](../operations/transfer.md#create) of the _**Snapshot and increment**_-type that will use the endpoints you created.
        1. [Activate](../operations/transfer.md#activate) the transfer.

            {% note warning %}

            Avoid changing the data schema in either the source or target cluster while the transfer is in progress. For more information, see [Working with databases during transfer](../operations/db-actions.md).

            {% endnote %}

    - Terraform {#tf}

        1. [Set up the source cluster](../operations/prepare.md#source-my).

        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-mysql-mmy.tf](https://github.com/yandex-cloud-examples/yc-data-transfer-from-on-premise-mysql-to-cloud/blob/main/data-transfer-mysql-mmy.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 permitting access to the cluster.
            * Managed Service for MySQL® target cluster.
            * Source endpoint.
            * Target endpoint.
            * Transfer.

        1. In the `data-transfer-mysql-mmy.tf` file, specify the following:

            * [Source endpoint parameters](../operations/endpoint/source/mysql.md#on-premise).
            * [Target endpoint parameters](../operations/endpoint/target/mysql.md#managed-service) inherited from the cluster configuration:

                * `target_mysql_version`: MySQL® version. Make sure it is the same as or higher than in the source cluster.
                * `target_sql_mode`: [SQL mode](../../managed-mysql/concepts/settings-list.md#setting-sql-mode). It must be the same as in the source cluster.
                * `target_db_name`: Database name.
                * `target_user` and `target_password`: Database owner username and password.

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

            The transfer will activate automatically upon creation.

    {% endlist %}

## Complete your data transfer {#finish-transfer}

1. Wait for the transfer status to change to **Replicating**.
1. Switch the source cluster to <q>read-only</q> mode and move the workload to the target cluster.
1. On the [transfer monitoring](../operations/monitoring.md) page, wait until the **Maximum data transfer delay** value drops to zero. This indicates that the target cluster now contains all changes made in the source cluster after the data copy completed.
1. [Deactivate](../operations/transfer.md#deactivate) the transfer and wait for its status to change to **Stopped**.

    For more information about transfer statuses, see [Transfer lifecycle](../concepts/transfer-lifecycle.md#statuses).

## 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 MySQL® cluster](../../managed-mysql/operations/cluster-delete.md).
    1. [Delete the stopped transfer](../operations/transfer.md#delete).
    1. [Delete the source and target endpoints](../operations/endpoint/index.md#delete).

- 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 %}


For a real example of MySQL® database migration using Data Transfer, see [Syncing data from using Yandex Data Transfer](../../tutorials/dataplatform/sync-mysql.md).

## See also {#see-also}

For other migration methods, see this [Yandex Managed Service for MySQL® tutorial](../../managed-mysql/tutorials/data-migration/index.md).