Skip to main content

mylox

PyPI Status Python versions CI License

MySQL binlog → PostgreSQL 变更数据捕获(CDC)。 从 MySQL 从库的 binlog 读行变更,按 pipeline 分发给你的 handler 写进 PostgreSQL; 表结构靠「information_schema 快照 + sqlglot 本地解析 DDL」维护,不需要在 MySQL 里建影子表。

pip install mylox

当前是 beta(0.8.x)。 核心路径已在 MySQL 5.7.44 / 8.4.7 与 PostgreSQL 16 / 18 上 用 178 个单测 + 9 个真机集成用例验证;但 1.0 之前 API(尤其是 CDCHandler 的签名和 EngineConfig 的字段)仍可能微调,升级前请看一眼 CHANGELOG。

它解决什么问题

做 MySQL → PostgreSQL 实时同步,绕不开三件麻烦事:

  1. 表结构会变。 mylox 启动时拉一次 information_schema,之后直接解析 binlog 里的 DDL 文本演进内存模型,解析不了才回退查一次库——不再需要维护影子表, 也不需要主库连接(只需要从库)。
  2. 位点不能丢。 业务写入和位点更新在同一个 PostgreSQL 事务里提交; 每行数据的 schema 从内存时间线按位点解析,不查库。
  3. 一个业务不能拖垮全部。 多个 pipeline 共享一条 binlog 流,各有独立位点; 某个 pipeline 的业务代码抛异常只会摘除它自己,其余继续推进。

再加上 GTID 支持(换从库自动切 auto_position 续传)、断线退避重连, 以及 MySQL 5.7 / 8.4 的语句差异回退,就是一个能长期跑的同步进程。

30 秒上手

from mylox import CDCEngine, CDCHandler, EngineConfig, MySQLSettings, PostgresSettings, RowChange


class Mirror(CDCHandler):
    """把 shop.orders 的行变更 upsert 进 dw.orders。"""

    def on_init(self, pipelines, ctx):
        # 冻结锚点上的全量装载:流式读源库,独立连接写目标库
        with ctx.mysql_cursor(streaming=True) as cur:
            cur.execute("SELECT * FROM shop.orders")
            rows = list(cur)
        with ctx.pg_cursor() as cur:
            for row in rows:
                cur.execute(
                    "INSERT INTO dw.orders (id, amount) VALUES (%s, %s) ON CONFLICT (id) DO NOTHING",
                    (row["id"], row["amount"]),
                )

    def handle_row(self, change: RowChange, pipeline: str, cur) -> None:
        if change.action == "DELETE":
            cur.execute("DELETE FROM dw.orders WHERE id = %s", (change.before["id"],))
        elif change.action == "UPDATE":
            sets = ", ".join("%s = %%s" % column for column in change.modify)
            cur.execute(
                "UPDATE dw.orders SET %s WHERE id = %%s" % sets,
                tuple(change.modify.values()) + (change.before["id"],),
            )
        else:
            cur.execute(
                "INSERT INTO dw.orders (id, amount) VALUES (%s, %s) "
                "ON CONFLICT (id) DO UPDATE SET amount = EXCLUDED.amount",
                (change.after["id"], change.after["amount"]),
            )


engine = CDCEngine(
    MySQLSettings(host="replica", port=3307, user="cdc", password="***"),
    PostgresSettings(host="pg", port=5432, dbname="dw", user="cdc", password="***"),
    pipelines=["orders"],
    handler=Mirror(),
    config=EngineConfig(filter_rules=["*", "!shop.*"]),  # 只要 shop 库(gitignore 语义,见下)
)
engine.run()

handle_row 里不要 commit/rollback——事务边界由引擎管理,每个 pipeline 的调用 都包在 SAVEPOINT 里。完整可运行的例子(按源表建镜像表、类型映射、幂等 upsert)见 example/mirror_to_pg.py。

⚠️ 过滤规则默认是 gitignore 语义:普通 pattern 表示排除,! 前缀表示重新包含。 「只要 shop 库」要写 ["*", "!shop.*"],而 ["shop.*"] 是排除 shop 库。 两种方言的对照表见配置。

