Skip to main content

CDC for MySQL

MySQL CDC integrations capture row-level changes from MySQL tables in real time using Debezium-based Change Data Capture. Unlike PostgreSQL's logical replication, MySQL CDC streams changes directly from the server's binary log (binlog). Use them for data synchronization, audit logging, and event-driven workflows that must react to database inserts, updates, and deletes without polling. This page covers creating the integration, configuring the service and listener, and adding event handlers for insert, update, delete and read events.

Prerequisites

Binary logging must be enabled on the MySQL server (log_bin set to ON) with binlog_format set to ROW and binlog_row_image set to FULL, and the database user used for CDC needs the SELECT, RELOAD, SHOW DATABASES, REPLICATION SLAVE, and REPLICATION CLIENT privileges, before creating this integration. See the CDC connector setup guide for step-by-step instructions.

Create a CDC service for MySQL​

Service configuration​

In the Service Designer, click the Configure icon in the header to open the CDC for MySQL Configuration panel. Select CDC for MySQL in the left panel.

FieldDescription
Service ConfigAdvanced CDC configuration as a record expression. The tables field sets the table(s) whose change events are routed to this service, using the fully qualified format <database>.<table>. Provide a single table as a string (for example, "mydb.customers"), or multiple tables as a string array (for example, ["mydb.customers", "mydb.orders"]).

Listener configuration​

In the CDC for MySQL Configuration panel, select mysqlCdcListener under Attached Listeners to configure the listener.

A CDC service can be attached to only one listener.

FieldDescriptionDefault
NameIdentifier for the listener.mysqlCdcListener
DatabaseDatabase connection configuration as a record expression with hostname, port, username, password, and includedDatabases fields.Required
Engine NameDebezium engine instance name.ballerina-cdc-connector
Internal Schema StorageSchema history storage configuration.{fileName: "tmp/dbhistory.dat"}
Offset StorageOffset storage configuration for tracking CDC progress.{fileName: "tmp/debezium-offsets.dat"}
Liveness IntervalInterval in seconds for checking CDC listener liveness.60.0
OptionsAdditional connector options as a record expression.{}

Click Save Changes to apply updates.

Server ID and binlog retention

The connector identifies itself to the MySQL server as a replication client using databaseServerId (inside the Database field); a random value is generated if you don't set one, but set an explicit, unique value when multiple connectors read from the same MySQL server. Also make sure binlog files are retained (via binlog_expire_logs_seconds on MySQL 8.0+, or expire_logs_days on older versions) long enough for the connector to catch up after downtime.

Event handlers​

Adding an event handler​

In the Service Designer, click + Add Handler. The Select Handler to Add panel lists onRead, onCreate, onUpdate, onDelete, and onError.

onRead, onCreate, onUpdate, and onDelete each open a Message Handler Configuration panel for the row payload. onError is added directly without additional configuration.

The configuration panel exposes the following fields:

FieldDescription
+ Define Database EntryDefines the record type representing one row of the tracked table. The handler receives this record at runtime with values from the change event.
Advanced Parameters > TableNameAdds a string parameter to the handler that receives the name of the table the change event came from. It does not filter events.

Click Save to add the handler.

Truncate events

MySQL binlogs in row-based format (binlog_format=ROW, the required setting for CDC) do not emit row-level events for TRUNCATE TABLE, because TRUNCATE is a DDL statement rather than a row change. There is no onTruncate handler for MySQL CDC services; use onDelete if your workflow needs to react to row removal instead.

Handler types​

HandlerTriggered whenUse when
onReadA row is read during the initial snapshot of the tableBootstrapping downstream systems with existing data
onCreateA row is inserted into the tracked tableSyncing new records to downstream systems
onUpdateA row is updated in the tracked tablePropagating field changes
onDeleteA row is deleted from the tracked tableRemoving records from downstream systems
onErrorA CDC processing error occursLogging failures and sending alerts

What's next​