Postgresql安装mysql_fdw并映射MySQL数据库

发布于:2025-04-14 ⋅ 阅读:(20) ⋅ 点赞:(0)

关于Postgresql映射Mysql数据库数据

领导:小汪啊,他们的数据库是不是能连接上了。
我:对啊,我已经读数据了。
领导:那改一下吧,直接把他们的数据映射过来,体现一下我们功能的多样性。
我:好啊
配置mysql_fdw三小时后……
我:(╬◣д◢)

前言

在Postgresql数据库上有一个简单方法获取MySQL数据库数据,安装mysql_fdw,把MySQL数据表直接映射到Postgresql数据库中。由于我的Postgresql数据库安装在Ubuntu系统上,接下来主要以Ubuntu系统为例。

Postgres数据库配置

安装Postgresql数据库

  1. 检查Postgresql安装

在后续的装配中,postgresql-server-dev-version这个包十分重要,如果有mysql_fdw装配报错,十有八九就是这个包的原因了。
version对应你Postgresql数据库版本

sudo apt update
sudo apt install postgresql-server-dev-version libmysqlclient-dev

完事后记得查看Postgresql数据库版本,查看安装情况(这个不一定准,有的就算可以查看版本号依旧会装配错误)

psql --version
  1. 下载mysql_fdw
    记得mysql_fdw版本要和Postgresql数据库版本一致
    在线下载
git clone https://github.com/EnterpriseDB/mysql_fdw.git

离线下载,下载后上传到服务器解压就好

https://github.com/EnterpriseDB/mysql_fdw/archive/refs/heads/master.zip
  1. mysql_fdw装配
    mysql_fdw采用的是代码装配,也是从这开始问题就多起来了
cd mysql_fdw  //首先进入mysql_fdw目录下
make USE_PGXS=1 //mysql_fdw预编译
make USE_PGXS=1 install //mysql_fdw装配

装配完成后,在/usr/lib/postgresql/version/lib/目录下会出现mysql_fdw.so文件,代表你的mysql_fdw已经装配完成(version代表你的postgresql数据库版本)。
在这里插入图片描述

  1. 创建mysql_server服务
    登录postgresql数据库
su postgres    //切换postgresql数据库登录用户
psql     //进入postgresql数据库

创建mysql_server服务
mysql_host指的是Mysql数据库所在服务器IP
mysql_port指的是Mysql数据库开放的端口

CREATE SERVER mysql_server
FOREIGN DATA WRAPPER mysql_fdw
OPTIONS (
  host 'mysql_host',
  port 'mysql_port'
);
  1. 创建mysql_server映射
    mysql_user指的是Mysql数据库访问用户账号
    mysql_password指的是Mysql数据库访问密码
CREATE USER MAPPING FOR postgres
SERVER mysql_server
OPTIONS (
  username 'mysql_user',
  password 'mysql_password'
);
  1. 创建mysql_server表映射为外部表
    psql_table指的是在postgresql数据创建的外部表名称
    mysql_table指的是Mysql数据库需要映射过来的表名称
    mysql_dbname指的是Mysql数据库映射表所在的库名称
CREATE FOREIGN TABLE psql_table (
  id SERIAL PRIMARY KEY,
  username VARCHAR(50) NOT NULL,
  email VARCHAR(100) UNIQUE,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  is_active BOOLEAN DEFAULT true
)
SERVER mysql_server
OPTIONS (
table_name 'mysql_table',dbname 'mysql_dbname'
);
  1. 删除外部表映射
    psql_table指的是在postgresql数据需要删除的外部表名称
    因为映射的外部表字段无法直接修改(我试了一下没成功)
    所以更新外部表就需要删除错表,重新创建映射的外部表
DROP FOREIGN TABLE psql_table;
  1. 验证外部表映射
\dE+    //查看外部表
SELECT * FROM psql_table  //查看外部表映射数据 

装配错误原因

Postgresql安装问题

The following packages have unmet dependencies:



 postgresql : Depends: postgresql-10 but it is not going to be installed

 postgresql-contrib : Depends: postgresql-contrib-10

 postgresql-server-dev-all : Depends: postgresql-common (>= 117~) but it is not going to be installed

                             Depends: postgresql-server-dev-10 but it is not going to be installed

E: Unable to correct problems, you have held broken packages.
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -g -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fno-omit-frame-pointer -fPIC  -D _MYSQL_LIBNAME=\"libmysqlclient.so\" -I. -I./ -I/usr/include/postgresql/13/server -I/usr/include/postgresql/internal  -Wdate-time -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -I/usr/include/libxml2   -c -o connection.o connection.c

connection.c:15:10: fatal error: postgres.h: No such file or directory

 #include "postgres.h"

          ^~~~~~~~~~~~

compilation terminated.

<builtin>: recipe for target 'connection.o' failed

make: *** [connection.o] Error 1
make: mysql_config: Command not found

make: mysql_config: Command not found

gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -g -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fno-omit-frame-pointer -fPIC  -D _MYSQL_LIBNAME=\"libmysqlclient.so\" -I. -I./ -I/usr/include/postgresql/13/server -I/usr/include/postgresql/internal  -Wdate-time -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -I/usr/include/libxml2   -c -o connection.o connection.c

connection.c:15:10: fatal error: postgres.h: No such file or directory

 #include "postgres.h"

          ^~~~~~~~~~~~

compilation terminated.

<builtin>: recipe for target 'connection.o' failed

make: *** [connection.o] Error 1

解决方法:找个虚拟机重新安装一个同版本的Postgresql数据库,然后在那边查看一下apt install postgresql-server-dev-version时装配的文件,找到对应安装需要的deb文件,下载过来安装,下图是我postgresql-server-dev-13的安装包。
version对应的是你Postgresql数据库版本

apt install --download-only postgresql-server-dev-version

在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到