[Yandex Cloud documentation](../../../index.md) > [Yandex Managed Service for MySQL®](../../index.md) > [Tutorials](../index.md) > [Migrating a database from a third-party MySQL® cluster](index.md) > Transferring data using Data Transfer

# Transferring data from a third-party MySQL® cluster using Data Transfer

# Migrating data via 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](../../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 the number of transferred data rows (see [Data Transfer pricing](../../../data-transfer/pricing.md)).


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

1. [Prepare the source cluster](../../../data-transfer/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](../../operations/cluster-create.md) with your preferred 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® guide](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](../../concepts/settings-list.md#setting-sql-mode) must be the same as in the source cluster.

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

        1. [Create a source endpoint](../../../data-transfer/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](../../../data-transfer/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](../../../data-transfer/operations/transfer.md#create) of the _**Snapshot and increment**_-type that will use the endpoints you created.
        1. [Activate](../../../data-transfer/operations/transfer.md#activate) the transfer.

            {% note warning %}

            Do not make any data schema changes in the source or target cluster during the transfer. For more information, see [Working with databases during transfer](../../../data-transfer/operations/db-actions.md).

            {% endnote %}

    - Terraform {#tf}

        1. [Prepare the source cluster](../../../data-transfer/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](../../../data-transfer/operations/endpoint/source/mysql.md#on-premise).
            * [Target endpoint settings](../../../data-transfer/operations/endpoint/target/mysql.md#managed-service) inherited from the target 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](../../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. Validate your Terraform configuration files using this command:

            ```bash
            terraform validate
            ```

            Terraform will display any configuration errors detected in your 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 be activated automatically as soon as it is created.

    {% 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](../../../data-transfer/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](../../../data-transfer/operations/transfer.md#deactivate) the transfer and wait for its status to change to **Stopped**.

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

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

To minimize resource consumption, delete the resources you no longer need:

{% list tabs group=instructions %}

- Manually {#manual}

    1. [Delete the Managed Service for MySQL® cluster](../../operations/cluster-delete.md).
    1. [Delete the stopped transfer](../../../data-transfer/operations/transfer.md#delete).
    1. [Delete the source and target endpoints](../../../data-transfer/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).