[Yandex Cloud documentation](../../index.md) > [Yandex MPP Analytics for PostgreSQL](../index.md) > [Tutorials](index.md) > Migrating a database from MySQL® to Greenplum®

# Migrating data from Yandex Managed Service for MySQL® to Yandex MPP Analytics for PostgreSQL using Yandex Data Transfer

# Migrating data from Yandex Managed Service for MySQL® to Yandex MPP Analytics for PostgreSQL using Yandex Data Transfer

You can set up a data transfer from Yandex Managed Service for MySQL® to Greenplum® databases in Yandex MPP Analytics for PostgreSQL using Yandex Data Transfer. Follow these steps:

1. [Prepare your test data](#prepare-data).
1. [Create a database in the target cluster](#prepare-data).
1. [Prepare and activate your 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: use of computing resources allocated to hosts, storage and backup size (see [Managed Service for MySQL® pricing](../../managed-mysql/pricing.md)).
* Yandex MPP Analytics for PostgreSQL cluster: use of computing resources allocated to hosts, storage and backup size (see [Yandex MPP Analytics for PostgreSQL pricing](../pricing/index.md)).
* Public IP addresses if public access is enabled for cluster hosts (see [Yandex Virtual Private Cloud pricing](../../vpc/pricing.md)).


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

Set up your infrastructure:

{% list tabs group=instructions %}

- Manually {#manual}

    {% note info %}
    
    Public access to cluster hosts is required if you plan to connect to the cluster via the internet. This connection option is simpler and is recommended for the purposes of this guide. You can connect to non-public hosts as well but only from Yandex Cloud virtual machines located in the same cloud network as the cluster.
    
    {% endnote %}

    1. [Create a Managed Service for MySQL® source cluster](../../managed-mysql/operations/cluster-create.md#create-cluster) in any [availability zone](../../overview/concepts/geo-scope.md) with publicly available hosts in any suitable configuration and the following settings:

        * **DB name**: `mmy_db`.
        * **Username**: `mmy_user`.
        * **Password**: `<source_password>`.

    1. [Grant](../../managed-mysql/operations/grant.md#grant-privilege) the `REPLICATION CLIENT` and `REPLICATION SLAVE` administrative privileges to `mmy_user`.

        For more information about administrative privileges, see the [settings description](../../managed-mysql/concepts/settings-list.md#setting-administrative-privileges).

    1. In the same availability zone, [create a Greenplum® target cluster](../operations/cluster-create.md#create-cluster) in any suitable configuration with publicly available hosts and the following settings:

        * **Username**: `mgp_user`.
        * **Password**: `<target_password>`.
        * **Access from Data Transfer**: Enabled.

    1. Make sure the cluster security groups are configured correctly and allow inbound cluster connections:

        * [Managed Service for MySQL®](../../managed-mysql/operations/connect/index.md#configure-security-groups).
        * [Yandex MPP Analytics for PostgreSQL](../operations/connect/index.md#configuring-security-groups).

- Using 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 [mmy-to-mgp.tf](https://github.com/yandex-cloud-examples/yc-data-transfer-from-mysql-to-greenplum/blob/main/mmy-to-mgp.tf) configuration file to the same working directory.

        This file describes:

        * [Networks](../../vpc/concepts/network.md#network) and [subnets](../../vpc/concepts/network.md#subnet) that will host your clusters.
        * [Security groups](../../vpc/concepts/security-groups.md) for cluster access.
        * Managed Service for MySQL® source cluster.
        * Greenplum® target cluster in Yandex MPP Analytics for PostgreSQL.
        * Source endpoint.
        * Transfer.

    1. In the `mmy-to-mgp.tf` file, specify the following:

        * MySQL® and Greenplum® versions.
        * MySQL® and Greenplum® user passwords.

    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. [Connect](../../managed-mysql/operations/connect/index.md) to the `mmy_db` database in the Managed Service for MySQL® source cluster.

1. Create a simple table named `table1`:

    ```sql
    CREATE TABLE table1 (
    id int NOT NULL,
    name varchar (10),
    PRIMARY KEY (id)
    );
    ```

1. Populate the table with data:

    ```sql
    INSERT INTO table1 VALUES
    (1, 'Name1'),
    (2, 'Name2'),
    (3, 'Name3');
    ```

## Create a database in the target cluster {#prepare-data}

1. [Connect](../operations/connect/index.md) to the `postgres` system database in the Greenplum® target cluster as `mgp_user`.

1. Create a database named `mgp_db`:

    ```sql
    CREATE DATABASE mgp_db;
    ```

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

1. [Create a `Greenplum®` target endpoint](../../data-transfer/operations/endpoint/target/greenplum.md) with these cluster connection settings:

    * **Connection type**: `Cluster`.
    * **Cluster**: `<Greenplum®_target_cluster_name>` from the drop-down list.
    * **Database**: `mgp_db`.
    * **User**: `mgp_user`.
    * **Password**: `<user_password>`.

1. Create a source endpoint and set up the transfer:

    {% list tabs group=instructions %}

    * Manually {#manual}

        1. [Create a `MySQL®` source endpoint](../../data-transfer/operations/endpoint/source/mysql.md) with these cluster connection settings:

            * **Connection type**: `Managed Service for MySQL cluster`.
            * **Managed Service for MySQL cluster**: `<MySQL®_source_cluster_name>` from the drop-down list.
            * **Database**: `mmy_db`.
            * **User**: `mmy_user`.
            * **Password**: `<user_password>`.

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

    - Using Terraform {#tf}

        1. In the `mmy-to-mgp.tf` file, specify the following settings:

            * `target_endpoint_id`: Target 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:

            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.

        1. The transfer will be activated automatically. Wait for its status to change to **Replicating**.

    {% endlist %}

## Test the transfer {#verify-transfer}

Make sure the transfer works correctly by testing copying and replication.

### Test copying {#verify-copy}

1. [Connect](../operations/connect/index.md) to the `mgp_db` database in the Greenplum® target cluster.

1. Run this request:

    ```sql
    SELECT * FROM mmy_db.table1;
    ```

### Test replication {#verify-replication}

1. [Connect](../../managed-mysql/operations/connect/index.md) to the `mmy_db` database in the Managed Service for MySQL® source cluster.

1. Populate the `table1` table with data:

    ```sql
    INSERT INTO table1 VALUES
    (4, 'Name4');
    ```

1. Check that the added row appears in the target database:

    1. [Connect](../operations/connect/index.md) to the database `mgp_db` in the Greenplum® target cluster.
    1. Run this request:

        ```sql
        SELECT * FROM mmy_db.table1;
        ```

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

{% note info %}

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

{% endnote %}

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

{% list tabs group=instructions %}

- Manually {#manual}

    1. [Delete the transfer](../../data-transfer/operations/transfer.md#delete).
    1. [Delete the endpoints](../../data-transfer/operations/endpoint/index.md#delete).
    1. [Delete the Managed Service for MySQL® cluster](../../managed-mysql/operations/cluster-delete.md).
    1. [Delete the Greenplum® cluster](../operations/cluster-delete.md).

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