[Yandex Cloud documentation](../../index.md) > [Tutorials](../index.md) > [Building a data platform](index.md) > Migrating to a third-party MySQL® cluster

# Migrating to a third-party MySQL® cluster

To migrate a database deployed in a Yandex Managed Service for MySQL® cluster to a third-party MySQL® cluster:

1. Transfer the data.
1. Disable data writes to the source database.
1. Switch all traffic to the third-party cluster.

You can migrate your databases from one MySQL® version to another, e.g., from MySQL® 5.7 to MySQL® 8. Note that the third-party cluster must run a major MySQL® version not lower than that on your Yandex Managed Service for MySQL® cluster.


## Required paid resources {#paid-resources}

The migration infrastructure cost includes a fee for each transfer: use of computing resources and the number of transferred data rows (see [Data Transfer pricing](../../data-transfer/pricing.md)).


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

Create a MySQL® cluster with your preferred configuration. In this case, the following applies:

* The MySQL® version must be the same or higher than the version 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](../../managed-mysql/concepts/settings-list.md#setting-sql-mode) must be the same as in the source cluster.

## Transferring data {#data-transfer}

# Migrating data via Yandex Data Transfer {#data-transfer}

1. [Set up the source cluster database](../../data-transfer/operations/prepare.md#source-my).
1. [Set up the target cluster database](../../data-transfer/operations/prepare.md#target-my).
1. Create endpoints and a transfer:

    {% list tabs group=instructions %}

    - Manually {#manual}

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

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

                Specify the source cluster ID.

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

            * **Database type**: `MySQL`.
            * **Connection settings**: `Custom installation`.
                
                Configure the target cluster connection settings.

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

        1. [Activate the transfer](../../data-transfer/operations/transfer.md#activate).

    - Terraform {#tf}

        1. If you do not have Terraform yet, [install it](../infrastructure-management/terraform-quickstart.md#install-terraform).
        1. [Get the authentication credentials](../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](../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](../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-mmy-mysql.tf](https://github.com/yandex-cloud-examples/yc-data-transfer-from-cloud-mysql-to-on-premise/blob/main/data-transfer-mmy-mysql.tf) file with the transfer and endpoint configuration file to the same working directory.

        1. Specify the following in the configuration file:

            * [Source endpoint parameters](../../data-transfer/operations/endpoint/source/mysql.md#managed-service).
            * [Target endpoint parameters](../../data-transfer/operations/endpoint/target/mysql.md#on-premise).

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

    {% 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](../../data-transfer/operations/db-actions.md).

    {% endnote %}

1. Wait for the transfer status to change to **Replicating**.
1. Switch the source cluster to "read-only" 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-transfer) 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).

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

    {% list tabs group=instructions %}

    - Manually {#manual}

      1. [Ensure the transfer is stopped, then delete it](../../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 %}

## Useful links {#see-also}

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