Skip to main content

mysql-event-stream — Python Binding

CI PyPI License Python MySQL MariaDB Platform

A lightweight CDC (Change Data Capture) engine for Python supporting MySQL 8.4+ and MariaDB 10.11+. Parses binlog replication streams and emits structured row-level change events (INSERT / UPDATE / DELETE).

Built on a self-contained C++ core using ctypes FFI for high throughput and low latency. No external MySQL client library (libmysqlclient) required.

Install

pip install mysql-event-stream

Platform wheels are available for:

  • Linux x86_64 and aarch64 (recommended for server deployments)
  • macOS 15.0 or newer on x86_64 and arm64 (development use)

Usage

Parsing binlog bytes

from mysql_event_stream import CdcEngine

engine = CdcEngine()

# Only needed when a checksum=NONE byte stream starts after its FDE:
# engine.set_checksum_enabled(False)

# Feed raw binlog bytes. feed() stops early once the event queue is full, so
# drain the queue and re-feed the unconsumed tail instead of dropping it.
offset = 0
while offset < len(binlog_chunk):
    consumed = engine.feed(binlog_chunk[offset:])
    offset += consumed

    while (event := engine.next_event()) is not None:
        print(event.type, event.database, event.table)
        print("before:", event.before)
        print("after:", event.after)

    if consumed == 0:
        # Partial event at the tail: retain binlog_chunk[offset:] and prepend
        # it to the next chunk.
        break

Streaming from MySQL

import asyncio
from mysql_event_stream import CdcStream


async def main():
    async for event in CdcStream(
        host="127.0.0.1",
        port=3306,
        user="replicator",
        password="secret",
    ):
        print(f"{event.type.name} {event.database}.{event.table}")
        print(f"  before: {event.before}")
        print(f"  after:  {event.after}")


asyncio.run(main())

Low-level client

BinlogClient exposes explicit connect(), start(), poll(), stop(), disconnect(), and close() calls for applications that own their own event loop. ClientConfig and SslMode describe its connection settings; PollResult contains packet data or a heartbeat. CdcStream is the higher-level async iterator and is the usual choice.

from mysql_event_stream import BinlogClient, SslMode

with BinlogClient(user="replicator", password="secret", ssl_mode=SslMode.REQUIRED) as client:
    client.connect()
    client.start()
    result = client.poll()

Errors and logging

ParseError, DecodeError, and ChecksumError identify malformed binlog input. Native failures also carry a stable MesErrorCode. Install a process-wide structured log handler with set_log_callback; it can run on the native reader thread, so keep it non-blocking and do not call client lifecycle methods from the handler.

from mysql_event_stream import LogLevel, set_log_callback

set_log_callback(lambda level, message: print(level.name, message), LogLevel.WARN)

Loading a specific native library

Set MES_LIB_PATH=/absolute/path/to/libmes.so (or .dylib) before import, or pass lib_path= to CdcEngine, BinlogClient, or set_log_callback() to select the libmes instance to use.

Event Format

Each ChangeEvent contains the event type, database/table name, binlog position, and row data as a plain dict keyed by column name:

ChangeEvent(
    type=EventType.UPDATE,
    database="mydb",
    table="users",
    before={"id": 1, "name": "Alice", "score": 42},
    after={"id": 1, "name": "Alice", "score": 100},
    timestamp=1773584164,
    position=BinlogPosition(file="mysql-bin.000003", offset=3611),
    names_resolved=True,
)

Lifecycle

BinlogClient() only allocates the native handle; call connect() explicitly, then start() before polling. Prefer with BinlogClient(...) as client: so close() runs on every exit path. close() is idempotent: it stops a pending poll, waits for native access to finish, then disconnects and destroys the handle. Calls to poll() are serialized by the binding.

Table filtering

CdcStream(include_tables=["mydb.audit_*"]) and the lower-level engine filters accept exact, case-sensitive database.table or bare table names. A trailing * is a prefix wildcard; other * characters are literal. If include filters see TABLE_MAP events but none matches, the configured native log callback receives one include_filter_matched_nothing WARN on reset or close.

Thread Safety

CdcEngine instances are single-owner objects. Do not call feed(), next_event(), reset(), or filter/configuration methods concurrently on the same engine instance. Use one engine per thread/task or serialize access externally.

CdcStream uses an internal reader thread through the native binlog client. Iteration and connection lifecycle operations should be owned by one task. Cancellation should go through the stream/client stop path instead of calling other lifecycle methods concurrently.

Features

  • Native performance — C++ core with ctypes FFI
  • Zero native dependencies — No libmysqlclient required; only OpenSSL
  • Streaming — Process events incrementally as bytes arrive
  • MySQL 8.4+ — Supports LTS and Innovation releases
  • MariaDB 10.11+ — Auto-detects flavor and handles MariaDB binlog protocol (GTID events type 162, ANNOTATE_ROWS SQL in ChangeEvent.source_sql, slave capability negotiation)
  • GTID support — Native BinlogClient with GTID-based replication (MySQL uuid:gno and MariaDB domain-server-seq formats)
  • Row-level events — Full before/after column values for INSERT, UPDATE, DELETE
  • VECTOR type — Native support for MySQL 9.0+ VECTOR columns (decoded as raw bytes)
  • Column names — Automatic resolution with binlog_row_metadata=FULL or a metadata connection that has SELECT
  • SSL/TLS — Full SSL/TLS support for secure MySQL connections
  • Backpressure — Internal reader thread with bounded event queue (default 10,000)
  • Auto-reconnection — Automatic reconnection with jittered linear backoff on connection loss

