How to fix MySQL lost connection

Published on September 13th, 2022

SimpleBackups founder

Laurent Lemaire

Co-founder, SimpleBackups

When running a long or complex query on MySQL you might encounter the error below:

 Error 2013: Lost connection to MySQL server during query

This error is often faced when running mysqldump on a large database.

In order to fix this error, you will have to update the default MySQL query timeout limits as explained below.

Display MySQL timeout variables

Before updating the timeouts, let's check their current values.

SHOW SESSION VARIABLES LIKE 'wait_timeout';
SHOW SESSION VARIABLES LIKE 'interactive_timeout';
  • wait_timeout : The number of seconds the server waits for activity on a noninteractive connection before closing it.

  • interactive_timeout: The number of seconds the server waits for activity on an interactive connection before closing it.

For each the default value is 28800 (seconds) so 8 hours.

Update MySQL timeout

If your queries require more time than the default values returned in the previous step, you can update them as follow:

SET @@GLOBAL.wait_timeout=57600;
SET @@GLOBAL.interactive_timeout=57600

In this example, we've doubled the timout to 16 hours.



Back to blog

Don't want to maintain backup scripts?

Unlock no-code & optimized backup, for all your projects.

Try SimpleBackups

No credit card required. Free 7-day trial.