利用pgloader工具将MySQL数据迁移至PostgreSQL数据库

news2024/10/5 14:12:01

一、简介

pgloader是一款开源软件,可以将各种来源的数据加载到PostgreSQL数据库中,支持动态读取数据,使用 COPY 流式传输协议将数据加载到 PostgreSQL 数据库中,并使用单独的线程读取和写入数据,由于能够直接从源数据库加载数据。今天我们就借助pgloader这款工具实现将MySQL数据迁移到PostgreSQL数据库。

二、版本说明

MySQL版本为8.0.31、PostgreSQL版本为13.5。

三、安装PostgreSQL数据库

1.创建pg用户及安装目录

useradd postgres  
mkdir -p /home/postgresql

2.安装pg数据库依赖包

yum install -y perl-ExtUtils-Embed readline-devel python3 python3-devel gcc-c++ cmake libarchive openssl-devel

3.pg安装部署

wget https://ftp.postgresql.org/pub/source/v13.5/postgresql-13.5.tar.gz
tar -zxvf postgresql-13.5.tar.gz  && cd postgresql-13.5/
./configure --prefix=/home/postgresql --with-python --with-perl --with-openssl
make &&make install
/home/postgresql/bin/pg_ctl --version   ##查看安装版本
pg_ctl (PostgreSQL) 13.5

4.设置环境变量

mkdir -p /app/postgresql/pgdata ##创建数据库存储目录
cat >> /etc/profile << EOF
## postgres ##
export PATH=/app/postgresql/bin:$PATH
export LD_LIBRARY_PATH=/app/postgresql/lib
export PGDATA=/app/postgresql/pgdata
EOF

source /etc/profile  #加载环境变量
pg_ctl --version
pg_ctl (PostgreSQL) 13.5

5.授权

chown -R postgres.postgres /home/postgresql
su - postgres
initdb  ##初始化

6.创建数据库

pg_ctl start -D $PGDATA  ##启动数据库
createuser -P zabbix  ##输出两次密码
Enter password for new role:
Enter it again:
createdb -O zabbix -E Unicode -T template0 zabbix  ##创建数据库

四、安装pgloader工具

1.安装部署

wget https://codeload.github.com/dimitri/pgloader/tar.gz/refs/tags/v3.6.9
tar -zxvf v3.6.9
dnf -y install freetds-devel  ##安装依赖包
cd pgloader-3.6.9/
chmod 755 bootstrap-centos.sh
./bootstrap-centos.sh
make pgloader
cp build/bin/pgloader /usr/local/bin/
pgloader --version
pgloader version "3.6.7~devel"
compiled with SBCL 2.2.10-1.rhel8

2.修改Mysql身份认证

echo "default-authentication-plugin=mysql_native_password" >> /etc/my.conf ##pgloader不支持caching_sha2_password身份验证插件,而这个是 MySQL 8 的默认设置,需要修改配置,如MySQL8.0前版本无需操作
systemctl restart mysqld  ##重启数据库

3.导入Zabbix表结构

vi database/postgresql/schema.sql  ##从INSERT INTO dbversion开始往下全部删除,参考命令  :.,$d
CREATE INDEX sla_service_tag_1 ON sla_service_tag (slaid);
CREATE TABLE dbversion (
        dbversionid              bigint                                    NOT NULL,
        mandatory                integer         DEFAULT '0'               NOT NULL,
        optional                 integer         DEFAULT '0'               NOT NULL,
        PRIMARY KEY (dbversionid)
);
INSERT INTO dbversion VALUES ('1','6000000','6000017');
create or replace function hosts_name_upper_upper()
returns trigger language plpgsql as $func$
begin
update hosts set name_upper=upper(name)
where hostid=new.hostid;
psql -Uzabbix -dzabbix -f database/postgresql/schema.sql

4.迁移Mysql配置信息

参考文献:https://pgloader.readthedocs.io/en/latest/ref/mysql.html

