AnalyticDB for MySQL Edition allows you to use external tables to import and export data. This topic describes how to use external tables of AnalyticDB for MySQL to import data from a self-managed MySQL database hosted on an Elastic Compute Service (ECS) instance to an AnalyticDB for MySQL cluster.

Prerequisites

  • A self-managed MySQL database is created and hosted on an ECS instance.
  • The ECS instance and the AnalyticDB for MySQL cluster are deployed in the same virtual private cloud (VPC).
  • A source table is created on the self-managed MySQL database. Test data is prepared.
    In this example, the test_adb self-managed MySQL database hosted on the ECS instance is used as the source database. A source table named goods is created in the database. The following statement is used to create the source table:
    CREATE TABLE goods (
    goods_id bigint(20) NOT NULL,
    price double NOT NULL,
    class bigint(20) NOT NULL,
    name varchar(32) NOT NULL,
    update_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (goods_id)
    );
  • Optional. If the AnalyticDB for MySQL cluster is in elastic mode, you must turn on ENI in the Network Information section of the Cluster Information page. Enable ENI

Procedure

  1. Connect to the AnalyticDB for MySQL cluster. For more information, see Connect to a cluster.
  2. Create a destination database. For more information, see Create a database.

    In this example, the database named adb_demo is used as the destination database in the AnalyticDB for MySQL cluster.

  3. Create an external mapping table.

    Execute the following statement to create an external mapping table named goods_external_table in the adb_demo destination database:

    CREATE TABLE IF NOT EXISTS goods_external_table (
            goods_id bigint(20) NOT NULL,
            price double NOT NULL,
            class bigint(20) NOT NULL,
            name varchar(32) NOT NULL,
            update_time timestamp,
            PRIMARY KEY (goods_id)
     )
            ENGINE='mysql'  
            TABLE_PROPERTIES='{  
            "url":"jdbc:mysql://mysql-vpc-address:3306/test_adb",  
            "tablename":"goods",  
            "username":"mysql-user-name",  
            "password":"mysql-user-password"  
      }';
    Parameter Description
    ENGINE='mysql' The storage engine used for the external table. In this example, MySQL is used.
    TABLE_PROPERTIES The connection information used by the AnalyticDB for MySQL cluster to access the data in the self-managed MySQL database hosted on the ECS instance.
    url The Primary Private IP Address value or the VPC endpoint of the ECS instance, and the name of the source database. In this example, the name of the source database is test_adb. Perform the following steps to view the VPC endpoint of the ECS instance:
    1. Log on to the ECS console and find the ECS instance.
    2. In the Network Information section on the Instance Details tab, view the Primary Private IP Address value of the ECS instance.

    The endpoint is in the "jdbc:mysql://mysql-vpc-address:3306/ecs-database-name" format.

    Example: jdbc:mysql://192.168.128.***:3306/test_adb

    tablename The name of the source table in the self-managed MySQL database hosted on the ECS instance. In this example, goods is used.
    username The account used to connect to the self-managed MySQL database hosted on the ECS instance.
    password The password of the preceding account.
  4. Create a destination table.

    Execute the following statement to create a destination table named mysql_import_test in the adb_demo database. The destination table is used to store the data that is imported from the self-managed MySQL database hosted on the ECS instance.

    CREATE TABLE IF NOT EXISTS  mysql_import_test (
            goods_id bigint(20) NOT NULL,
            price double NOT NULL,
            class bigint(20) NOT NULL,
            name varchar(32) NOT NULL,
            update_time timestamp,
            PRIMARY KEY (goods_id)
     )
    DISTRIBUTED BY HASH(goods_id);
  5. Import data from the self-managed MySQL database hosted on the ECS instance to the AnalyticDB for MySQL cluster.

    Execute the following statement:

    REPLACE INTO mysql_import_test
    SELECT * FROM goods_external_table;

What to do next

After the data is imported, you can log on to the adb_demo destination database in the AnalyticDB for MySQL cluster. Then, execute the following statement to check whether the data is imported from the source table to the mysql_import_test destination table:
SELECT * FROM mysql_import_test LIMIT 100;