All Products
Search
Document Center

AnalyticDB:Use the mysql_fdw extension to access MySQL data

Last Updated:Jul 25, 2024

AnalyticDB for PostgreSQL allows you to use the mysql_fdw extension to access, add, remove, modify, and query data on the MySQL server without the need to write the data to AnalyticDB for PostgreSQL.

Usage notes

When you perform DML operations, such as UPDATE, DELETE, and INSERT, on a MySQL foreign table, the source table must use the first column as the unique primary key column or a part of the composite primary key. The primary key constraint limits do not apply to the SELECT operation.

Prerequisites

Getting started

To use the mysql_fdw extension to access data on a remote MySQL database, perform the following steps:

  1. Log on to the AnalyticDB for PostgreSQL console. Find the AnalyticDB for PostgreSQL instance that you want to manage and click the instance ID to go to the Basic Information page of the instance.

  2. In the upper-right corner of the page, click Log On to Database. In the Log On to Database Instance dialog box, configure the Database Account and Database Password parameters and click Login.

  3. Enter a database and execute the CREATE SERVER statement to create a foreign server as the MySQL database that you want to access. Sample statements:

    -- Create a foreign server named mysql_svr.
    CREATE SERVER mysql_svr FOREIGN DATA WRAPPER mysql_fdw
      OPTIONS (host '127.0.0.1', port '3306');
    
    -- Create a user mapping for the mysql_svr server by specifying the username and the password that are used to access the MySQL database.
    CREATE USER MAPPING FOR public SERVER mysql_svr
      OPTIONS (username '$MYSQL_USER_NAME', password '$MYSQL_PASS');
    
    -- Use the mysql_svr server to create a foreign table.
    CREATE FOREIGN TABLE test_foreign_table(
      c1 INT,
      c2 INT,
      c3 TEXT,
      c4 TEXT)
      SERVER mysql_svr OPTIONS(dbname 'mysql_fdw_database', table_name 'test_foreign_table');

    Parameters:

    • CREATE SERVER: creates a foreign server. The foreign server specifies the MySQL database that you want to access.

    • CREATE USER MAPPING: creates a user mapping. You must specify the username and the password that are used to access the MySQL database.

    • CREATE FOREIGN TABLE: creates a foreign table. Column names of the foreign table must match the column names of the remote table that you want to access. You can also execute the IMPORT FOREIGN SCHEMA statement to allow the mysql_fdw extension to automatically create a foreign table on the AnalyticDB for PostgreSQL instance for the MySQL table that you want to access.

  4. Perform the query, insert, add, and remove operations on data of the MySQL database. Only the preceding operations are supported.

Syntax

CREATE SERVER

Create a foreign server.

CREATE SERVER [IF NOT EXISTS] server_name [TYPE 'server_type'] [VERSION 'server_version']
    FOREIGN DATA WRAPPER fdw_name
    [OPTIONS([mpp_execute 'any | master | all segments' ], option 'value' [, ... ])]

Supported values of the option 'value' parameter

Option

Description

Default value

host

The IP address of the MySQL database that you want to access.

127.0.0.1

port

The port number of the MySQL database that you want to access.

3306

init_command

The SQL statement that is used to establish a connection with the MySQL database that you want to access.

Empty

secure_auth

Before V5.7.5, MySQL uses the password() function to convert a plaintext password into an encrypted password. The secure_auth option specifies whether the preceding encryption method is used on the foreign server.

true

use_remote_estimate

Specifies whether to execute the EXPLAIN statement to obtain table statistics from MySQL for generating an execution plan.

false

reconnect

Specifies whether to allow automatic reconnection.

false

character_set

The character set that is used for connections. The default value is auto, which specifies the character set of the OS in which the MySQL client is deployed.

auto

sql_mode

The SQL mode in MySQL, which is used to affect the SQL syntax and data validation checks. For more information, see

Server SQL Modes.

ANSI_QUOTES