mkdir -p /root/migration && cd /root/migration
vi config.pgloader  ##当复制下面配置的时候请去除所有的注释
LOAD DATABASE
FROM mysql://zabbix:*****@127.0.0.1:3306/zabbix
INTO postgresql://zabbix:*****@127.0.0.1:5432/zabbix
WITH include no drop,
#当列出此选项时,pgloader在加载数据时将不包含任何DROP语句。
truncate,
#当列出这个选项时,pgloader在将数据加载到每个PostgreSQL表之前,对每个PostgreSQL表发出TRUNCATE命令。删除表中的所有行,但表结构及其列、约束、索引等保持不变。新行标识所用的计数值重置为该列的种子
create no tables,
#当列出此选项时,pgloader在加载数据之前跳过表的创建,目标表必须已存在。
#此外,当使用不创建表时,pgloader从当前目标数据库获取元数据并检查类型转换,然后在加载数据之前删除约束和索引,并在加载完成后重新安装它们。
create no indexes,
#当列出此选项时,pgloader将跳过创建索引。
no foreign keys,
#当列出此选项时,pgloader将跳过创建外键。
reset sequences,
#当列出这个选项时,在数据加载结束时,在所有索引都创建完成之后,pgloader将创建的所有PostgreSQL序列重置为它们所附列的当前最大值。
data only
#当列出此选项时,pgloader只发出COPY语句,而不进行任何处理。
SET maintenance_work_mem TO '1024MB', work_mem to '512MB'
#设置maintenance_work_mem和work_mem,根据自己机器的配置来设置,越大迁移越快
EXCLUDING TABLE NAMES MATCHING ~/history.*/, ~/trend.*/
#不迁移history表和trends表
ALTER SCHEMA 'zabbix' RENAME TO 'public';
#将pgloader转换生成的zabbix模式更名为public

pgloader config.pgloader ##开始迁移所有的配置不包含历史数据
Total import time          ✓     126602    12.5 MB          3.820s  ##新库耗时时间较短

5.查看迁移数据

#  psql -Uzabbix -dzabbix -h127.0.0.1
> \d items
      Column      |          Type           | Collation | Nullable |          Default          
------------------+-------------------------+-----------+----------+---------------------------
 itemid           | bigint                  |           | not null |
 type             | integer                 |           | not null | 0
 snmp_oid         | character varying(512)  |           | not null | ''::character varying
 hostid           | bigint                  |           | not null |
 name             | character varying(255)  |           | not null | ''::character varying
 ...
 Indexes:
    "items_pkey" PRIMARY KEY, btree (itemid)
    "items_1" btree (hostid, key_)
    "items_3" btree (status)
    "items_4" btree (templateid)
    "items_5" btree (valuemapid)
    "items_6" btree (interfaceid)
    "items_7" btree (master_itemid)
    "items_8" btree (key_)
    "items_9" btree (hostid, name_upper)
迁移MySQL历史数据

