Jarwis.ClusterOps

Blog

MySQL replication stopped: reading IO and SQL thread errors and repairing a replica

Jarwis Team · · 5 min read · MySQL, MariaDB, Replication

A replica that stops replicating rarely makes noise on its own. Reads keep working, the server stays up, and the data quietly gets older. The first sign is often a report that shows yesterday's numbers, or a failover that promotes a replica that is hours behind.

This post covers how to read a stopped replica, how to tell an IO thread problem from an SQL thread problem, and how to repair each one without making things worse.

Two threads, two kinds of failure

Classic MySQL and MariaDB replication runs two threads on the replica:

  • The IO thread connects to the source, reads binary log events and writes them to the local relay log.
  • The SQL thread (the applier) reads the relay log and applies the events to the replica's data.

They fail for very different reasons. An IO thread failure is almost always about connectivity, credentials or a binary log that no longer exists on the source. An SQL thread failure is about the data itself: the replica cannot apply an event because its data does not match what the source expected.

Read the status first

On MySQL 8.0.22 and later:

SHOW REPLICA STATUS\G

On older MySQL and on MariaDB, SHOW SLAVE STATUS\G gives the same information (MariaDB 10.5+ also accepts SHOW REPLICA STATUS). The fields that matter:

  • Replica_IO_Running / Slave_IO_Running: Yes, No or Connecting.
  • Replica_SQL_Running / Slave_SQL_Running: Yes or No.
  • Last_IO_Errno and Last_IO_Error: why the IO thread stopped.
  • Last_SQL_Errno and Last_SQL_Error: why the SQL thread stopped.
  • Seconds_Behind_Source / Seconds_Behind_Master: shows NULL when the SQL thread is not running, which is not the same as zero.
  • Retrieved_Gtid_Set and Executed_Gtid_Set: what was received versus what was applied, when GTIDs are on.

On MySQL 8.0 the Performance Schema adds per-worker detail, which helps with multi-threaded replicas:

SELECT * FROM performance_schema.replication_applier_status_by_worker\G
SELECT * FROM performance_schema.replication_connection_status\G

IO thread errors

Stuck on Connecting. The replica cannot reach the source or cannot log in. Check from the replica host that the port is open, that the replication user exists on the source with the REPLICATION SLAVE privilege, and that the password has not been rotated on one side only. With caching_sha2_password on MySQL 8, a replica without TLS also needs GET_SOURCE_PUBLIC_KEY=1 in its replication settings.

Error 1236, could not find first log file. The replica asked for a binary log the source has already purged. This usually means the replica was down or stopped for longer than binlog_expire_logs_seconds (or expire_logs_days) allows. There is no clean fix: the events are gone. The replica has to be rebuilt from a fresh backup of the source. To keep it from happening again, raise binary log retention on the source above the longest outage you expect a replica to survive, and alert on stopped replicas within minutes, not days.

Duplicate server IDs or UUIDs. Two replicas with the same server_id, or a cloned host that kept the same auto.cnf UUID, will keep kicking each other off the source. Give every server a unique server_id and remove auto.cnf on a cloned machine before its first start.

SQL thread errors

Error 1062, duplicate entry. The replica already has a row the source is trying to insert. Error 1032, can't find record. The source is updating or deleting a row the replica does not have. Both mean the replica's data has drifted from the source. Common causes are writes made directly on the replica, a replica built from an inconsistent backup, or a crash with non-crash-safe replication settings.

The tempting fix is to skip the event:

-- Without GTIDs
STOP REPLICA;
SET GLOBAL sql_replica_skip_counter = 1;  -- sql_slave_skip_counter on older versions and MariaDB
START REPLICA;

With GTIDs you inject an empty transaction for the failing GTID instead. Either way, skipping does not repair anything. It hides one symptom of a replica that no longer matches its source, and the next mismatch is usually close behind. Skip only when you understand exactly which row is affected and you will fix it by hand, and treat it as a reason to rebuild or re-verify the replica soon.

A few habits prevent most SQL thread failures:

  • Set read_only = ON and super_read_only = ON on every replica, so application writes cannot land there by accident.
  • Use binlog_format = ROW.
  • Keep crash-safe replication on (relay_log_recovery = ON; on MySQL 8 the repository tables are crash-safe by default).
  • Build replicas from a physical backup taken with a known GTID or binary log position, not from a copy of a running data directory.

When to rebuild instead of repair

Rebuild the replica from a fresh backup when the source has purged the binary logs it needs, when more than a handful of rows disagree, or when you cannot say where the drift came from. A rebuilt replica is a known state. A replica that has had five events skipped is not, and it is the one that gets promoted during the next failover.

How this looks in Jarwis ClusterOps

In Jarwis you do not need to write a rule to catch a stopped MySQL or MariaDB replica. The health monitor watches the IO and SQL threads, and if either one has been stopped for about 60 seconds it opens a critical replication alarm. The alarm clears on its own when replication resumes. Alarms are delivered to the notification channels you configure: email over SMTP, Slack, PagerDuty or a generic webhook. Ack an alarm to show that someone is on it.

On Pro, the alarm also shows an Autopilot button. Prepare plan for approval builds a step-by-step plan to repair the replica and sends it to the Approvals page. Nothing runs until an admin reviews and approves it, so a repair never happens behind your back. Pro also adds an AI explanation of the likely root cause and the steps to fix it.

The same High Availability page shows replica roles and lag, and a node you have explicitly marked Stopped is excluded from automatic recovery, so planned maintenance on a replica does not trigger unwanted actions. Jarwis runs as a hosted service, Jarwis Cloud, and you install only an outbound agent on your database servers.

Lite is free up to three nodes, and the stopped-replication alarm and notification channels work there too. Details are in the high availability and alerting docs, and you can look around a read-only demo at demo.jarwis.com.