MySQL CDC 到 BigQuery:周期性同步的局限及 Binlog 的解决方案
MySQL CDC to BigQuery: what periodic syncs miss, and how binlog avoids it

原始链接: https://www.erathos.com/en/blog/mysql-cdc-to-bigquery

传统的 MySQL 到数据仓库的同步方式通常依赖定时 `SELECT` 查询,这种方式难以追踪删除操作、捕获中间行状态,且会给生产数据库造成不必要的负担。 变更数据捕获(CDC)通过直接读取 MySQL 的二进制日志,提供了一种更可靠的替代方案。由于 CDC 可以实时捕获每一次 `INSERT`、`UPDATE` 和 `DELETE` 操作,它能提供完整且准确的数据变更历史,确保无论流水线运行频率如何,都能保证数据的一致性。 成功实施 CDC 需要特定的 MySQL 配置: * **二进制日志 (Binary Logging):** 必须设置为 `ROW` 格式,并使用 `FULL` 行镜像。 * **权限:** 连接器需要具备 `REPLICATION SLAVE` 和 `REPLICATION CLIENT` 权限。 * **维护:** 必须管理 `server-id` 的唯一性,并确保 binlog 保留时间足够长,以应对停机情况。 虽然手动构建此类基础设施非常复杂,但像 Erathos 这样的托管平台可以自动处理快照、错误恢复和冲突管理等繁琐事务。归根结底,从批量同步转向 CDC 不仅仅是为了速度,更是为了透明度——确保你的数据仓库能够反映生产环境的全部真相,而不仅仅是周期性的快照。

```Hacker News 最新 | 过往 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 MySQL CDC 到 BigQuery:周期性同步的缺失,以及 Binlog 如何规避(erathos.com) 7 分,gpaulbagetti,43 分钟前 | 隐藏 | 过往 | 收藏 | 1 条评论 帮助 gpaulbagetti 43 分钟前 [-] 在多次看到同样的故障模式后写了这篇文章:一个 MySQL 同步任务在仪表盘上看起来很正常,但实际上已经在几个月内静默丢失了删除操作和中间更新,原因在于它使用的是快照对比而非读取 Binlog。我尝试详细梳理了 MySQL 端必须满足的条件(行镜像、binlog_row_value_options、server-id、保留策略),以确保 CDC 真正完整,而不仅仅是“最终一致”。欢迎在评论区深入探讨。 回复 准则 | 常见问题 | 列表 | API | 安全 | 法律 | 加入 YC | 联系 搜索:```
相关文章

原文

Most MySQL-to-warehouse pipelines run on the same pattern: a scheduled job selects rows, compares them to what was there before, and writes the difference. It works, until it doesn't.

What periodic syncs miss

A SELECT-based sync only sees what exists right now. It has no way to know a row existed and was deleted between two runs, no way to see intermediate states of a row that changed more than once, and it puts real load on your production database every time it scans a large table just to find a handful of changed rows.

What CDC does differently

Change Data Capture reads directly from MySQL's binary log (binlog), the same mechanism MySQL uses internally for replication. Every INSERT, UPDATE, and DELETE is captured as it's written to the log, in order, with the complete row state. Nothing is inferred by comparison. Nothing depends on when a batch job happens to run.

This isn't about speed. A CDC pipeline that runs once an hour is still fundamentally more reliable than a batch sync that runs once a minute, because it captures everything that happened, not just the latest snapshot.

What has to be true on the MySQL side

CDC via binlog has real prerequisites:

  1. Binary logging in ROW format, with FULL row images. If binlog_row_image isn't set to FULL, DELETE and UPDATE events won't carry the complete before/after state, only what's strictly needed to apply the change. That's often not enough for a downstream consumer that needs the full row.
  2. binlog_row_value_options must not be PARTIAL_JSON. If it is, updates to JSON columns only log what changed inside the JSON value, not the full value. Silent, and easy to miss until you compare against the source.
  3. The replication user needs REPLICATION SLAVE and REPLICATION CLIENT privileges to read and monitor the binlog, plus SELECT, RELOAD, and SHOW DATABASES for the initial snapshot.
  4. A unique server-id for every replication client attached to the database, including your CDC connection. Collisions with existing replicas cause silent failures that are painful to debug.
  5. Binlog retention long enough to cover downtime. MySQL purges binlog files after a configurable window (30 days by default). If your CDC connection is offline longer than that, it won't be able to resume from where it left off. It'll need a fresh initial snapshot.

Setting it up

GRANT SELECT, RELOAD, SHOW DATABASES, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'your_user';
FLUSH PRIVILEGES;

Then confirm your binlog configuration:

SHOW VARIABLES LIKE 'log_bin';
SHOW VARIABLES LIKE 'binlog_format';
SHOW VARIABLES LIKE 'binlog_row_image';

If any of those aren't set correctly, they go in my.cnf, and MySQL needs a restart to apply them.

The full connector documentation, including every prerequisite and troubleshooting step, is at docs.erathos.com/connectors/databases/mysql#cdc-setup.

This is where a managed platform earns its keep. Erathos and similar tools handle the snapshot mode selection (full initial snapshot vs. binlog-only), the server-id assignment and collision avoidance, and the recovery logic when a binlog gets purged before the connection catches up, so the person running the pipeline doesn't have to rebuild that logic by hand every time a new source gets connected.

Landing it in BigQuery

Once CDC is capturing changes correctly, the destination side is comparatively simple: each change event maps to a row operation in your BigQuery tables. The part worth getting right isn't the load into BigQuery, it's making sure what arrives there is complete. A pipeline that lands incomplete data on time is worse than one that's occasionally a few minutes behind but never wrong.

If your team is still running full-table batch syncs against production MySQL, the question worth asking isn't "how do we make this faster." It's "what are we currently unable to see."

If you want to try this in practice, you can create an Erathos account and connect your MySQL source with CDC enabled in a few minutes.

联系我们 contact @ memedata.com