# cd /root/migration
# vi data.pgloader  ##过滤掉history和trends七张表,每个大版本的表数量不相同,下面过滤的表请按实际版本中表数量过滤
LOAD DATABASE
FROM mysql://zabbix:*****@127.0.0.1:3306/zabbix
INTO postgresql://zabbix:*****@127.0.0.1:5432/zabbix
WITH include no drop,
no truncate,
create no tables,
create no indexes,
no foreign keys,
reset sequences,
data only
SET maintenance_work_mem TO '1024MB', work_mem TO '512MB'
EXCLUDING TABLE NAMES MATCHING 'acknowledges',
'actions',
'alerts',
'auditlog',
'autoreg_host',
'conditions',
'config',
'config_autoreg_tls',
'corr_condition',
'corr_condition_group',
'corr_condition_tag',
'corr_condition_tagpair',
'corr_condition_tagvalue',
'corr_operation',
'correlation',
'dashboard',
'dashboard_page',
'dashboard_user',
'dashboard_usrgrp',
'dbversion',
'dchecks',
'dhosts',
'drules',
'dservices',
'escalations',
'event_recovery',
'event_suppress',
'event_tag',
'events',
'expressions',
'functions',
'globalmacro',
'globalvars',
'graph_discovery',
'graph_theme',
'graphs',
'graphs_items',
'group_discovery',
'group_prototype',
'ha_node',
'host_discovery',
'host_inventory',
'host_tag',
'hostmacro',
'hosts',
'hosts_groups',
'hosts_templates',
'housekeeper',
'hstgrp',
'httpstep',
'httpstep_field',
'httpstepitem',
'httptest',
'httptest_field',
'httptest_tag',
'httptestitem',
'icon_map',
'icon_mapping',
'ids',
'images',
'interface',
'interface_discovery',
'interface_snmp',
'item_condition',
'item_discovery',
'item_parameter',
'item_preproc',
'item_rtdata',
'item_tag',
'items',
'lld_macro_path',
'lld_override',
'lld_override_condition',
'lld_override_opdiscover',
'lld_override_operation',
'lld_override_ophistory',
'lld_override_opinventory',
'lld_override_opperiod',
'lld_override_opseverity',
'lld_override_opstatus',
'lld_override_optag',
'lld_override_optemplate',
'lld_override_optrends',
'maintenance_tag',
'maintenances',
'maintenances_groups',
'maintenances_hosts',
'maintenances_windows',
'media',
'media_type',
'media_type_message',
'media_type_param',
'module',
'opcommand',
'opcommand_grp',
'opcommand_hst',
'opconditions',
'operations',
'opgroup',
'opinventory',
'opmessage',
'opmessage_grp',
'opmessage_usr',
'optemplate',
'problem',
'problem_tag',
'profiles',
'proxy_autoreg_host',
'proxy_dhistory',
'proxy_history',
'regexps',
'report',
'report_param',
'report_user',
'report_usrgrp',
'rights',
'role',
'role_rule',
'script_param',
'scripts',
'service_alarms',
'service_problem',
'service_problem_tag',
'service_status_rule',
'service_tag',
'services',
'services_links',
'sessions',
'sla',
'sla_excluded_downtime',
'sla_schedule',
'sla_service_tag',
'sysmap_element_trigger',
'sysmap_element_url',
'sysmap_shape',
'sysmap_url',
'sysmap_user',
'sysmap_usrgrp',
'sysmaps',
'sysmaps_element_tag',
'sysmaps_elements',
'sysmaps_link_triggers',
'sysmaps_links',
'tag_filter',
'task',
'task_acknowledge',
'task_check_now',
'task_close_problem',
'task_data',
'task_remote_command',
'task_remote_command_result',
'task_result',
'timeperiods',
'token',
'trigger_depends',
'trigger_discovery',
'trigger_queue',
'trigger_tag',
'triggers',
'users',
'users_groups',
'usrgrp',
'valuemap',
'valuemap_mapping',
'widget',
'widget_field'
ALTER SCHEMA 'zabbix' RENAME TO 'public';

# pgloader data.pgloader  ##只有一台Server监控数据用时较短
2023-06-11T21:00:32.007800+21:00 LOG pgloader version "3.6.7~devel"
2023-06-11T21:00:32.007800+21:00 LOG Migrating from #<MYSQL-CONNECTION mysql://zabbix@127.0.0.1:3306/zabbix {10067B4643}>
2023-06-11T21:00:32.007800+21:00 LOG Migrating into #<PGSQL-CONNECTION pgsql://zabbix@127.0.0.1:5432/zabbix {10067B47D3}>
2023-06-11T21:00:32.007800+21:00 WARNING Source column "public"."history_uint"."value" is casted to type "bigint" which is not the same as "numeric", the type of current target database column "public"."history_uint"."value".
2023-06-11T21:00:32.007800+21:00 WARNING Source column "public"."trends_uint"."value_min" is casted to type "bigint" which is not the same as "numeric", the type of current target database column "public"."trends_uint"."value_min".
2023-06-11T21:00:32.007800+21:00 WARNING Source column "public"."trends_uint"."value_avg" is casted to type "bigint" which is not the same as "numeric", the type of current target database column "public"."trends_uint"."value_avg".
2023-06-11T21:00:32.007800+21:00 WARNING Source column "public"."trends_uint"."value_max" is casted to type "bigint" which is not the same as "numeric", the type of current target database column "public"."trends_uint"."value_max".
2023-06-11T21:00:32.007800+21:00 LOG report summary reset
             table name     errors       rows      bytes      total time