环境要求

组件 要求
MySQL 5.7 / 8.4 实测通过;需要 log_bin=ON、binlog_format=ROW、gtid_mode=ON、log_slave_updates=ON
PostgreSQL 16 / 18 实测通过;首次运行会自动建 mylox_checkpoint、mylox_schema_history
Python 3.10+(本地在 3.14 上全量验证)
连接 只需要从库:binlog 流 + information_schema + STOP/START REPLICA;主库不需要连接

从库账号需要 SELECT、REPLICATION SLAVE、REPLICATION CLIENT、REPLICATION SLAVE ADMIN (或 SUPER,用于初始化时暂停复制)。

文档

文档 内容
架构与实现 schema 混合获取、时间线、引擎主循环、事务与位点、状态表、代码阅读路线
编写 Handler RowChange 字段、幂等写法、on_init 全量装载、失败回调、多 pipeline 分工
配置 连接设置、EngineConfig 全量配置、过滤规则的两种方言
运维与排障 部署拓扑、启动行为、信号与退出、排障清单、已知限制、数据恢复
版本兼容性 MySQL 5.7 与 8.4 的语句差异、实测结论、支持的版本范围
测试 单元/集成测试怎么跑、环境变量、每个用例证明什么、实测结果
开发与发布 本地环境、目录结构、日常命令、发版流程(GitHub Actions → PyPI)
设计取舍与缺陷对照 为什么这么设计、每个已知缺陷是怎么修的

项目结构

src/mylox/
├── schema/     # ★ 混合 schema:model 时间线 / DDL 解析 / information_schema 读取 / 编排
├── engine/     # binlog 主循环、pipeline 状态、handler API、reader 工厂
├── state/      # PostgreSQL 位点与 schema 历史(双连接:业务事务 + 元数据)
├── utils/      # 位点比较、位图清洗、序列化
├── gtid.py filtering.py config.py errors.py constants.py
docs/           # 用户与开发文档(不打进 wheel)
example/        # 可运行示例
test/           # 单测(伪 binlog 事件)+ 集成测试(真实主从)

测试状态

两个 MySQL 版本各跑一遍:187 passed(178 单测 + 9 集成), 环境为 MySQL 5.7.44 / 8.4.7(各一主两从)+ PostgreSQL 18.6。 单测不需要数据库:

pytest test -q --ignore=test/test_integration.py

集成测试的拓扑要求、环境变量与每个用例的断言见测试。

贡献 / 许可

欢迎 issue 和 PR,先看一眼 CONTRIBUTING.md。 本项目使用 MIT 许可证。

Metadata

Release files for mylox 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mylox 0.8.0
File Size Uploaded Provenance Actions
mylox-0.8.0.tar.gz 129.9 kB With provenance Details for mylox-0.8.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mylox 0.8.0
File Interpreter ABI Platform Actions
mylox-0.8.0-py3-none-any.whl Python 3 none any Details for mylox-0.8.0-py3-none-any.whl

Total release size: 205.3 kB

Release files / mylox-0.8.0.tar.gz

Download URL mylox-0.8.0.tar.gz
Size 129.9 kB
Tags Source
SHA-256 checksum
How to use checksums
58a34ed5dc44411cd027c96ced17d205f2cd388867a697f61008ef8404d9b52a
BLAKE2b-256 checksum
How to use checksums
04e53b7e0c8d3c4fe31a6a6aa1d5b52cc62abfa1aea57243b69041a23d2820d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Release files / mylox-0.8.0-py3-none-any.whl

Download URL mylox-0.8.0-py3-none-any.whl
Size 75.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9f55c0a550948b407e13ab6eaa600b0b46b2de7f81366785accb2ac187e1a58a
BLAKE2b-256 checksum
How to use checksums
8b8eb0f8f75cb8620be9c66ff9c818d3ee7f515f6e847ef8eb9bfbfd18d2a1a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Security for mylox 0.8.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mylox-0.8.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.8.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page