English 简体中文 繁體中文 한국 사람 日本語 Deutsch русский بالعربية TÜRKÇE português คนไทย french
查看: 2|回复: 0

Pgloader极简教程

[复制链接]
查看: 2|回复: 0

Pgloader极简教程

[复制链接]
查看: 2|回复: 0

341

主题

0

回帖

1033

积分

金牌会员

积分
1033
okkkk

341

主题

0

回帖

1033

积分

金牌会员

积分
1033
2025-2-7 02:54:07 | 显示全部楼层 |阅读模式
目录


简介

pgloader是一个数据同步工具,用来将数据从其它地方迁移到postgresql中,支持从如下源迁移:

  • 文件:CSV、Fixed Format、Postgres COPY text format、DBF、IXF
  • 数据库系统:SQLite、MySql、MSSQLServer、PostgreSQL、Redshift
应用场景
需要往postgresql中导入数据的时候,如数据迁移。
安装

安装概述

安装方式比较丰富,详见  https://pgloader.readthedocs.io/en/latest/install.html  。
遗憾的是未提供CentOS环境编译好的程序供下载,所以需要手动编译安装。
CentOS编译安装

去官网下载最新源码:
https://github.com/dimitri/pgloader
将源码放到 /usr/bin下,本文为例:
[root@bogon pgloader-3.6.9]# pwd/usr/local/pgloader-3.6.9如果下载的是源码压缩包需要使用如下命令解压:
tar -zxvf pgloader-3.6.9.tar.gz赋予脚本执行权限:
cd /usr/local/pgloader-3.6.9chmod -R 777 *执行 bootstrap-centos7.sh 脚本,下载相关依赖
bootstrap-centos7.sh执行编译:
make  pgloader如果有提示到 ("libcrypto.so.1.1" "libcrypto.so.1.0.0" "libcrypto.so.3" "libcrypto.so") 没有找到或者相关信息
需要先安装 openssl
yum -y install openssl openssl-devel复制编译好的程序到系统执行目录 /usr/local/bin/ 下
cp  /usr/local/pgloader-3.6.9/build/bin/pgloader        /usr/local/bin/查看是否安装好了:
[root@bogon home]# pgloader --versionpgloader version "3.6.7~devel"compiled with SBCL 2.2.5使用

pgloader 有两种常见的使用方式:

  • 通过命令行
  • 通过迁移配置文件
命令行

如下命令行:
pgloader mysql://user:password@ip:3306/dbName postgresql://user:password@ip:5432/dbName

  • 将名为dbName的数据库结构和数据 从mysql迁移到postgresql
  • pgloader 为上述 /usr/local/bin/pgloader 的可执行文件
  • 后面是mysql 的连接信息 , postgresql 的连接信息,中间使用空格分隔
  • 需要使用有写入权限的账号,建议使用root用户操作
配置文件迁移

另外一种方式就是编写迁移配置文件,然后使用  pgloader  sync.load 命令执行配置文件。
如下配置文件演示了仅同步mysql的source_db库中的ramble_doc 表到 postgresql中的target_db库中,执行完毕之后将在postgresql中新建一个名为ramble_doc 的表,并新增数据。
LOAD DATABASE     FROM     mysql://root:xxx@192.168.1.92:3306/source_db     INTO     postgresql://postgres:xxx@192.168.1.24:5432/target_dbINCLUDING ONLY TABLE NAMES matching 'ramble_doc' ;

  • LOAD DATABASE :表示从数据库执行迁移
  • FROM :源数据库连接信息
  • INTO :目标数据库连接信息
  • INCLUDING ONLY TABLE NAMES matching :仅包含匹配的表
  • 最后那个分号不可少
  • 配置文件需要按照格式编写,如缩进
如下配置文件演示了同步mysql 的source_db库下所有表到postgresql的target_db库下面,包含表结构和数据。
LOAD DATABASE     FROM     mysql://root:xxx@192.168.1.92:3306/source_db     INTO     postgresql://postgres:xxx@192.168.1.24:5432/target_db     WITH batch rows = 10000 , batch size =200MB , prefetch rows  = 5000 , workers = 4 ,concurrency = 3;

  • WITH:with 后面可以追加一些附属参数,各个参数使用英文逗号分隔。常见的参数如:是否需要同步数据还是仅同步结构,是否在写入数据前先删除表等
  • batch rows :在同步数据的时候分批插入postgresql的行数,默认为2.5万。
  • batch size:每批最大数据大小,设置此参数可避免出现内存溢出
  • prefetch rows:在同步数据的时候分批从mysql读取的行数,默认为1000。
  • workders: 线程数量
  • concurrency:并发线程数量
可能遇到的错误

内存溢出
报错信息为:
Heap exhausted during garbage collection: 64 bytes available, 80 requested.垃圾回收期间堆已耗尽:可用64个字节,请求80个字节。解决方案为调优分批数量和并发数量,需要根据源数据库数据量,硬件情况不断尝试。
更多配置参考

官网给了一个例子:
LOAD DATABASE     FROM      mysql://root@localhost/sakila     INTO postgresql://localhost:54393/sakila WITH include drop, create tables, create indexes, reset sequences,      workers = 8, concurrency = 1,      multiple readers per thread, rows per range = 50000  SET PostgreSQL PARAMETERS      maintenance_work_mem to '128MB',      work_mem to '12MB',      search_path to 'sakila, public, "$user"'  SET MySQL PARAMETERS      net_read_timeout  = '120',      net_write_timeout = '120' CAST type bigint when (= precision 20) to bigserial drop typemod,      type date drop not null drop default using zero-dates-to-null,      -- type tinyint to boolean using tinyint-to-boolean,      type year to integer MATERIALIZE VIEWS film_list, staff_list -- INCLUDING ONLY TABLE NAMES MATCHING ~/film/, 'actor' -- EXCLUDING TABLE NAMES MATCHING ~<ory> -- DECODING TABLE NAMES MATCHING ~/messed/, ~/encoding/ AS utf8 -- ALTER TABLE NAMES MATCHING 'film' RENAME TO 'films' -- ALTER TABLE NAMES MATCHING ~/_list$/ SET SCHEMA 'mv' ALTER TABLE NAMES MATCHING ~/_list$/, 'sales_by_store', ~/sales_by/  SET SCHEMA 'mv' ALTER TABLE NAMES MATCHING 'film' RENAME TO 'films' ALTER TABLE NAMES MATCHING ~/./ SET (fillfactor='40') ALTER SCHEMA 'sakila' RENAME TO 'pagila' BEFORE LOAD DO   $$ create schema if not exists pagila; $$,   $$ create schema if not exists mv;     $$,   $$ alter database sakila set search_path to pagila, mv, public; $$;   https://pgloader.readthedocs.io/en/latest/ref/mysql.html
总结

pgloader是一个数据库迁移工具,花一点点时间研究一下如何使用,将在数据库迁移的时候起到事半功倍的效果,往往比自己编写迁移脚本更加完善和可靠。
引用

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

341

主题

0

回帖

1033

积分

金牌会员

积分
1033

QQ|智能设备 | 粤ICP备2024353841号-1

GMT+8, 2025-3-10 15:53 , Processed in 0.907017 second(s), 27 queries .

Powered by 智能设备

©2025

|网站地图