-----------------------  ---------  ---------  ---------  --------------
        fetch meta data          0          7                     0.076s
-----------------------  ---------  ---------  ---------  --------------
         public.history          0       8008   286.8 kB          0.110s
    public.history_uint          0       2429    74.1 kB          0.067s
     public.trends_uint          0         50     1.6 kB          0.082s
    public.history_text          0          2    52.6 kB          0.117s
          public.trends          0        142     8.4 kB          0.020s
     public.history_str          0         12     0.9 kB          0.036s
     public.history_log          0          0                     0.025s
-----------------------  ---------  ---------  ---------  --------------
COPY Threads Completion          0          4                     0.171s
        Reset Sequences          0          0                     0.027s
       Install Comments          0          0                     0.000s
-----------------------  ---------  ---------  ---------  --------------
      Total import time          ✓      10643   424.5 kB          0.198s

6.查看历史数据迁移

psql -Uzabbix
\c zabbix
zabbix=> select * from history;
 itemid |   clock    |         value          |    ns     
--------+------------+------------------------+-----------
  10073 | 1670483513 |     0.8324606300857088 | 661499763
  10073 | 1670483573 |     1.0157088072329086 | 718082482
  10073 | 1670483693 |     0.8991120422218923 | 907381983
  10073 | 1670483753 |      1.015714184025525 | 963646786
  10073 | 1670483813 |     1.0329172154884476 |  19686404
  10073 | 1670483873 |     1.0158031677606336 |  70690315
  10073 | 1670483933 |     1.0157542389272938 | 124586880
  10073 | 1670483993 |      1.015691219317195 | 182209551
  10073 | 1670484053 |     1.0156428524089065 | 242692284

7.设置主外键

cat schema.sql |tail -n +2090 > altertable.sql > altertable.sql  ##将所有ALTER以及另一段sql放入altertable.sql中
psql -Uzabbix -dzabbix -f database/postgresql/altertable.sql
psql -Uzabbix
\c zabbix
\d+ items
...
Foreign-key constraints:
    "c_items_1" FOREIGN KEY (hostid) REFERENCES hosts(hostid) ON DELETE CASCADE
    "c_items_2" FOREIGN KEY (templateid) REFERENCES items(itemid) ON DELETE CASCADE
    "c_items_3" FOREIGN KEY (valuemapid) REFERENCES valuemap(valuemapid)
    "c_items_4" FOREIGN KEY (interfaceid) REFERENCES interface(interfaceid)
    "c_items_5" FOREIGN KEY (master_itemid) REFERENCES items(itemid) ON DELETE CASCADE

五、Zabbix前端访问测试

1.数据库连接配置

2.测试连接成功

注:按照实际环境测试环境先熟悉验证,不建议直接生产操作该步骤

博客可能不能及时回复问题,技术问题欢迎加入交流。

具有丰富的模板资源及模板开发能力、项目落地管理经验分享欢迎加入交流

微信号:king_songax

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/757729.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

1771_Windows下格式化Linux硬盘

全部学习汇总&#xff1a; GreyZhang/little_bits_of_linux: My notes on the trip of learning linux. (github.com) 我自己使用Linux系统多年&#xff0c;但是对于很多操作系统相关的知识我其实并不是很了解。我当初之所以使用Linux一是因为这个系统能够提供给我所有想要的工…

