Skip to main content

Overview

The MySQL CDC streaming source captures real-time changes from MySQL binary logs and processes them into structured records. It provides comprehensive CDC capabilities with automatic schema evolution tracking and primary key detection.

Real-time CDC

Captures changes as they happen in MySQL

Schema Evolution

Tracks table structure changes automatically

Primary Key Detection

Automatically identifies and extracts primary key columns

Error Recovery

Handles server ID conflicts and binlog file issues

Configuration

Required Parameters

Optional Parameters

SSL Configuration

Record Formats

Standard Event Format

When return_db_records_only: false:

DB Records Only Format

When return_db_records_only: true:
Note: The _mage_* timestamp columns are returned as datetime objects (ISO 8601 format strings when serialized) in UTC timezone, not Unix timestamps. This ensures proper type handling when writing to databases.

Event Types

  • WriteRowsEvent (op: "c"): INSERT operations
  • UpdateRowsEvent (op: "u"): UPDATE operations with before/after data
  • DeleteRowsEvent (op: "d"): DELETE operations
  • QueryEvent (op: "ddl"): DDL statements (CREATE, ALTER, DROP)
  • Automatically clears table schema cache when DDL occurs
  • Extracts table name from SQL for targeted cache clearing
  • XidEvent (op: "transaction"): Transaction commit events
  • Useful for maintaining transaction boundaries
  • GtidEvent (op: "gtid"): Global Transaction Identifier events
  • RotateEvent (op: "rotate"): Binlog file rotation events
  • HeartbeatLogEvent (op: "heartbeat"): Replication heartbeat events
  • TableMapEvent (op: "table_map"): Table structure metadata
  • IntvarEvent (op: "intvar"): Integer variable changes
  • LoadQueryEvent (op: "begin_load_query"/op: "execute_load_query"): LOAD DATA INFILE events

Primary Key Detection

The source automatically detects and extracts primary key columns:
  1. Schema Discovery: Queries INFORMATION_SCHEMA for table structure
  2. Primary Key Detection: Identifies actual primary key columns from database constraints
  3. Caching: Stores schema and primary key information for performance
  4. Key Extraction: Extracts primary key column names from row events
Note: key_columns is a list of column names (not values) that can be used for deduplication or upsert operations in downstream sinks.

Examples

Prerequisites

MySQL Server Configuration

User Permissions

Troubleshooting

  1. Server ID Conflict: Enable auto_generate_server_id: true
  2. Binlog File Not Found: Check if binlog file exists and is accessible
  3. Permission Denied: Ensure user has replication privileges
  4. Connection Timeout: Increase connect_timeout value
  5. Schema Not Found: Ensure include_table_map_events: true
Enable debug logging to see detailed event processing:
Monitor key metrics:
  • Batch Size: Average events per batch
  • Flush Rate: How often batches are flushed
  • Error Rate: Failed events or connections
  • Lag: Time between event occurrence and processing

Integration with Generic IO Sink

The MySQL CDC source works seamlessly with the Generic IO sink, which provides:
  • Automatic Column Type Mapping: Automatically maps _mage_* timestamp columns to appropriate database types (TIMESTAMP, DATETIME2, DateTime64, etc.) based on the target database
  • Metadata Interpolation: Use metadata values from MySQL CDC events in sink configurations for dynamic routing and table naming

Supported Databases

Generic IO Sink supports the following databases:
  • BigQuery
  • ClickHouse
  • DuckDB
  • MySQL
  • MSSQL
  • Postgres

Metadata Interpolation

You can use metadata values from MySQL CDC events in your sink configuration using Python string formatting syntax:
  • {schema}: Database/schema name from the event
  • {table}: Table name from the event
  • {key_columns}: List of primary key column names (e.g., ["id"] or ["user_id", "tenant_id"])
When using {key_columns} in unique_constraints, it will be automatically converted from a string representation to a list. The format supports both Python-style ("['id']") and JSON-style ('["id"]') array strings.

Example Configurations

How Metadata Interpolation Works

  1. Message Grouping: Messages are automatically grouped by their interpolated config values. For example, if you use table_name: "{schema}_{table}", messages from mydb.users will be grouped together and written to mydb_users table.
  2. Key Columns Interpolation: When using {key_columns} in unique_constraints, the sink automatically:
    • Converts the list to a string representation during interpolation
    • Parses it back to a list (supports both "['id']" and '["id"]' formats)
    • Uses it for upsert operations based on unique_conflict_method
Example: If you have a table users with primary key id, and you configure unique_constraints: "{key_columns}", it will automatically use ["id"] for upsert operations.

Best Practices

  1. Use GTID: Enable GTID for reliable resume capabilities
  2. Filter Events: Disable unnecessary event types for performance
  3. Monitor Resources: Watch memory and CPU usage
  4. Test Resume: Verify checkpoint functionality
  5. Secure Connections: Use SSL in production
  6. Regular Backups: Backup checkpoint files
  7. Schema Validation: Test with schema changes
  8. Use Generic IO Sink: Leverage automatic type mapping and metadata interpolation for flexible data routing
  9. Metadata Interpolation: Use {schema}, {table}, and {key_columns} for dynamic table routing and upsert configuration

Timestamp Handling

The MySQL CDC source automatically converts Unix timestamps to datetime objects:
  • _mage_created_at: Set to event timestamp (datetime) for INSERT operations
  • _mage_updated_at: Set to event timestamp (datetime) for UPDATE operations
  • _mage_deleted_at: Set to event timestamp (datetime) for DELETE operations
All timestamps are in UTC timezone and returned as datetime objects, which ensures:
  • Proper type handling in downstream databases
  • Automatic type conversion in Generic IO sink
  • Consistent timezone handling across systems

Limitations

  • MySQL 5.7+: Requires MySQL 5.7 or later
  • ROW Format: Requires binlog_format = ROW
  • Network Dependency: Requires stable network connection
  • Memory Usage: Schema caching uses memory
  • Binlog Retention: Depends on MySQL binlog retention settings
  • Timezone: All timestamps are in UTC timezone