[Yandex Cloud documentation](../../index.md) > [Yandex Managed Service for MySQL®](../index.md) > [Tutorials](index.md) > Migrating a database to Yandex Managed Service for YDB using Yandex Data Transfer

# Migrating a database to Yandex Managed Service for YDB using Yandex Data Transfer

# Migrating data from MySQL® to YDB with a storage change using Yandex Data Transfer


Data Transfer enables you to transfer data from a Managed Service for MySQL® source cluster to Managed Service for YDB.

To transfer data:

1. [Prepare the source cluster](#prepare-source).
1. [Prepare and activate the transfer](#prepare-transfer).
1. [Test the transfer](#verify-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)).
* Managed Service for YDB database (see [Managed Service for YDB pricing](../../ydb/pricing/index.md)). Its cost depends on the deployment mode:

    * In serverless mode, you pay for data operations and storage volume, including stored backups.
    * In dedicated instance mode, you pay for the use of computing resources allocated to the database, storage and backup size.


## Set up your infrastructure {#deploy-infrastructure}

{% list tabs group=instructions %}

- Manually {#manual}

    1. [Create a Managed Service for MySQL® source cluster](../operations/cluster-create.md) of any suitable configuration.

    1. [Create a Managed Service for YDB database](../../ydb/operations/manage-databases.md) of any suitable configuration.

    
    1. If using security groups, [configure them](../../managed-kafka/operations/connect/index.md#configuring-security-groups) to allow internet access to your cluster.



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

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

        * Managed Service for MySQL® source cluster parameters to use for the [source endpoint](../../data-transfer/operations/endpoint/target/mysql.md#managed-service):

            * `source_mysql_version`: MySQL® version.
            * `source_db_name`: Database name.
            * `source_user` and `source_password`: Database owner username and password.

        * `target_db_name`: Managed Service for YDB database name.

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

{% endlist %}

## Prepare the source cluster {#prepare-source}

1. If you created the infrastructure manually, [set up your source cluster](../../data-transfer/operations/prepare.md#source-my).

1. [Connect to the Managed Service for MySQL® source cluster](../operations/connect/index.md).

1. Populate the database with test data. In this example, we will use a simple table with car sensor information.

    Create a table:

    ```sql
    CREATE TABLE measurements (
        device_id varchar(200) NOT NULL,
        datetime timestamp NOT NULL,
        latitude real NOT NULL,
        longitude real NOT NULL,
        altitude real NOT NULL,
        speed real NOT NULL,
        battery_voltage real,
        cabin_temperature real NOT NULL,
        fuel_level real,
        PRIMARY KEY (device_id)
    );
    ```

    Populate the table with data:

    ```sql
    INSERT INTO measurements VALUES
        ('iv9a94th6rzt********', '2022-06-05 17:27:00', 55.70329032, 37.65472196,  427.5,    0, 23.5, 17, NULL),
        ('rhibbh3y08qm********', '2022-06-06 09:49:54', 55.71294467, 37.66542005, 429.13, 55.5, NULL, 18, 32);
    ```

## Prepare and activate a transfer {#prepare-transfer}

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

    * **Database type**: `YDB`.
    * **Endpoint parameters**:

        * **Connection settings** → **Database**: Select the Managed Service for YDB database from the list.

        
        * **Service account ID**: Select an existing service account or create a new one with the `ydb.editor` role.


1. Create a source endpoint and transfer:

    {% list tabs group=instructions %}

    - Manually {#manual}

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

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

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

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

    - Terraform {#tf}

        1. In the `data-transfer-mmy-ydb.tf` file, uncomment the following:

            * `target_endpoint_id` variable. Also, set it to the ID of the target endpoint created in the previous step.
            * `yandex_datatransfer_endpoint` and `yandex_datatransfer_transfer` resources.

        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.

            The transfer will be activated automatically upon creation.

    {% endlist %}

## Test the transfer {#verify-transfer}

1. Wait for the transfer status to change to **Replicating**.

1. Make sure the data from the Managed Service for MySQL® source cluster has been transferred to the Managed Service for YDB database:

    {% list tabs group=instructions %}

    - Management console {#console}

        1. In the [management console](https://console.yandex.cloud), select the folder containing your database.
        1. Navigate to **Managed Service for&nbsp;YDB**.
        1. Select your database from the list.
        1. Navigate to the **Navigation** tab.
        1. Check that the Managed Service for YDB database contains the `<source_cluster_DB_name>_measurements` table with test data.

    - CLI {#cli}

        1. [Connect to the Managed Service for YDB database](../../ydb/operations/connection.md).
        1. Check that the database contains the `<source_cluster_DB_name>_measurements` table with test data:

            ```sql
            SELECT *
            FROM <source_cluster_DB_name>_measurements;
            ```

    {% endlist %}

1. [Connect to the Managed Service for MySQL® source cluster](../operations/connect/index.md) and populate the `measurements` table with data:

    ```sql
    INSERT INTO measurements VALUES
        ('iv7b74th678t********', '2020-06-08 17:45:00', 53.70987913, 36.62549834, 378.0, 20.5, 5.3, 20, NULL);
    ```

1. Check that the Managed Service for YDB database shows information about the added row:

    {% list tabs group=instructions %}

    - Management console {#console}

        1. In the [management console](https://console.yandex.cloud), select the folder containing your database.
        1. Navigate to **Managed Service for&nbsp;YDB**.
        1. Select your database from the list.
        1. Navigate to the **Navigation** tab.
        1. Make sure the `<source_cluster_DB_name>_measurements` table now contains the new data.

    - CLI {#cli}

        1. [Connect to the Managed Service for YDB database](../../ydb/operations/connection.md).
        1. Make sure the `<source_cluster_DB_name>_measurements` table now contains the new data:

            ```sql
            SELECT *
            FROM <source_cluster_DB_name>_measurements;
            ```

    {% endlist %}

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

{% note info %}

Before deleting any resources, [deactivate the transfer](../../data-transfer/operations/transfer.md#deactivate).

{% endnote %}

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

1. [Delete the transfer](../../data-transfer/operations/transfer.md#delete).
1. [Delete the target endpoint](../../data-transfer/operations/endpoint/index.md#delete).


1. If you created a service account when creating the target endpoint, [delete it](../../iam/operations/sa/delete.md).


1. Delete the other resources depending on how you created them:

   {% list tabs group=instructions %}

   - Manually {#manual}

       1. [Delete the source endpoint](../../data-transfer/operations/endpoint/index.md#delete).
       1. [Delete the Managed Service for YDB database](../../ydb/operations/manage-databases.md#delete-db).
       1. [Delete the Managed Service for MySQL® cluster](../operations/cluster-delete.md).

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