Unity游戏源码分享-Unity手游射击横版游戏

Unity游戏源码分享-Unity手游射击横版游戏 战斗场景 项目地址&#xff1a; https://download.csdn.net/download/Highning0007/88050256

Deployment:让应用永不宕机

“Deployment”&#xff0c;顾名思义&#xff0c;它是专门用来部署应用程序的&#xff0c;能够让应用永不宕机&#xff0c;多用来发布无状态的应用&#xff0c;是 Kubernetes 里最常用也是最有用的一个对象。 Deployment 的关键字段&#xff1a;先看 replicas 字段。它的含义比…

在Vitis IDE中使用第三方库 libtiff 保存 tiff 文件

目的和思路 一个Vitis IDE 裸机项目&#xff0c;需要将视频帧无损地保存下来 由于每帧的像素数据是 16bit 1通道的 bayer 格式&#xff0c;满足这一需求的图像格式似乎只有 tiff 格式 开源的tiff 库是 libtiff&#xff0c;而在 Vitis IDE 裸机项目中要使用的话就需要交叉编译…

AD22软件系统参数的一些基本设置

AD22软件系统参数设置 SystemData managementSchematicPCB Editor System Data management Schematic PCB Editor

集群基础5——keepalived对apache高可用

文章目录 一、基本了解二、配置文件参数释义2.1 默认配置文件2.2 定制配置文件2.2.1 vrrp_instance段配置参数2.2.2 vrrp_script段配置参数2.2.3 real_server段配置参数2.2.4 tcp_check段配置参数 三、keepalived对apache高可用3.1 环境说明3.2 安装服务3.3 配置主服务器3.4 配…

前端编码规范