Server Requirements

MySQL:

  • Version: 8.4+
  • GTID mode enabled (for BinlogClient)
  • Replication privileges: REPLICATION SLAVE, REPLICATION CLIENT
  • For schema-derived column names, set binlog_row_metadata=FULL or also grant SELECT. Metadata queries use a separate connection with the same credentials.

MariaDB:

  • Version: 10.11+ (tested against 10.11 and 11.4)
  • GTID replication enabled (log_bin in ROW format)
  • Replication privileges: REPLICATION SLAVE, REPLICATION CLIENT
  • For schema-derived column names, set binlog_row_metadata=FULL or also grant SELECT. Metadata queries use a separate connection with the same credentials.

MySQL binlog configuration

The connection validator requires the following MySQL settings. Copy this into your my.cnf (or its included configuration file) and restart MySQL after changing it:

[mysqld]
log_bin=ON
gtid_mode=ON
binlog_format=ROW
binlog_row_image=FULL
binlog_transaction_compression=OFF
binlog_row_value_options=""

binlog_row_value_options must not contain PARTIAL_JSON. MariaDB is checked for the equivalent required row format and rejects log_bin_compress=ON.

License

Apache-2.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

mysql_event_stream-1.6.1-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (3.2 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

mysql_event_stream-1.6.1-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl (3.6 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARM64

mysql_event_stream-1.6.1-py3-none-macosx_15_0_x86_64.whl (2.4 MB view details)

Uploaded Python 3macOS 15.0+ x86-64

mysql_event_stream-1.6.1-py3-none-macosx_15_0_arm64.whl (2.7 MB view details)

Uploaded Python 3macOS 15.0+ ARM64

File details

Details for the file mysql_event_stream-1.6.1-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for mysql_event_stream-1.6.1-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 6584446f87958f6d1ceb6af63168a2b27993dbebc55595db0a90a4772f06cc0e
MD5 853b522549c5208102069d94b9caff61
BLAKE2b-256 6031a2211429b38f65b90ee19b5e0d93ace185128ecd69422282326969242f49

See more details on using hashes here.

Provenance

The following attestation bundles were made for mysql_event_stream-1.6.1-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl:

Publisher: publish.yml on libraz/mysql-event-stream

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mysql_event_stream-1.6.1-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl.

File metadata

File hashes

Hashes for mysql_event_stream-1.6.1-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Algorithm Hash digest
SHA256 51447c7f848249a89363f89f4f5b7fb33dd649d7e84f17a0a6920ad009698f83
MD5 376ab7d0e737437cba02ba3c778bb558
BLAKE2b-256 b959b4c824c9d391e81ee94821924f88ab9d531faa914b1a85d25abdd692008a

See more details on using hashes here.

Provenance

The following attestation bundles were made for mysql_event_stream-1.6.1-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl:

Publisher: publish.yml on libraz/mysql-event-stream

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mysql_event_stream-1.6.1-py3-none-macosx_15_0_x86_64.whl.

File metadata

File hashes

Hashes for mysql_event_stream-1.6.1-py3-none-macosx_15_0_x86_64.whl
Algorithm Hash digest
SHA256 15fa90be6ff426017362f0f379e31c3a46deaf50eb57b2a01ba390878d2b1c0f
MD5 061704507189998c5b97b063a59e4e3c
BLAKE2b-256 a5b11d16b0cd164890d902d6c2c801b5e55c608cb3e081882c2eb97f73f716a9

See more details on using hashes here.

Provenance

The following attestation bundles were made for mysql_event_stream-1.6.1-py3-none-macosx_15_0_x86_64.whl:

Publisher: publish.yml on libraz/mysql-event-stream

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mysql_event_stream-1.6.1-py3-none-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for mysql_event_stream-1.6.1-py3-none-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 1b102f99d837cb1aae639ea5d7f639cef08fb74a067d3f7d3c25f29139845065
MD5 561ded446cb631f03bffeb20c5bc4a84
BLAKE2b-256 02d2dcf6b9c40cc2acce408913a00aae5fdd8d4043d5e2645eebc31c35e3bfc7

See more details on using hashes here.

Provenance

The following attestation bundles were made for mysql_event_stream-1.6.1-py3-none-macosx_15_0_arm64.whl:

Publisher: publish.yml on libraz/mysql-event-stream

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.6.1 This release

4 files

1.6.0

4 files

1.5.0

4 files

1.4.0

4 files

1.3.2

4 files

1.3.1

4 files

1.3.0

4 files

1.2.0

4 files

1.1.0

4 files

1.0.1

4 files

1.0.0

3 files

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