|
| 1 | +:sectnums: |
| 2 | +:sectnumlevels: 5 |
| 3 | + |
| 4 | += mysql_fdw |
| 5 | + |
| 6 | +== Overview |
| 7 | + |
| 8 | +mysql_fdw is a PostgreSQL foreign data wrapper for MySQL. It allows IvorySQL to query and modify MySQL tables through SQL, supports `IMPORT FOREIGN SCHEMA`, and can push filters, projections, joins, aggregates, sorting, and limits to the remote server. |
| 9 | + |
| 10 | +Project page: <https://github.com/EnterpriseDB/mysql_fdw> |
| 11 | + |
| 12 | +Tested version: 2.9.3 |
| 13 | + |
| 14 | +License: PostgreSQL License |
| 15 | + |
| 16 | +== Compatibility |
| 17 | + |
| 18 | +The following combination was validated on x86_64 Linux: |
| 19 | + |
| 20 | +[cols="1,1"] |
| 21 | +|=== |
| 22 | +| Component | Version |
| 23 | +| IvorySQL | current `IVORY_REL_5_STABLE` (IvorySQL 5.6 / PostgreSQL 18.6) |
| 24 | +| mysql_fdw | 2.9.3 |
| 25 | +| MySQL Server | 8.4 |
| 26 | +| MariaDB Connector/C | 3.1 |
| 27 | +|=== |
| 28 | + |
| 29 | +[IMPORTANT] |
| 30 | +mysql_fdw 2.9.3 requires the PostgreSQL-compatible extension APIs restored by IvorySQL PR #1448. The released IvorySQL 5.4 image predates that fix and fails to compile this version with an `ExecTypeFromTL` argument-count error. Use a current `IVORY_REL_5_STABLE` build containing that change or a newer IvorySQL release. |
| 31 | + |
| 32 | +== Installation |
| 33 | + |
| 34 | +Install a C compiler, GNU make, and the MySQL or MariaDB client development package. Then build against the intended IvorySQL installation explicitly: |
| 35 | + |
| 36 | +[source,bash] |
| 37 | +---- |
| 38 | +git clone https://github.com/EnterpriseDB/mysql_fdw.git |
| 39 | +cd mysql_fdw |
| 40 | +git checkout REL-2_9_3 |
| 41 | +
|
| 42 | +export PG_CONFIG=/usr/local/ivorysql/bin/pg_config |
| 43 | +make USE_PGXS=1 PG_CONFIG="$PG_CONFIG" |
| 44 | +sudo make USE_PGXS=1 PG_CONFIG="$PG_CONFIG" install |
| 45 | +---- |
| 46 | + |
| 47 | +Confirm that `pg_config --version` points to IvorySQL before building. No server preload or restart is required. |
| 48 | + |
| 49 | +== MySQL Preparation |
| 50 | + |
| 51 | +[source,sql] |
| 52 | +---- |
| 53 | +CREATE DATABASE fdw_test CHARACTER SET utf8mb4; |
| 54 | +CREATE USER 'fdw_user'@'%' IDENTIFIED BY 'fdw_pass'; |
| 55 | +GRANT ALL PRIVILEGES ON fdw_test.* TO 'fdw_user'@'%'; |
| 56 | +
|
| 57 | +CREATE TABLE fdw_test.products ( |
| 58 | + id integer PRIMARY KEY, |
| 59 | + name varchar(100), |
| 60 | + price decimal(10,2), |
| 61 | + note varchar(100) |
| 62 | +); |
| 63 | +INSERT INTO fdw_test.products VALUES |
| 64 | + (1, 'IvorySQL', 99.50, '中文'), |
| 65 | + (2, 'MySQL', 49.00, NULL); |
| 66 | +---- |
| 67 | + |
| 68 | +== IvorySQL Configuration and Use |
| 69 | + |
| 70 | +[source,sql] |
| 71 | +---- |
| 72 | +CREATE EXTENSION mysql_fdw; |
| 73 | +
|
| 74 | +CREATE SERVER mysql_server |
| 75 | + FOREIGN DATA WRAPPER mysql_fdw |
| 76 | + OPTIONS (host '127.0.0.1', port '3306', character_set 'utf8mb4'); |
| 77 | +
|
| 78 | +CREATE USER MAPPING FOR CURRENT_USER |
| 79 | + SERVER mysql_server |
| 80 | + OPTIONS (username 'fdw_user', password 'fdw_pass'); |
| 81 | +
|
| 82 | +CREATE SCHEMA mysql_remote; |
| 83 | +IMPORT FOREIGN SCHEMA fdw_test |
| 84 | + FROM SERVER mysql_server INTO mysql_remote; |
| 85 | +
|
| 86 | +SELECT * FROM mysql_remote.products ORDER BY id; |
| 87 | +INSERT INTO mysql_remote.products VALUES (3, 'FDW', 10.00, 'insert'); |
| 88 | +UPDATE mysql_remote.products SET price = 11.00 WHERE id = 3; |
| 89 | +DELETE FROM mysql_remote.products WHERE id = 3; |
| 90 | +---- |
| 91 | + |
| 92 | +Set `character_set 'utf8mb4'` when the remote database stores UTF-8 text; otherwise non-ASCII data can be decoded incorrectly. |
| 93 | + |
| 94 | +Use `EXPLAIN VERBOSE` to inspect pushdown. The `Remote query` field should contain operations that mysql_fdw can execute remotely: |
| 95 | + |
| 96 | +[source,sql] |
| 97 | +---- |
| 98 | +EXPLAIN (VERBOSE, COSTS OFF) |
| 99 | +SELECT name, price |
| 100 | +FROM mysql_remote.products |
| 101 | +WHERE price > 40 |
| 102 | +ORDER BY price DESC |
| 103 | +LIMIT 1; |
| 104 | +---- |
| 105 | + |
| 106 | +== Dual-mode Validation |
| 107 | + |
| 108 | +The same foreign tables can be used in both IvorySQL modes: |
| 109 | + |
| 110 | +[source,sql] |
| 111 | +---- |
| 112 | +SET ivorysql.compatible_mode = pg; |
| 113 | +SELECT * FROM mysql_remote.products ORDER BY id; |
| 114 | +
|
| 115 | +SET ivorysql.compatible_mode = oracle; |
| 116 | +SELECT 1 FROM dual; |
| 117 | +SELECT * FROM mysql_remote.products ORDER BY id; |
| 118 | +INSERT INTO mysql_remote.products VALUES (4, 'Oracle mode', 20.00, NULL); |
| 119 | +UPDATE mysql_remote.products SET price = 21.00 WHERE id = 4; |
| 120 | +DELETE FROM mysql_remote.products WHERE id = 4; |
| 121 | +---- |
| 122 | + |
| 123 | +Validation covered extension creation, schema import, UTF-8 and NULL values, CRUD, and filter/projection/order/limit pushdown in both modes. All nine upstream mysql_fdw regression groups also passed against a MySQL 8.4 server: `server_options`, `connection_validation`, `dml`, `select`, `pushdown`, `join_pushdown`, `aggregate_pushdown`, `limit_offset_pushdown`, and `misc`. |
| 124 | + |
| 125 | +== Troubleshooting |
| 126 | + |
| 127 | +* `too few arguments to function 'ExecTypeFromTL'`: the IvorySQL installation is older than the extension-API compatibility fix described above. |
| 128 | +* `Can't connect to MySQL server`: verify the host, port, firewall, and that the MySQL account accepts connections from the IvorySQL host. |
| 129 | +* Garbled text: set the server option `character_set 'utf8mb4'` and verify the remote database character set. |
| 130 | +* Keep credentials out of shared scripts and restrict access to user mappings in production. |
0 commit comments