ssl_key

The path name of the private key files on the MySQL client.

Empty

ssl_cert

The path name of the public key certificates on the MySQL client.

Empty

ssl_ca

The path name of the certificates issued by certificate authorities (CAs). If you specify this option, the foreign server uses the same certificates.

Empty

ssl_capath

The path name of a directory in which the trusted SSL CA certificates are stored.

Empty

ssl_cipher

The passwords that can be used for SSL encryption.

<none>

fetch_size

The number of rows that are obtained by each execution of the mysql_fdw extension. If you specify the fetch_size option for the foreign table, the value of the fetch_size option that you specify for the foreign server is overwritten.

100

CREATE USER MAPPING

Execute the CREATE USER MAPPING statement to create a user mapping.

CREATE USER MAPPING [ IF NOT EXISTS ] FOR { user_name | USER | CURRENT_USER | PUBLIC }
    SERVER server_name
    [ OPTIONS ( option 'value' [ , ... ] ) ]

Supported values of the option 'value' parameter

Option

Description

Default value

username

The username that is used to access the MySQL database.

Empty

password

The password that is used to access the MySQL database.

Empty

CREATE FOREIGN TABLE

Create a foreign table in the database management system.

CREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name ( [
  { column_name data_type [ OPTIONS ( option 'value' [, ... ] ) ] [ COLLATE collation ] [ column_constraint [ ... ] ]
    | table_constraint }
    [, ... ]
] )
[ INHERITS ( parent_table [, ... ] ) ]
  SERVER server_name
[ OPTIONS ( [ mpp_execute 'any | master | all segments' ], option 'value' [, ... ] ) ]

Supported values of the option 'value' parameter

Option

Description

Default value

dbname

The name of the foreign database that you created.

Empty

table_name

The name of the foreign table that you want to create.

A custom name AnalyticDB for PostgreSQL

fetch_size

The number of rows that are obtained by each execution of the mysql_fdw extension. If you specify the fetch_size option for the foreign table, the value of the fetch_size option that you specify for the foreign server is overwritten.

100

IMPORT FOREIGN SCHEMA

Import information about the MySQL table into AnalyticDB for PostgreSQL to save the information as a foreign table.

IMPORT FOREIGN SCHEMA remote_schema
    [ { LIMIT TO | EXCEPT } ( table_name [, ...] ) ]
    FROM SERVER server_name
    INTO local_schema
    [ OPTIONS ( option 'value' [, ... ] ) ]

Supported values of the option 'value' parameter

Option

Description

Default value

import_default

Specifies whether to import the default expression when you synchronize the foreign table schema.

FALSE

import_not_null

Specifies whether to import non-NULL expressions when you synchronize the foreign table schema.

TRUE

import_enum_as_text

Specifies whether to map MySQL data of the ENUM type to the TEXT type. If you set the import_enum_as_text option to FALSE, the system prompts you to create data of the TEXT type.

FALSE

Compatibility

The mysql_fdw extension of AnalyticDB for PostgreSQL is compatible with the following MySQL instances and databases:

ApsaraDB RDS for MySQL

Version number

Compatible

5.5

Yes

5.6

Yes

5.7

Yes

8.0

Yes

Self-managed MySQL database

MySQL versions earlier than V5.7 are no longer maintained. In most cases, MySQL V5.7 and later are used.

Version number

Compatible

5.7

Yes

8.0 (mainly used)

Yes

PolarDB for MySQL

Version number

Compatible

8.0.2

Yes

8.0.1

Yes

5.7

Yes

5.6

Yes

Features and data types

Features

Connection pool

All queries in the same session use the same MySQL database connection instead of a new MySQL connection.

WHERE pushdown

The WHERE clause of a foreign table is pushed down to MySQL. This way, the WHERE conditions related to the foreign table are executed in MySQL, and fewer rows of data are transmitted to the AnalyticDB for PostgreSQL instance.

