暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

[象讯] 开源项目 wal2mongo 更新

原创 严少安 2024-06-17
314

ivory3.png

wal2mongo

wal2mongo 是由瀚高软件开发的一个 PostgreSQL 逻辑解码输出插件,旨在通过将输出格式化为 mongo 接受的类似 JSON 的格式,从而简化 PostgreSQL 到 MongoDB 的逻辑复制。

开源地址为: https://github.com/HighgoSoftware/wal2mongo

2024.6.12,wal2mongo v1.0.7 版本发布。

本文将基于 IvorySQL 数据库介绍 wal2mongo 的常见用法。

IvorySQL

IvorySQL 项目是瀚高软件提出的一个开源项目,旨在将 Oracle 兼容性功能添加到流行的 PostgreSQL 数据库中。IvorySQL 添加了 compatible_db 切换开关,可在 Oracle 和 PostgreSQL 兼容模式之间切换。IvorySQL 项目是在 Apache 2 许可下发布的,鼓励各种类型的贡献,欢迎你成为 IvorySQL 社区贡献者。

IvorySQL 官方网站:https://www.ivorysql.org/zh-cn/

环境准备

当前环境使用的是 Rocky Linux 9.4,搭载 IvorySQL 3.2 数据库。

[ivorysql@ivorysql3 ~]$ hostnamectl Static hostname: ivorysql3.shawnyan.cn Operating System: Rocky Linux 9.4 (Blue Onyx) [ivorysql@ivorysql3 ~]$ psql -Atc 'select version()' PostgreSQL 16.2-ShawnYan (IvorySQL 3.2) on x86_64-pc-linux-gnu, compiled by gcc (GCC) 11.4.1 20231218 (Red Hat 11.4.1-3), 64-bit

数据库配置项 wal_level 需设定为 logical

wal2mongo 安装

下载 wal2mongo 源码,并进行编译。

cd wal2mongo USE_PGXS=1 make USE_PGXS=1 make install

输出:

[ivorysql@ivorysql3 wal2mongo]$ USE_PGXS=1 make gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2 -fPIC -fvisibility=hidden -I. -I./ -I/opt/ivorysql3/include/postgresql/server -I/opt/ivorysql3/include/postgresql/internal -D_GNU_SOURCE -c -o wal2mongo.o wal2mongo.c gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2 -fPIC -fvisibility=hidden wal2mongo.o -L/opt/ivorysql3/lib -Wl,--as-needed -Wl,-rpath,'/opt/ivorysql3/lib',--enable-new-dtags -fvisibility=hidden -shared -o wal2mongo.so [ivorysql@ivorysql3 wal2mongo]$ USE_PGXS=1 make install /usr/bin/mkdir -p '/opt/ivorysql3/lib/postgresql' /usr/bin/install -c -m 755 wal2mongo.so '/opt/ivorysql3/lib/postgresql/'

wal2mongo 用例

创建复制槽

ivorysql=# SELECT * FROM pg_create_logical_replication_slot('w2m_slot', 'wal2mongo'); slot_name | lsn -----------+----------- w2m_slot | 0/27C9F30 (1 row) ivorysql=# SELECT slot_name, plugin, slot_type, database, active, restart_lsn, confirmed_flush_lsn FROM pg_replication_slots; slot_name | plugin | slot_type | database | active | restart_lsn | confirmed_flush_lsn -----------+-----------+-----------+----------+--------+-------------+--------------------- w2m_slot | wal2mongo | logical | ivorysql | f | 0/27C9EF8 | 0/27C9F30 (1 row)

创建测试数据

ivorysql=# create table website (id int, title varchar(100), url varchar(200)); CREATE TABLE ivorysql=# insert into website values (1,'ivorysql','www.ivorysql.org'); INSERT 0 1

查看数据变化

ivorysql=# SELECT * FROM pg_logical_slot_peek_changes('w2m_slot', NULL, NULL); lsn | xid | data -----------+------+------------------------------------------------------------------------------------------- 0/27E20E0 | 1166 | use mycluster_ivorysql_w2m_slot; 0/27E20E0 | 1166 | db.website.insertOne( { id: NumberInt("1"), title:"ivorysql", url:"www.ivorysql.org" } ); (2 rows)

写入验证

将 wal2mongo 解析出的语句写入到 MongoDB 中,验证语句正确性。

$ mongosh
Current Mongosh Log ID: 666fd64f9502155720597192
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.9
Using MongoDB:          7.0.11
Using Mongosh:          2.2.9

test> use mycluster_ivorysql_w2m_slot;
switched to db mycluster_ivorysql_w2m_slot
mycluster_ivorysql_w2m_slot> db.website.insertOne( { id: NumberInt("1"), title:"ivorysql", url:"www.ivorysql.org" } );
{
  acknowledged: true,
  insertedId: ObjectId('666fd66c9502155720597193')
}
mycluster_ivorysql_w2m_slot> db.website.find();
[
  {
    _id: ObjectId('666fd66c9502155720597193'),
    id: 1,
    title: 'ivorysql',
    url: 'www.ivorysql.org'
  }
]

🌻 往期精彩 ▼

[Oracle]

[MySQL]

[TiDB]

[PG]


– / END / –

👉 这里可以找到我

如果这篇文章为你带来了灵感或启发,就请帮忙点『赞』or『在看』or『转发』吧,感谢!(๑˃̵ᴗ˂̵)

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论