[Yandex Cloud documentation](../../index.md) > [Yandex Data Transfer](../index.md) > [Tutorials](index.md) > Uploading data to data marts > Object Storage in MySQL®

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

# Migrating data from Object Storage to a Yandex Managed Service for MySQL® cluster using Yandex Data Transfer


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

To transfer data:

1. [Prepare your test data](#prepare-data).
1. [Set up and activate the transfer](#prepare-transfer).
1. [Test your transfer](#verify-transfer).

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


## Required paid resources {#paid-resources}

* Object Storage bucket: use of storage, data operations (see [Object Storage pricing](../../storage/pricing.md)).
* Managed Service for MySQL® cluster, which includes 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)).


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

Set up your infrastructure:

{% list tabs group=resources %}

- Manually {#manual}

    1. [Create a Yandex Object Storage bucket](../../storage/operations/buckets/create.md).

    
    1. [Create a service account](../../iam/operations/sa/create.md#create-sa) named `storage-viewer` with the `storage.viewer` role. The transfer will use it to access the bucket.
    1. [Create a static access key](../../iam/operations/authentication/manage-access-keys.md#create-access-key) for the `storage-viewer` service account.


    1. [Create a target Managed Service for MySQL® cluster](../../managed-mysql/operations/cluster-create.md) with the following settings:

        * **DB name**: `db1`.
        * **Username**: `mmy-user`.
        * **Password**: `<user_password>`.

    1. [Assign](../../managed-mysql/operations/grant.md#grant-role) the `ALL_PRIVILEGES` role for the target database to the MySQL® user.

- 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-objs-mmy.tf](https://github.com/yandex-cloud-examples/yc-data-transfer-from-object-storage-to-mysql/tree/master/data-transfer-objs-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 required for connecting to the Managed Service for MySQL® cluster.
        * Service account to use for creating and accessing the bucket.
        * Yandex Lockbox secret for the service account static key required to configure the source endpoint.
        * Object Storage source bucket.
        * Managed Service for MySQL® target cluster.
        * Target endpoint.
        * Transfer.

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

        * `folder_id`: [ID of the folder](../../resource-manager/operations/folder/get-id.md) for the new resources.
        * `bucket_name`: Bucket name consistent with the [naming conventions](../../storage/concepts/bucket.md#naming).
        * `mmy_password`: MySQL® user 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).

{% endlist %}

## Prepare your test data {#prepare-data}

1. On your local machine, create a `data.csv` text file and fill it with test data. In our example, we will use readings from car sensors:

    ```csv
    1;99101;2022-06-05 17:27:00;55.70329032;37.65472196;427.5;52.3;23.5;17.;52.
    2;95106;2022-06-06 09:49:54;55.71294467;37.66542005;429.13;55.5;21.;18.;32.
    3;117890;2023-06-07 06:21:29;55.71294467;37.66542005;429.13;56.2;20.;18.7;45.
    4;99101;2022-06-07 08:15:32;55.29194467;37.66542005;429.13;59.1;21.;18.;20.
    ```

1. [Upload](../../storage/operations/objects/upload.md#simple) the file to the Object Storage bucket you created earlier.

## Set up and activate the transfer {#prepare-transfer}

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

    * **Database type**: `Object Storage`.
    * **Bucket**: Object Storage bucket name.

    
    * **Access Key ID**: Service account’s static access key ID. If you created your infrastructure using Terraform, [copy the key’s value from the Yandex Lockbox secret](../../lockbox/operations/secret-get-info.md#secret-contents).
    * **Secret Access Key**: Service account’s secret access key. If you created your infrastructure using Terraform, [copy the key’s value from the Yandex Lockbox secret](../../lockbox/operations/secret-get-info.md#secret-contents).


    * **Endpoint**: `https://storage.yandexcloud.net`.
    * **Region**: `ru-central1`.
    * **Data format**: `CSV`.
    * **Delimiter**: Semicolon (`;`).
    * **Table**: `measurements`.
    * **Add system columns**: Disable this option.
    * **Result table schema**: Select `Manual` and specify the following field names and data types:

        * `id`: `INT64`, **Key** property
        * `device_id`: `INT32`
        * `datetime`: `DATETIME`
  
           {% note info %}

           The target endpoint writes `DATETIME` type data in the UTC+0 time zone.

           {% endnote %}

        * `latitude`: `DOUBLE`
        * `longitude`: `DOUBLE`
        * `altitude`: `DOUBLE`
        * `speed`: `DOUBLE`
        * `battery_voltage`: `DOUBLE`
        * `cabin_temperature`: `DOUBLE`
        * `fuel_level`: `DOUBLE`

    Keep the default values for all other settings.

1. Create a target endpoint and transfer:

    {% list tabs group=resources %}

    - Manually {#manual}

        1. [Create a target endpoint](../operations/endpoint/target/postgresql.md):

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

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

    - Terraform {#tf}

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

            * `source_endpoint_id`: Source endpoint ID.
            * `transfer_enabled`: Set to `1` to create a transfer.

        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 for the transfer:

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

1. Wait for the transfer status to change to **Replicating**.
1. [Connect to the Managed Service for MySQL® target cluster database](../../managed-mysql/operations/connect/index.md).
1. To make sure the data was successfully transferred, run this query:

    ```sql
    SELECT * FROM db1.measurements;
    ```

    {% cut "Response example" %}

    ```text
     id | device_id |     datetime        |  latitude | longitude | altitude | speed | battery_voltage | cabin_temperature | fuel_level
    ----------------+---------------------+-----------+-----------+----------+-------+-----------------+-------------------+----------+
      1 |     99101 | 2022-06-05T17:27:00 | 55.703289 |  37.66542 |   427.50 | 52.29 |            23.5 |              17.0 |     52.0 |
      2 |     95106 | 2022-06-06T09:49:54 | 55.712944 |  37.66542 |   429.13 | 55.50 |            21.0 |              18.0 |     32.0 |
      3 |    117890 | 2023-06-07T06:21:29 | 55.712944 |  37.66542 |   429.13 | 56.20 |            20.0 |              18.7 |     45.0 |
      4 |     99101 | 2022-06-05 17:27:00 | 55.291944 |  37.66542 |   429.13 | 59.09 |            21.0 |              18.0 |     20.0 |
    ```

    {% endcut %}

1. Create a text file named `data2.csv` with the new data:

    ```csv
    7;95106;2022-06-07 09:54:32;47.71294467;37.66542005;429.13;62.2;21.;20.4;27.
    9;117890;2022-06-07 11:56:27;52.71294467;37.66542005;429.13;65.5;19.;19.;38.
    10;117890;2022-06-07 12:36:27;57.71294467;37.66542005;429.13;72.1;19.;20.;25.
    11;99101;2022-06-07 15:22:27;59.71294467;37.66542005;429.13;35.5;19.;18.;12.
    ```

1. Upload the file to the Object Storage bucket for transfer.
1. Make sure the `db1.measurements` table now contains the new data.

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

{% note info %}

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

{% endnote %}

To reduce the consumption of resources, delete those you do not need:

1. [Delete the transfer](../operations/transfer.md#delete).
1. [Delete the target endpoint](../operations/endpoint/index.md#delete).
1. Delete the other resources depending on how you created them:

   {% list tabs group=resources %}

   - Manually {#manual}

       1. [Delete the source endpoint](../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 %}