1.mysql_fdw源码克隆
链接:https://github.com/EnterpriseDB/mysql_fdw/releases
选择最新版REL-2_5_1
2.mysql_fdw编译安装
下载来的源码不自带window工程文件,自己新建工程,将源码加入
工程依赖PostgreSQL和mysql, C++编译附加包含目录需要设置相应目录
文件路径自己修改:以我的为例使用的是PostgreSQL9.5.4和mysql5.7.2
C:\Program Files\PostgreSQL\9.5\include
C:\Program Files\PostgreSQL\9.5\include\server
C:\Program Files\PostgreSQL\9.5\include\server\port\win32
C:\Program Files\PostgreSQL\9.5\include\server\port\win32_msvc
D:\MySQL\MySQL Server 5.7\include
链接目录
C:\Program Files\MySQL\MySQL Server 5.7\lib\libmysql.lib
C:\Program Files\PostgreSQL\9.5\lib\postgres.lib
MYSQL_FDW.def文件(通过模块定义文件导入)
设置需要导出的函数
Pg_magic_func
_PG_init
is_foreign_expr
mysql_append_where_clause
mysql_bind_result
mysql_bind_sql_var
mysql_cleanup_connection
mysql_connect
mysql_convert_to_pg
mysql_deparse_analyze
mysql_deparse_delete
mysql_deparse_insert
mysql_deparse_select
mysql_deparse_update
mysql_dll_handle DATA
mysql_fdw_handler
mysql_fdw_validator
mysql_fdw_version
mysql_get_connection
mysql_get_options
mysql_is_valid_option
mysql_load_library
mysql_rel_connection
pg_finfo_mysql_fdw_handler
pg_finfo_mysql_fdw_validator
pg_finfo_mysql_fdw_version
3.mysql_fdw库导入PG
1)将mysql_fdw.control、mysql_fdw--1.0.sql、mysql_fdw--1.0--1.1.sqlmysql_fdw--1.1.sql复制到C:\Program Files\PostgreSQL\9.5\share\extension目录下
2)将生成的mysql_fdw.dll复制到C:\Program Files\PostgreSQL\9.5\lib目录下
注意:由于项目在编译时将libmysql.dll引用路径定死了(C:\Program Files\MySQL\MySQL Server 5.7\lib),当生成的mysql_fdw.dll移植到其他机器时,libmysql.dll路径不一致或者不存在则出现以下错误提示:
解决办法:采用方法2
(1)人为添加C:\Program Files\MySQL\MySQL Server 5.7\lib目录和libmysql.dll文件;
(2)将libmysql.dll在编译解决添加到C:\Program Files\PostgreSQL\9.5\lib目录下并包含--由于数据库的安装目录是确定的;
说明:以上两种方法都依赖libmysql.dll文件的路径,都不是最佳解决方案。
4.mysql_fdw测试
1.mysql
--mysql(账号:root; 密码:123456)
--进入Mysql设置基本测试表
mysql -uroot -p123456
mysql> show databases;
mysql> create database mydb;
mysql> show databases;
mysql> use mydb;
mysql> show tables;
mysql> CREATE TABLE example ( id INT,data VARCHAR(100) );
mysql> INSERT INTO example VALUES (1,'aaa');
mysql> INSERT INTO example VALUES (2,'bbb');
mysql> INSERT INTO example VALUES (3,'ccc');
mysql> INSERT INTO example VALUES (4,'ddd');
mysql> INSERT INTO example VALUES (5,'eee');
mysql> show tables;
mysql> select * from example;
2.postgresql
postgres=# CREATE EXTENSION mysql_fdw;
CREATE EXTENSION
postgres=# CREATE SERVER mysql_server FOREIGN DATA WRAPPER mysql_fdw OPTIONS (host '127.0.0.1', port '3306');
CREATE SERVER
postgres=# CREATE USER MAPPING FOR PUBLIC SERVER mysql_server OPTIONS (username 'root', password '123456');
CREATE USER MAPPING
postgres=# CREATE FOREIGN TABLE example (id INT,data VARCHAR(100) ) SERVER mysql_server OPTIONS (dbname 'mydb', table_name 'example');
CREATE FOREIGN TABLE
postgres=# SELECT * FROM example;
id | data
----+------
1 | aaa
2 | bbb
3 | ccc
4 | ddd
5 | eee
(5 rows)
postgres=#