阕阵闲 发表于 2025-6-11 10:35:12

postgresql实时同步到mysql

应客户要求,需要同步数据到他们自己的数据库用于简单的数据分析,但这部分数据在postgresql,客户又不想再建pg,想直接同步到他们现有的mysql库,实时性倒是不要求。
考虑到
1、异构数据库同步
2、只同步指定客户的行数据
有之前同步到es的经验,同样使用了腾讯oceanus,其它工具没搞定
客户库中创建表

CREATE TABLE tb_1 (
    id bigint primary key,
    did bigint,
    gid bigint,
    fee DECIMAL(10,2),
    created_at timestamp,
    type smallint,
    remark string ,
    key i_did(did)
);创建SQL作业

CREATE TABLE tb_1 (
    id bigint,
    did bigint,
    gid bigint,
    fee DECIMAL(10,2),
    create_time timestamp,
    type smallint,
    remark string,
    PRIMARY KEY (`id`) NOT ENFORCED
) WITH (
'connector' = 'postgres-cdc',
'hostname' = 'ip',
'port' = '5432',            
'username' = 'user',
'password' = 'pwd',
'database-name' = 'db',
'schema-name' = 'your-schema',
'table-name' = 'tbname',
'slot.name' = 'slotname_tb_1',
'scan.incremental.snapshot.enabled' = 'true'
);



CREATE TABLE kh_tb_1 (
    id bigint,
    did bigint,
    gid bigint,
    fee DECIMAL(10,2),
    create_time timestamp,
    type smallint,
    remark string,
    PRIMARY KEY (`id`) NOT ENFORCED
) WITH (
'connector' = 'jdbc',
'url' = 'jdbc:mysql://xxxxxx:3306/db?rewriteBatchedStatements=true&serverTimezone=Asia/Shanghai',
'table-name' = 'tb_1',
'username' = 'user',
'password' = 'pwd',
'sink.buffer-flush.max-rows' = '5000',
'sink.buffer-flush.interval' = '2s',
'sink.max-retries' = '10'
);

insert into kh_tb_1 select * from tb_1 where did=xxxxxxx;需要注意的:
1.字段类型要合理和对应,跟着cdc的类型走,不跟数据库一样
2.只有这个客户数据,insert不要忘了加where
3.pg涉及同步slot, slot.name要一张表一个,表多的话,要修改pg参数,max_replication_slots(默认是10,修改此参数要重启)
4.报错: ERROR: cannot delete from table "tb_1" because it does not have a replica identity ,调整下表 alter table tb_1 REPLICA IDENTITY FULL;
启动作业任务即可。
欢迎关注我的公众号:老王76。一起进步吧!

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

锄淫鲷 发表于 2025-11-6 19:44:23

前排留名,哈哈哈

事值 发表于 2025-11-12 11:29:32

谢谢分享,辛苦了

笃迩讦 发表于 2025-12-9 09:42:12

东西不错很实用谢谢分享

蝌棚煌 发表于 2025-12-15 12:00:39

感谢分享,下载保存了,貌似很强大

刘凤 发表于 2025-12-17 22:48:40

新版吗?好像是停更了吧。

匣卒 发表于 2025-12-21 20:13:06

鼓励转贴优秀软件安全工具和文档!

泠邸 发表于 2025-12-22 01:20:26

感谢分享

峰埋姚 发表于 2025-12-25 09:56:53

过来提前占个楼

宁觅波 发表于 2025-12-27 16:59:20

这个好,看起来很实用

羊舌正清 发表于 2026-1-15 11:58:34

这个好,看起来很实用

供挂 发表于 2026-1-18 07:02:52

热心回复!

笙芝 发表于 2026-1-19 02:22:45

谢谢分享,试用一下

齐娅晶 发表于 2026-1-21 22:25:59

喜欢鼓捣这些软件,现在用得少,谢谢分享!

喳谍 发表于 2026-1-22 12:59:03

这个有用。

趣侮 发表于 2026-1-29 07:15:36

东西不错很实用谢谢分享

左丘雅秀 发表于 2026-1-29 08:07:39

感谢分享

柏雅云 发表于 2026-1-31 21:08:16

谢谢楼主提供!

二艰糖 发表于 2026-2-1 02:17:14

这个好,看起来很实用

汹萃热 发表于 2026-2-1 02:25:49

很好很强大我过来先占个楼 待编辑
页: [1] 2 3
查看完整版本: postgresql实时同步到mysql