Projection pushdown

Not all data of a MySQL table is queried and transmitted to the AnalyticDB for PostgreSQL instance. The mysql_fdw extension returns only the columns in the table that you want to query by executing the SELECT statement. This way, less data is transmitted to the AnalyticDB for PostgreSQL instance to improve performance.

Prepared statement

SELECT queries are performed by using prepared statements instead of simple SELECT statements.

JOIN pushdown

Join operations between two foreign tables in the same MySQL database are pushed down to a remote MySQL database for execution. This improves performance compared with the method of obtaining all rows of the two tables and performing join operations on the AnalyticDB for PostgreSQL instance.

Note
  • Only JOIN clauses that contain relational and arithmetic operators can be pushed down to prevent potential connection failure issues.

  • Only the INNER JOIN, LEFT OUTER JOIN, and RIGHT OUTER JOIN clauses are supported. The FULL OUTER JOIN, SEMI JOIN, or ANTI JOIN clauses are not supported.

Aggregate pushdown

Aggregate functions are pushed down to a remote MySQL database for execution. This improves performance compared with the method of obtaining all rows and performing aggregate operations on the AnalyticDB for PostgreSQL instance.

Note

Aggregate pushdown supports only the MIN(), MAX(), SUM(), AVG(), and COUNT() functions.

ORDER BY pushdown

ORDER BY clauses are pushed down to a remote MySQL database for execution to obtain an ordered result set from MySQL. MySQL databases perform NULL-related operations in the opposite manner compared with AnalyticDB for PostgreSQL. To obtain a result that is the same as the result in AnalyticDB for PostgreSQL, add the expression IS NULL predicate to the beginning of each ORDER BY clause. This way, MySQL places NULL values after a result set that is sorted in ascending order.

LIMIT and OFFSET pushdown

The LIMIT and OFFSET clauses are pushed down to a remote MySQL database for execution. This reduces the amount of data transmitted between the AnalyticDB for PostgreSQL instance and the MySQL database compared with the method of transmitting all data to the AnalyticDB for PostgreSQL instance.

Data types

No.

MySQL data type

AnalyticDB for PostgreSQL data type

1

BIT(N)

BIT(N)

2

TINYINT

SMALLINT

3

TINYINT UNSIGNED

SMALLINT

4

SMALLINT

SMALLINT

5

SMALLINT UNSIGNED

BIGINT

6

MEDIUMINT

INT

7

MEDIUMINT UNSIGNED

INT

8

INT

INT

9

INT UNSIGNED

BIGINT

10

BIGINT

BIGINT

11

BIGINT UNSIGNED

NUMERIC

12

DECIMAL(M,N)

NUMERIC

13

FLOAT

REAL

14

DOUBLE(M,N)

DOUBLE PRECISION

15

DATE

DATE

16

DATETIME

DATETIME without TIME zone

17

TIMESTAMP

DATETIME without TIME zone

18

TIME

TIME without TIME zone

19

YEAR[4]

Not supported

20

CHAR[N]

CHAR[N]

21

VARCHAR[N]

CHARACTER VARYING(N)

22

BINARY[N]

BYTEA

23

VARBINARY[N]

BYTEA

24

TINYBLOB

Not supported

25

TINYTEXT

TEXT

26

BLOB

bytea

27

TEXT

TEXT

28

MEDIUMBLOB

BYTEA

29

MEDIUMTEXT

TEXT

30

LONGBLOB

BYTEA

31

LONGTEXT

TEXT

32

ENUM(",")

Prompted to manually create data of the ENUM type

33

SET('','')

Not supported

34

GEOMETRY

Not supported

35

POINT

POINT

36

LINESTRING

Not supported

37

POLYGON

POLYGON

38

MULTIPOINT

Not supported

39

MULTILINESTRING

Not supported

40

MULTIPOLYGON

Not supported

41

GEOMETRCOLLECTION

Not supported

42

JSON

JSON