1.安装mysql和pg数据略过
2.安装mysql_fdw
[root@slt032qws38 dbadmin]# unzip mysql_fdw-master.zip
[root@slt032qws38 dbadmin]# cd mysql_fdw-master/
[root@slt032qws38 mysql_fdw-master]# export PATH=/data/postgres/bin/:$PATH
[root@slt032qws38 mysql_fdw-master]# export PATH=/usr/local/mysql/bin/:$PATH
[root@slt032qws38 mysql_fdw-master]# make USE_PGXS=1
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -I/usr/local/mysql/include -D _MYSQL_LIBNAME=\"libmysqlclient.so\" -I. -I./ -I/data/postgres/include/server -I/data/postgres/include/internal -D_GNU_SOURCE -c -o connection.o connection.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -I/usr/local/mysql/include -D _MYSQL_LIBNAME=\"libmysqlclient.so\" -I. -I./ -I/data/postgres/include/server -I/data/postgres/include/internal -D_GNU_SOURCE -c -o option.o option.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -I/usr/local/mysql/include -D _MYSQL_LIBNAME=\"libmysqlclient.so\" -I. -I./ -I/data/postgres/include/server -I/data/postgres/include/internal -D_GNU_SOURCE -c -o deparse.o deparse.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -I/usr/local/mysql/include -D _MYSQL_LIBNAME=\"libmysqlclient.so\" -I. -I./ -I/data/postgres/include/server -I/data/postgres/include/internal -D_GNU_SOURCE -c -o mysql_query.o mysql_query.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -I/usr/local/mysql/include -D _MYSQL_LIBNAME=\"libmysqlclient.so\" -I. -I./ -I/data/postgres/include/server -I/data/postgres/include/internal -D_GNU_SOURCE -c -o mysql_fdw.o mysql_fdw.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -shared -o mysql_fdw.so connection.o option.o deparse.o mysql_query.o mysql_fdw.o -L/data/postgres/lib -Wl,--as-needed -Wl,-rpath,'/data/postgres/lib',--enable-new-dtags
[root@slt032qws38 mysql_fdw-master]# make USE_PGXS=1 install
/bin/mkdir -p '/data/postgres/lib'
/bin/mkdir -p '/data/postgres/share/extension'
/bin/mkdir -p '/data/postgres/share/extension'
/bin/install -c -m 755 mysql_fdw.so '/data/postgres/lib/mysql_fdw.so'
/bin/install -c -m 644 .//mysql_fdw.control '/data/postgres/share/extension/'
/bin/install -c -m 644 .//mysql_fdw--1.0.sql .//mysql_fdw--1.1.sql .//mysql_fdw--1.0--1.1.sql '/data/postgres/share/extension/'
3.创建插件
mypg=# create extension mysql_fdw ;
ERROR: failed to load the mysql query:
libmysqlclient.so: cannot open shared object file: No such file or directory
HINT: Export LD_LIBRARY_PATH to locate the library.
如果出现以上错误,解决方式
找到libmysqlclient.so所在位置,在/etc/ld.so.conf中增加路径
[root@slt032qws38 ~]# vi /etc/ld.so.conf
添加一行
/usr/local/mysql/lib
运行 ldconfig 命令,让其生效。
[root@slt032qws38 ~]# ldconfig
4.使用插件
pg端:
--创建mysql_fdw插件
CREATE EXTENSION mysql_fdw;
--创建服务项目
CREATE SERVER mysql_server
FOREIGN DATA WRAPPER mysql_fdw
OPTIONS (host '127.0.0.1', port '3306');
--创建mapping
CREATE USER MAPPING FOR postgres
SERVER mysql_server
OPTIONS (username 'root', password 'Mysql@123');
--创建外部表
CREATE FOREIGN TABLE warehouse
(
warehouse_id int,
warehouse_name text,
warehouse_created timestamp
)
SERVER mysql_server
OPTIONS (dbname 'mypg', table_name 'warehouse');
mysql端
--创建数据库
mysql> create database mypg;
Query OK, 1 row affected (0.00 sec)
mysql> use mypg;
Database changed
--创建表
mysql> create table warehouse(warehouse_id int,warehouse_name text,warehouse_created timestamp);
Query OK, 0 rows affected (0.01 sec)
--插入数据
mysql> INSERT INTO warehouse values (1, 'UPS', current_date);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO warehouse values (2, 'TV', current_date);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO warehouse values (3, 'Table', current_date);
Query OK, 1 row affected (0.00 sec)
--查看数据
mysql> select * from warehouse;
+--------------+----------------+---------------------+
| warehouse_id | warehouse_name | warehouse_created |
+--------------+----------------+---------------------+
| 1 | UPS | 2021-10-19 00:00:00 |
| 2 | TV | 2021-10-19 00:00:00 |
| 3 | Table | 2021-10-19 00:00:00 |
+--------------+----------------+---------------------+
3 rows in set (0.00 sec)
pg端查看数据
mypg=# select * from warehouse ;
warehouse_id | warehouse_name | warehouse_created
--------------+----------------+---------------------
1 | UPS | 2021-10-19 00:00:00
2 | TV | 2021-10-19 00:00:00
3 | Table | 2021-10-19 00:00:00
(3 rows)
mysql端修改数据
mysql> UPDATE warehouse set warehouse_name = 'UPS_NEW' where warehouse_id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from warehouse;
+--------------+----------------+---------------------+
| warehouse_id | warehouse_name | warehouse_created |
+--------------+----------------+---------------------+
| 1 | UPS_NEW | 2021-10-19 15:05:08 |
| 2 | TV | 2021-10-19 00:00:00 |
| 3 | Table | 2021-10-19 00:00:00 |
+--------------+----------------+---------------------+
3 rows in set (0.00 sec)
pg端查看
mypg=# select * from warehouse ;
warehouse_id | warehouse_name | warehouse_created
--------------+----------------+---------------------
1 | UPS_NEW | 2021-10-19 15:05:08
2 | TV | 2021-10-19 00:00:00
3 | Table | 2021-10-19 00:00:00
(3 rows)
mysql端删除数据
mysql> DELETE FROM warehouse where warehouse_id = 3;
Query OK, 1 row affected (0.00 sec)
mysql> select * from warehouse;
+--------------+----------------+---------------------+
| warehouse_id | warehouse_name | warehouse_created |
+--------------+----------------+---------------------+
| 1 | UPS_NEW | 2021-10-19 15:05:08 |
| 2 | TV | 2021-10-19 00:00:00 |
+--------------+----------------+---------------------+
2 rows in set (0.00 sec)
pg端查看
mypg=# select * from warehouse ;
warehouse_id | warehouse_name | warehouse_created
--------------+----------------+---------------------
1 | UPS_NEW | 2021-10-19 15:05:08
2 | TV | 2021-10-19 00:00:00
(2 rows)