prettier 配置 1. vscode 安装prettier 的 插件 2. 新建 .prettierrc 文件 {"semi": false, // 不尾随分号"singleQuote": true, // 使用单引号"trailingComma": "none" // 多行逗号分隔的语法&#xff0c;最后一行不加逗号 }eslin…

orbslam3 生成标定板rosrun kalibr kalibr_create_target_pdf --type

rosrun kalibr kalibr_create_target_pdf --type apriltag --nx 6 --ny 6 --tsize 0.08 --tspace 0.3小师妹要做相机视觉标定&#xff0c;需要制作棋盘格&#xff0c;无奈其电脑有些卡&#xff0c;对此毫无经验的博主从头开始安装&#xff08;此前博主已经安装了ROS环境&#x…

精品个人或团队引导页网站HTML源码_好看大气

2023全新宽屏大气好看团队个人指导页网站HTML源码&#xff0c;带音乐视频mv&#xff0c;源码比较小&#xff0c;只有七兆&#xff0c;就因为一个MV占了十几兆。源码也很漂亮&#xff0c;但是有个缺点就是没有手机适配&#xff0c;只能PC浏览器正常显示&#xff0c;手机不能完全…

“探索图像处理的奥秘:使用Python和OpenCV进行图像和视频处理“

1、上传图片移除背景后下载。在线抠图软件_图片去除背景 | remove.bg – remove.bg 2、对下载的图片放大2倍。ClipDrop - Image upscaler 3、对放大后的下载照片进行编辑。 4、使用deepfacelive进行换脸。 1&#xff09;将第三步的照片复制到指定文件夹。C:\myApp\deepfakeliv…

Java 设计模式——模板方法模式

目录 1.概述2.结构3.案例实现3.1.抽象类3.2.具体子类3.3.测试 4.优缺点5.使用场景6.JDK 源码解析6.1.InputStream6.2.AbstractQueuedSynchronizer 1.概述 &#xff08;1&#xff09;在面向对象程序设计过程中&#xff0c;程序员常常会遇到这种情况&#xff1a;设计一个系统时知…

【代码随想录 | Leetcode | 第三天】数组 | 滑动窗口 | 209

前言 欢迎来到小K的Leetcode|代码随想录|专题化专栏&#xff0c;今天将为大家带来滑动窗口的分享✨ 目录 前言209. 长度最小的子数组总结 209. 长度最小的子数组 ✨题目链接点这里 给定一个含有 n 个正整数的数组和一个正整数target。找出该数组中满足其和 ≥ target 的长度…

docker在arm64架构ubuntu系统的安装

卸载可能存在的旧版本 sudo apt remove docker docker-engine docker-ce docker-io安装依赖使apt可通过HTTPS下载包 sudo apt update && apt install -y apt-tranport-https ca-certificates curl software-properties-commonapt-transport-https用于支持通过HTTPS协…

如何设计光场2.0(聚焦型光场相机)系统参数

1. 系统参数设计 目前的硬件系统的现状&#xff1a;主透镜50mm&#xff0c;MLA&#xff1a;15*15&#xff0c;d0.5mm&#xff0c;f15mm&#xff0c;s4.8um 开普勒型光场系统&#xff1a; 首先我们需要确定系统的M&#xff0c;M参数表示单个位置的点能被多少个小微透镜成像&am…

C++【哈希表的完善及封装】

✨个人主页&#xff1a; 北 海 &#x1f389;所属专栏&#xff1a; C修行之路 &#x1f383;操作环境&#xff1a; Visual Studio 2019 版本 16.11.17 文章目录 &#x1f307;前言&#x1f3d9;️正文1、哈希表的完善1.1、拷贝与赋值1.2、优化&#xff1a;哈希函数1.3、优化&am…

带你快速了解字符(串)函数

​ ⭐ 作者&#xff1a;小胡_不糊涂 &#x1f331; 作者主页&#xff1a;小胡_不糊涂的个人主页 &#x1f496; 持续更文&#xff0c;谢谢大家支持 &#x1f496; 文章目录 本文重点1. strlen函数1.1 模拟实现 2. strcpy函数2.1 模拟实现 3. strcat函数3.1 模拟实现 4. strcmp函…

基于linux下的高并发服务器开发(第一章)- 目录遍历函数

10 / 目录遍历函数 // 打开一个目录 #include <sys/types.h> #include <dirent.h>DIR *opendir(const char *name); 参数&#xff1a; - name: 需要打开的目录的名称 返回值&#xff1a; DIR * 类型&#xff0c;理解为目录流 错误…

Hcip第五次作业----BGP联邦综合实验

配置IP地址 r1 [r1]int g0/0/0 [r1-GigabitEthernet0/0/0]ip add 12.0.0.1 24 [r1-GigabitEthernet0/0/0]int lo0 [r1-LoopBack0]ip add 192.168.1.1 24 [r1-LoopBack0]int lo1 [r1-LoopBack1]ip add 10.0.0.1 24 r2 [r2]int g0/0/0 [r2-GigabitEthernet0/0/0]ip add 12.0.0.2…

Orangepi Zero2 基于官方外设开发(二)

一、OLED屏显示-IIC协议 1、相关介绍 IIC及OLED相关内容请参考以下文章&#xff1a; IIC协议_单行梦想家的博客-CSDN博客 OLED显示屏_单行梦想家的博客-CSDN博客 2、OrangePi的IIC接口 由原理图可知&#xff0c;Orange Pi Zero 2 可用的 i2c 为 i2c3 Linux系统启动后&…

针对我国水资源量设计的农田灌溉收费管理平台

安科瑞虞佳豪 降水量 2013年&#xff0c;全国平均降水量661.9mm&#xff0c;折合降水总量62674.4亿立方米&#xff0c;比常年值偏多3.0%。从水资源分区看&#xff0c;松花江、辽河、海河、黄河、淮河、西北诸河6个水资源一级区&#xff08;以下简称北方6区&#xff09;平均降水…