Skip to content
Latchkey

MySQL "Lost connection to MySQL server during query" in CI

MySQL accepted the connection and started a query, then the link died before the result came back. In CI the usual causes are the server being OOM-killed, a wait/timeout firing, or a packet larger than max_allowed_packet.

What this error means

A query fails partway with "ERROR 2013 (HY000): Lost connection to MySQL server during query" or "Lost connection to MySQL server at 'reading initial communication packet'".

Terminal
ERROR 2013 (HY000): Lost connection to MySQL server during query

Common causes

The MySQL container was OOM-killed

A low memory limit on the service container lets the kernel kill mysqld mid-query, dropping every connection. Check for exit code 137 on the container.

A large statement exceeds max_allowed_packet

Loading a big fixture or dump in one statement can exceed the server packet limit, which closes the connection.

How to fix it

Give the container more memory

Raise the memory available to the MySQL service so it is not OOM-killed during heavier tests.

docker-compose.yml
# docker-compose: bump the limit
services:
  mysql:
    image: mysql:8.0
    deploy:
      resources:
        limits:
          memory: 1g

Raise max_allowed_packet for large loads

Increase the packet limit when importing large fixtures or dumps.

Terminal
mysql --max_allowed_packet=256M app_test < fixtures.sql

How to prevent it

  • Allocate enough memory to the DB service for your workload.
  • Split very large imports into batches under the packet limit.
  • Watch for container exit code 137, which signals an OOM kill.

Frequently asked questions

What causes "MySQL "Lost connection during query""?
A low memory limit on the service container lets the kernel kill mysqld mid-query, dropping every connection. Check for exit code 137 on the container.
How do I fix MySQL "Lost connection during query"?
Raise the memory available to the MySQL service so it is not OOM-killed during heavier tests.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card