You have a MySQL 8.0 OLTP database doing two hundred writes per second and a downstream warehouse that wants those writes within a minute. The current pipeline is a cron that runs every fifteen minutes, ships a SELECT * WHERE updated_at > ?, and produces a dashboard that’s somewhere between fifteen and thirty minutes stale. The data team has stopped trusting it. The CFO has stopped looking at it. The honest answer is that batch polling against a primary is the wrong tool — what you want is change data capture reading the binlog, streaming row-level events into Kafka, and letting any number of consumers tail them in near-real time. This article stands up that pipeline end-to-end on Ubuntu 22.04: Kafka in KRaft mode (no ZooKeeper), Kafka Connect in distributed mode, the Debezium MySQL connector, Confluent Schema Registry for Avro payloads, and a smoke test that proves a row insert reaches a consumer in under a second.
What’s happening
Debezium is a Kafka Connect source connector. It reads MySQL’s row-based binary log (RBR), parses each row change into a structured event (before / after / op / ts_ms), and writes that event to a Kafka topic named after the source table. Because it reads the binlog and not the table, it doesn’t touch the primary’s working set — the cost on MySQL is the cost of having binlog enabled and one replication user attached. Downstream consumers (a warehouse loader, a search index, an audit log, an event-driven service) tail the topic at their own pace; if a consumer falls behind, Kafka retains the events for as long as you’ve configured retention.ms, so the consumer catches up without anyone touching MySQL.
The pieces that have to be wired correctly:
- MySQL binlog must be in
ROWformat (notSTATEMENT, notMIXED), withbinlog_row_image = FULLso deletes carry the deleted row’s columns, GTID enabled for resumable consumption, and a replication user with the right grants. - Kafka in KRaft mode removes the ZooKeeper dependency; the broker and controller run in the same JVM. For a single-node lab the controller and broker share
node.id=1; for production you scale to three controllers and three brokers. - Kafka Connect runs the Debezium connector as a worker plugin. In distributed mode the workers form a Connect cluster; configs live in three Kafka topics (
connect-configs,connect-offsets,connect-status). Distributed mode is what you want even with one worker — it’s how you scale tomorrow. - Schema Registry keeps Avro schemas out of the messages. The producer (Debezium) registers a schema and writes a 4-byte schema ID + the binary payload; consumers fetch the schema by ID and decode. This is what stops a column rename from corrupting downstream consumers.
How to verify
After the installer runs you check four things in order:
# 1. Kafka is up
/opt/kafka/bin/kafka-broker-api-versions.sh --bootstrap-server localhost:9092 | head
# 2. Schema Registry is up
curl -s http://localhost:8081/subjects | jq
# 3. Connect is up
curl -s http://localhost:8083/ | jq
curl -s http://localhost:8083/connector-plugins | jq '.[] | .class' | grep -i debezium
# 4. The connector is RUNNING (not PAUSED, FAILED, or UNASSIGNED)
curl -s http://localhost:8083/connectors/mysql-source-shop/status | jq
Then insert a row into the source MySQL and confirm it lands on Kafka in under a second:
mysql -uroot -p shop -e "INSERT INTO orders(amount,status) VALUES (99.50,'paid')"
/opt/kafka/bin/kafka-console-consumer.sh \
--bootstrap-server localhost:9092 \
--topic shop.shop.orders --from-beginning --max-messages 1 \
--property print.key=true
If the topic doesn’t exist yet, the connector hasn’t snapshotted that table. If the connector status is FAILED, the most common causes are wrong binlog format, missing GTID, or the replication user lacking REPLICATION SLAVE and REPLICATION CLIENT grants. The status JSON’s trace field tells you which.
The fix
Save the script as /root/setup-debezium-mysql.sh and run it on a fresh Ubuntu 22.04 VM. It installs Java 17, Kafka 3.7 in KRaft mode, Schema Registry, Kafka Connect with the Debezium 2.6 MySQL connector, configures MySQL 8.0 for CDC (binlog + GTID + replication user), and registers the connector via the Connect REST API. The script is opinionated for a single-node lab — for production you’d split Kafka onto three brokers, run two or three Connect workers behind a load balancer, and put TLS + SASL between every hop.
#!/usr/bin/env bash
set -euo pipefail
### Ubuntu 22.04 — Debezium CDC: MySQL 8.0 -> Kafka 3.7 + Schema Registry
### Run as root on a fresh VM. Single-node lab topology.
### Production: scale Kafka to 3 brokers, run 2+ Connect workers, add TLS + SASL.
if [[ "$(id -u)" -ne 0 ]]; then echo "Run as root."; exit 1; fi
# === EDIT ===
HOST_IP="10.20.0.10"
MYSQL_ROOT_PASSWORD="CHANGE_ME_ROOT"
DEBEZIUM_PASSWORD="CHANGE_ME_DBZ"
DATABASE_NAME="shop"
KAFKA_VERSION="3.7.1"
SCALA_VERSION="2.13"
DEBEZIUM_VERSION="2.6.2.Final"
CONFLUENT_VERSION="7.6.1"
# ============
echo "==> Updating base OS + base packages"
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get -y upgrade
apt-get -y install curl wget vim jq tar gzip unzip ca-certificates \
fail2ban ufw chrony net-tools openjdk-17-jre-headless
systemctl enable --now chronyd 2>/dev/null || systemctl enable --now chrony
echo "==> UFW (allow 9092 Kafka, 8083 Connect, 8081 SR, 22 SSH; deny inbound default)"
ufw --force reset
ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp
ufw allow from 10.20.0.0/16 to any port 9092 proto tcp
ufw allow from 10.20.0.0/16 to any port 8081 proto tcp
ufw allow from 10.20.0.0/16 to any port 8083 proto tcp
ufw --force enable
echo "==> fail2ban"
cat > /etc/fail2ban/jail.d/sshd.local <<'FAIL2BAN'
[sshd]
enabled = true
port = ssh
backend = systemd
maxretry = 4
findtime = 10m
bantime = 1h
FAIL2BAN
systemctl enable --now fail2ban
echo "==> System tuning for Kafka"
cat > /etc/sysctl.d/99-kafka.conf <<'SYSCTL'
vm.swappiness = 1
vm.dirty_background_ratio = 5
vm.dirty_ratio = 60
fs.file-max = 2097152
net.core.somaxconn = 4096
net.ipv4.tcp_max_syn_backlog = 8192
SYSCTL
sysctl --system
cat > /etc/security/limits.d/99-kafka.conf <<'LIMITS'
kafka soft nofile 200000
kafka hard nofile 200000
kafka soft nproc 65535
kafka hard nproc 65535
LIMITS
echo "==> Creating service users"
id -u kafka >/dev/null 2>&1 || useradd -r -s /usr/sbin/nologin -d /var/lib/kafka kafka
id -u connect >/dev/null 2>&1 || useradd -r -s /usr/sbin/nologin -d /var/lib/connect connect
echo "==> Installing Kafka ${KAFKA_VERSION}"
mkdir -p /opt /var/lib/kafka /var/log/kafka
cd /opt
if [[ ! -d kafka_${SCALA_VERSION}-${KAFKA_VERSION} ]]; then
curl -fsSLo /tmp/kafka.tgz \
"https://archive.apache.org/dist/kafka/${KAFKA_VERSION}/kafka_${SCALA_VERSION}-${KAFKA_VERSION}.tgz"
tar -xzf /tmp/kafka.tgz -C /opt
fi
ln -sfn /opt/kafka_${SCALA_VERSION}-${KAFKA_VERSION} /opt/kafka
chown -R kafka:kafka /opt/kafka_${SCALA_VERSION}-${KAFKA_VERSION} /var/lib/kafka /var/log/kafka
echo "==> Kafka KRaft config (single-node combined controller+broker)"
mkdir -p /etc/kafka
cat > /etc/kafka/server.properties <<EOF
process.roles=broker,controller
node.id=1
controller.quorum.voters=1@${HOST_IP}:9093
listeners=PLAINTEXT://${HOST_IP}:9092,CONTROLLER://${HOST_IP}:9093
inter.broker.listener.name=PLAINTEXT
advertised.listeners=PLAINTEXT://${HOST_IP}:9092
controller.listener.names=CONTROLLER
listener.security.protocol.map=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
log.dirs=/var/lib/kafka/data
num.partitions=6
default.replication.factor=1
min.insync.replicas=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.retention.hours=168
auto.create.topics.enable=false
EOF
chown -R kafka:kafka /etc/kafka
echo "==> KRaft storage format"
KRAFT_UUID=$(sudo -u kafka /opt/kafka/bin/kafka-storage.sh random-uuid)
sudo -u kafka /opt/kafka/bin/kafka-storage.sh format -t "${KRAFT_UUID}" -c /etc/kafka/server.properties
echo "==> systemd unit: kafka"
cat > /etc/systemd/system/kafka.service <<'KAFKA'
[Unit]
Description=Apache Kafka (KRaft)
After=network.target
[Service]
Type=simple
User=kafka
Group=kafka
Environment=KAFKA_HEAP_OPTS=-Xms2g -Xmx2g
ExecStart=/opt/kafka/bin/kafka-server-start.sh /etc/kafka/server.properties
Restart=on-failure
LimitNOFILE=200000
[Install]
WantedBy=multi-user.target
KAFKA
systemctl daemon-reload
systemctl enable --now kafka
echo "==> Waiting for Kafka"
for i in {1..60}; do
if /opt/kafka/bin/kafka-broker-api-versions.sh --bootstrap-server ${HOST_IP}:9092 >/dev/null 2>&1; then break; fi
sleep 2
done
echo "==> Pre-creating Connect internal topics"
for T in connect-configs connect-offsets connect-status; do
/opt/kafka/bin/kafka-topics.sh --bootstrap-server ${HOST_IP}:9092 \
--create --if-not-exists --topic "$T" \
--partitions 1 --replication-factor 1 \
--config cleanup.policy=compact >/dev/null
done
echo "==> Installing Confluent Schema Registry ${CONFLUENT_VERSION}"
cd /opt
if [[ ! -d confluent-${CONFLUENT_VERSION} ]]; then
curl -fsSLo /tmp/confluent.tar.gz \
"https://packages.confluent.io/archive/${CONFLUENT_VERSION%.*}/confluent-community-${CONFLUENT_VERSION}.tar.gz"
tar -xzf /tmp/confluent.tar.gz -C /opt
fi
ln -sfn /opt/confluent-${CONFLUENT_VERSION} /opt/confluent
chown -R kafka:kafka /opt/confluent-${CONFLUENT_VERSION}
mkdir -p /etc/schema-registry
cat > /etc/schema-registry/schema-registry.properties <<EOF
listeners=http://0.0.0.0:8081
kafkastore.bootstrap.servers=PLAINTEXT://${HOST_IP}:9092
kafkastore.topic=_schemas
schema.compatibility.level=backward
host.name=${HOST_IP}
EOF
chown -R kafka:kafka /etc/schema-registry
cat > /etc/systemd/system/schema-registry.service <<'SR'
[Unit]
Description=Confluent Schema Registry
After=kafka.service
Wants=kafka.service
[Service]
Type=simple
User=kafka
Group=kafka
Environment=SCHEMA_REGISTRY_HEAP_OPTS=-Xms512m -Xmx512m
ExecStart=/opt/confluent/bin/schema-registry-start /etc/schema-registry/schema-registry.properties
Restart=on-failure
LimitNOFILE=200000
[Install]
WantedBy=multi-user.target
SR
systemctl daemon-reload
systemctl enable --now schema-registry
echo "==> Waiting for Schema Registry"
for i in {1..60}; do
if curl -fs http://${HOST_IP}:8081/subjects >/dev/null 2>&1; then break; fi
sleep 2
done
echo "==> Installing Debezium MySQL connector ${DEBEZIUM_VERSION}"
mkdir -p /opt/connect-plugins/debezium-mysql
cd /opt/connect-plugins/debezium-mysql
if [[ ! -f debezium-connector-mysql-${DEBEZIUM_VERSION}-plugin.tar.gz ]]; then
curl -fsSLo debezium-connector-mysql-${DEBEZIUM_VERSION}-plugin.tar.gz \
"https://repo1.maven.org/maven2/io/debezium/debezium-connector-mysql/${DEBEZIUM_VERSION}/debezium-connector-mysql-${DEBEZIUM_VERSION}-plugin.tar.gz"
tar -xzf debezium-connector-mysql-${DEBEZIUM_VERSION}-plugin.tar.gz --strip-components=1
fi
chown -R kafka:kafka /opt/connect-plugins
echo "==> Kafka Connect distributed config"
mkdir -p /etc/connect /var/log/connect
cat > /etc/connect/connect-distributed.properties <<EOF
bootstrap.servers=${HOST_IP}:9092
group.id=connect-cluster
key.converter=io.confluent.connect.avro.AvroConverter
value.converter=io.confluent.connect.avro.AvroConverter
key.converter.schema.registry.url=http://${HOST_IP}:8081
value.converter.schema.registry.url=http://${HOST_IP}:8081
internal.key.converter=org.apache.kafka.connect.json.JsonConverter
internal.value.converter=org.apache.kafka.connect.json.JsonConverter
internal.key.converter.schemas.enable=false
internal.value.converter.schemas.enable=false
offset.storage.topic=connect-offsets
config.storage.topic=connect-configs
status.storage.topic=connect-status
offset.storage.replication.factor=1
config.storage.replication.factor=1
status.storage.replication.factor=1
offset.flush.interval.ms=10000
plugin.path=/opt/connect-plugins,/opt/confluent/share/java
rest.host.name=0.0.0.0
rest.port=8083
rest.advertised.host.name=${HOST_IP}
EOF
chown -R kafka:kafka /etc/connect /var/log/connect
cat > /etc/systemd/system/kafka-connect.service <<'CONN'
[Unit]
Description=Kafka Connect (distributed)
After=schema-registry.service
Wants=schema-registry.service
[Service]
Type=simple
User=kafka
Group=kafka
Environment=KAFKA_HEAP_OPTS=-Xms1g -Xmx2g
Environment=KAFKA_OPTS=-Dlog4j.configuration=file:/opt/kafka/config/connect-log4j.properties
ExecStart=/opt/kafka/bin/connect-distributed.sh /etc/connect/connect-distributed.properties
Restart=on-failure
LimitNOFILE=200000
[Install]
WantedBy=multi-user.target
CONN
systemctl daemon-reload
systemctl enable --now kafka-connect
echo "==> Waiting for Connect REST API"
for i in {1..60}; do
if curl -fs http://${HOST_IP}:8083/ >/dev/null 2>&1; then break; fi
sleep 2
done
echo "==> Installing MySQL 8.0 (source DB)"
DEBIAN_FRONTEND=noninteractive apt-get -y install mysql-server-8.0
cat > /etc/mysql/mysql.conf.d/cdc.cnf <<EOF
[mysqld]
bind-address = 0.0.0.0
server-id = 1
log_bin = /var/log/mysql/mysql-bin
binlog_format = ROW
binlog_row_image = FULL
expire_logs_days = 7
max_binlog_size = 256M
gtid_mode = ON
enforce_gtid_consistency = ON
EOF
mkdir -p /var/log/mysql
chown mysql:mysql /var/log/mysql
systemctl restart mysql
mysql -uroot <<SQL
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '${MYSQL_ROOT_PASSWORD}';
CREATE USER IF NOT EXISTS 'debezium'@'%' IDENTIFIED BY '${DEBEZIUM_PASSWORD}';
GRANT SELECT, RELOAD, SHOW DATABASES, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'debezium'@'%';
FLUSH PRIVILEGES;
CREATE DATABASE IF NOT EXISTS ${DATABASE_NAME};
USE ${DATABASE_NAME};
CREATE TABLE IF NOT EXISTS orders (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
amount DECIMAL(10,2) NOT NULL,
status VARCHAR(32) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
SQL
echo "==> Registering Debezium MySQL connector"
cat > /tmp/connector.json <<EOF
{
"name": "mysql-source-${DATABASE_NAME}",
"config": {
"connector.class": "io.debezium.connector.mysql.MySqlConnector",
"tasks.max": "1",
"database.hostname": "${HOST_IP}",
"database.port": "3306",
"database.user": "debezium",
"database.password": "${DEBEZIUM_PASSWORD}",
"database.server.id": "184054",
"topic.prefix": "shop",
"database.include.list": "${DATABASE_NAME}",
"table.include.list": "${DATABASE_NAME}.orders",
"schema.history.internal.kafka.bootstrap.servers": "${HOST_IP}:9092",
"schema.history.internal.kafka.topic": "schema-history.${DATABASE_NAME}",
"include.schema.changes": "false",
"snapshot.mode": "initial",
"decimal.handling.mode": "string",
"time.precision.mode": "connect",
"key.converter": "io.confluent.connect.avro.AvroConverter",
"value.converter": "io.confluent.connect.avro.AvroConverter",
"key.converter.schema.registry.url": "http://${HOST_IP}:8081",
"value.converter.schema.registry.url": "http://${HOST_IP}:8081"
}
}
EOF
curl -fs -X POST -H 'Content-Type: application/json' \
--data @/tmp/connector.json \
http://${HOST_IP}:8083/connectors | jq
echo
echo "========================================"
echo "DONE — smoke test:"
echo "========================================"
echo
echo "1) Listeners:"
ss -lntp | grep -E ':(3306|8081|8083|9092|9093)\b' || true
echo
echo "2) Connector status:"
curl -fs http://${HOST_IP}:8083/connectors/mysql-source-${DATABASE_NAME}/status | jq
echo
echo "3) Insert a row, watch it land:"
echo " mysql -uroot -p${MYSQL_ROOT_PASSWORD} ${DATABASE_NAME} -e \"INSERT INTO orders(amount,status) VALUES (12.34,'paid');\""
echo " /opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server ${HOST_IP}:9092 --topic shop.${DATABASE_NAME}.orders --from-beginning --max-messages 1"
echo
Make it executable and run:
chmod 700 /root/setup-debezium-mysql.sh
/root/setup-debezium-mysql.sh
Once the script finishes, the smoke-test sequence at the bottom is what proves the pipeline works end-to-end. If the INSERT doesn’t show up on the consumer within a second, the connector is the place to look first — curl http://host:8083/connectors/mysql-source-shop/status will tell you whether the task is RUNNING or stuck. A common production gap is forgetting to set database.server.id to a value that doesn’t collide with any MySQL replica already attached to the source; Debezium acts as a replica, so it needs its own server ID. For replicated topologies, point Debezium at a dedicated read replica instead of the primary — the load is small but it’s still load you don’t need on the writer.
For CDC pipelines we operate end-to-end with retention tuning, schema-evolution checks, and 24/7 Sev-1 escalation, see Managed Cloud Ops and the monitoring + on-call plane — or book a scoping call if you want us to migrate from your existing batch pipeline.