[Yandex Cloud documentation](../../index.md) > [Tutorials](../index.md) > [Building a data platform](index.md) > Migrating a database from Managed Service for MySQL® to Object Storage using Data Transfer

# Migrating databases from Yandex Managed Service for MySQL® to Yandex Object Storage

# Loading data from MySQL® to Yandex Object Storage using Yandex Data Transfer


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

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](../../managed-mysql/pricing.md)).
* Public IP addresses if public access is enabled for cluster hosts (see [Virtual Private Cloud pricing](../../vpc/pricing.md)).
* Object Storage bucket: use of storage, data operations (see [Object Storage pricing](../../storage/pricing.md)).


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

Set up your infrastructure:

{% list tabs group=instructions %}

- Manually {#manual}

    1. [Create a Managed Service for MySQL® source cluster](../../managed-mysql/operations/cluster-create.md) with your preferred configuration.
    1. [Create a bucket in Yandex Object Storage](../../storage/operations/buckets/create.md).


- 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-objs.tf](https://github.com/yandex-cloud-examples/yc-data-transfer-from-mysql-to-object-storage/blob/main/data-transfer-mmy-objs.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.
        * Service account with the `editor`, `storage.editor`, and `storage.uploader` roles.
        * Object Storage bucket.
        * Source endpoint.
        * Transfer.

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

        * `folder_id`: [ID of the folder](../../resource-manager/operations/folder/get-id.md) for the new resources.
        * `sa_name`: Name of the service account for creating a bucket and for use in the target endpoint.
        * `bucket_name`: Object Storage bucket name.
        * Managed Service for MySQL® source cluster parameters to use for the [source endpoint](../../data-transfer/operations/endpoint/source/mysql.md#managed-service):

            * `source_mysql_version`: MySQL® version.
            * `source_db_name`: Database name.
            * `source_user` and `source_password`: Database owner username and password.
        * `transfer_enabled`: Set to `0` to ensure that no transfer is created before you [manually create the target endpoint](#prepare-transfer).

    1. Make sure the Terraform configuration files are correct 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](../../managed-mysql/operations/connect/index.md).

1. Populate the database with test data. In this example, we will use a table containing information from car sensors.

    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 your transfer {#prepare-transfer}

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

    * **Database type**: `Object Storage`.
    * **Endpoint settings**:

        * **Connection settings**:

          * **Bucket**: Specify the Object Storage bucket name.

          
          * **Service account**: Select an existing service account or create a new one with the `storage.uploader` role.


        * **Folder name**: `measurements`.

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**:
                * **Connection type**: `Managed Service for MySQL cluster`.

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

              * **Included tables**, **Excluded tables**: Regular expressions for the tables you want to transfer and for those you want to ignore. These parameters are optional.

        1. [Create a transfer](../../data-transfer/operations/transfer.md#create) of the **_Snapshot_** 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-objs.tf` file, specify the following variables:

            * `target_endpoint_id`: Target endpoint ID.
            * `transfer_enabled`: `1` to create a transfer.
            * `include_tables_regex`, `exclude_tables_regex`: Regular expressions for the tables you want to transfer and for those you want to ignore. These variables are optional.

        1. Make sure the Terraform configuration files are correct 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 **Completed**.
1. Make sure the data has been transferred from the Managed Service for MySQL® source cluster to the Object Storage bucket:

    1. In the [management console](https://console.yandex.cloud), select the folder containing your bucket.
    1. Navigate to **Object Storage**.
    1. Select the bucket from the list.
    1. Navigate to the **Objects** tab.
    1. Make sure the Object Storage bucket contains the `measurements` directory with the `<source_cluster_database_name>_measurements` object with test data.

## 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. Delete the rest of the 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 Object Storage bucket](../../storage/operations/buckets/delete.md).
       1. [Delete the Managed Service for MySQL® cluster](../../managed-mysql/operations/cluster-delete.md).

       
       1. If you created a service account when creating the target endpoint, [delete it](../../iam/operations/sa/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 %}