Load Data from MySQL Server Using MySQL_FDW
MySQL FDW is a Foreign Data Wrapper (FDW). You can use it to connect SynxDB to a MySQL database. MySQL FDW is developed based on Linux and can run on any POSIX-compliant system.
Prerequisites
Before enabling MySQL FDW, ensure the following steps have been completed:
MySQL Server has been correctly installed and is accessible.
Install the MySQL Client on the Coordinator node of SynxDB. You can download and install client from the MySQL official website.
Note
Ensure that
libmysqlclient.so
is installed. You can usefind / -name libmysqlclient.so
to check its path. If no path is returned, you can install the MySQL Client development package using the following command:sudo yum install mysql-community-devel
On the Coordinator node, as the
gpadmin
user, add the directory path oflibmysqlclient.so
to theLD_LIBRARY_PATH
environment variable:export LD_LIBRARY_PATH=<path_to_directory_of_libmysqlclient.so>:$LD_LIBRARY_PATH
. You can find the path using the command from step 2.
Use MySQL FDW
CREATE SERVER options
MySQL FDW accepts the following options via the CREATE SERVER command:
Option |
Description |
---|---|
|
String, optional, default value |
|
Integer, optional, default value |
|
Boolean, optional, default value |
|
String, optional, no default value. SQL command to execute upon connection to the MySQL server. |
|
Boolean, optional, default value |
|
Boolean, optional, default value |
|
String, optional, default value |
|
String, optional, no default value. Path to the client private key file. |
|
String, optional, no default value. Path to the client public key certificate file. |
|
String, optional, no default value. Path to the Certificate Authority (CA) certificate file. If this option is used, the same certificate as the server must be specified. |
|
String, optional, no default value. Directory path containing trusted SSL CA certificate files. |
|
String, optional, no default value. Allowed cipher list for SSL encryption. |
|
Integer, optional, default value |
|
String, optional, default value |
|
String, optional, no default value. Specifies the path to the MySQL default file if connection details (like username, password) are to be retrieved from it. |
|
Boolean, optional, default value |
CREATE USER MAPPING options
MySQL FDW accepts the following options via the CREATE USER MAPPING command:
Option |
Description |
---|---|
|
String, no default value. Username used to connect to MySQL. |
|
String, no default value. Password used for authenticating the MySQL server. |
CREATE FOREIGN TABLE options
MySQL FDW accepts the following table-level options via the CREATE FOREIGN TABLE command:
Option |
Description |
---|---|
|
String, required. The name of the MySQL database to query. |
|
String, optional, default is the name of the foreign table. Name of the MySQL table. |
|
Integer, optional. Same as the fetch_size parameter for the foreign server. |
|
Integer, optional. The maximum blob size to read without truncation. |
|
Boolean, optional, default value |
IMPORT FOREIGN SCHEMA options
MySQL FDW supports the IMPORT FOREIGN SCHEMA command and accepts the following custom options:
Option |
Description |
---|---|
|
Boolean type, optional, default value is |
|
Boolean type, optional, default value is |
|
Boolean type, optional, default value is |
|
Boolean type, optional, default value is |
TRUNCATE support
MySQL FDW implements the TRUNCATE
API, which is available for PostgreSQL kernel versions 14 and above. MySQL provides a TRUNCATE
command; see the TRUNCATE TABLE Statement.
Note the following usage limitations:
TRUNCATE ... CASCADE
is not supported.TRUNCATE ... RESTART IDENTITY
is not supported and is ignored.TRUNCATE ... CONTINUE IDENTITY
is not supported and is ignored.MySQL tables with foreign key references cannot be truncated.
Functions
In addition to the standard mysql_fdw_handler()
and mysql_fdw_validator()
functions, MySQL FDW also provides the following utility functions for user invocation:
mysql_fdw_version()
: Returns the version number as an integer.mysql_fdw_display_pushdown_list()
: Displays the contents of themysql_fdw_pushdown.config
file.
Note
While MySQL FDW may insert or update values for generated columns in MySQL, it cannot prevent these values from being modified in MySQL. Therefore, there is no guarantee that a subsequent SELECT operation will retrieve the expected generated value for the column.
Usage Examples
Installing the Extension
In a SynxDB database, enable the MySQL FDW extension as a superuser gpadmin
:
CREATE EXTENSION mysql_fdw;
Creating a Foreign Server
Create a foreign server object and specify the connection parameters for the MySQL database:
CREATE SERVER <mysql_server>
FOREIGN DATA WRAPPER mysql_fdw
OPTIONS (host '<localhost>', port '<3306>');
Note
If MySQL Server and SynxDB Coordinator are installed on the same node, the value for host
must be localhost
.
Granting Permissions to Use Foreign Server
Grant permission for a regular user to use the foreign server:
GRANT USAGE ON FOREIGN SERVER <mysql_server> TO <gpadmin>;
Creating a User Mapping
Create a user mapping for the MySQL server, specifying the username and password. The following example code creates a MySQL user mapping for the current user:
CREATE USER MAPPING for CURRENT_USER
SERVER <mysql_server>
OPTIONS (username '<mysql_username>', password '<mysql_pwd>');
Creating a Foreign Table
Create a foreign table, ensuring that the user has been granted permission to use the foreign server.
If the MySQL table name differs from the foreign table name, use the table_name
option to specify the MySQL table name.
Note
The corresponding MySQL table must exist for the foreign table. To perform insert, delete, or update operations, the first column of the MySQL table must be unique. You can either set it as the primary key or add a UNIQUE
constraint.
The following example demonstrates how to create a foreign table. Replace the values in the command before running it:
CREATE FOREIGN TABLE external_warehouse (
warehouse_id int,
warehouse_name text,
warehouse_created timestamp
)
SERVER mysql_server
OPTIONS (dbname 'mysql_db', table_name 'mysql_warehouse');
You can perform insert, select, delete, and update operations in the same way as on a local table.
-- Insert new rows into the table
INSERT INTO external_warehouse values (1, 'UPS', current_date);
INSERT INTO external_warehouse values (2, 'TV', current_date);
INSERT INTO external_warehouse values (3, 'Table', current_date);
-- Query data from the table
SELECT * FROM external_warehouse ORDER BY 1;
warehouse_id | warehouse_name | warehouse_created
-------------+----------------+-------------------
1 | UPS | 10-JUL-20 00:00:00
2 | TV | 10-JUL-20 00:00:00
3 | Table | 10-JUL-20 00:00:00
-- Delete rows from the table
DELETE FROM external_warehouse where warehouse_id = 3;
-- Update rows in the table
UPDATE warehouse set warehouse_name = 'UPS_NEW' where warehouse_id = 1;
-- Use the VERBOSE option to EXPLAIN the table
EXPLAIN VERBOSE SELECT warehouse_id, warehouse_name FROM external_warehouse WHERE warehouse_name LIKE 'TV' limit 1;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------
Limit (cost=10.00..11.00 rows=1 width=36)
Output: warehouse_id, warehouse_name
-> Foreign Scan on public.warehouse (cost=10.00..1010.00 rows=1000 width=36)
Output: warehouse_id, warehouse_name
Local server startup cost: 10
Remote query: SELECT `warehouse_id`, `warehouse_name` FROM `db`.`warehouse` WHERE ((`warehouse_name` LIKE BINARY 'TV'))
Importing a MySQL Database as a SynxDB Schema
Use the IMPORT FOREIGN SCHEMA
command to import a schema from a MySQL database into SynxDB:
IMPORT FOREIGN SCHEMA someschema
FROM SERVER <mysql_server>
INTO public;