MySQL Error 2006: Server Has Gone Away (During Import)
Your import dies with 'MySQL server has gone away'. Here's what's happening and how to fix it.
Importing a SQL file and it fails partway through with 'ERROR 2006 (HY000): MySQL server has gone away'. Your import is dead, your database is half-populated.
What this error actually means
MySQL closed the connection. This usually happens because:
- Packet too large — A single query exceeded max_allowed_packet
- Connection timeout — The query took too long (wait_timeout)
- Server crashed — Less common, but possible with huge queries
During imports, it's almost always #1 or #2.
Fix for max_allowed_packet
If you can change MySQL settings:
SET GLOBAL max_allowed_packet=512*1024*1024;Or add to my.cnf and restart MySQL:
[mysqld]
max_allowed_packet=512MFix for wait_timeout
SET GLOBAL wait_timeout=28800;
SET GLOBAL interactive_timeout=28800;This sets 8-hour timeouts. Overkill for most imports, but it won't hurt.
Can't change MySQL settings?
On shared hosting, you probably can't. Your options:
- Split the SQL file into smaller chunks
- Re-export with smaller INSERT statements (--skip-extended-insert)
- Ask your host to import it for you
Recovering from a partial import
If the import died halfway, your database is probably in a bad state. You have two options:
- Drop all tables and start over — Safest. DROP DATABASE, CREATE DATABASE, try again.
- Figure out where it stopped — Harder. You'd need to check which tables have data and resume from there.
For a clean import, starting over with properly split files is usually less painful than debugging a partial import.
Frequently Asked Questions
What causes 'MySQL server has gone away' during import?
Usually a query that's too large (exceeds max_allowed_packet) or takes too long (exceeds wait_timeout). MySQL closes the connection when limits are exceeded.
How do I increase MySQL packet size?
Run SET GLOBAL max_allowed_packet=512*1024*1024; in MySQL, or add max_allowed_packet=512M to my.cnf under [mysqld]. You need server access to change these settings.
Can I resume a failed MySQL import?
MySQL doesn't have built-in resume. You'd need to manually find where it stopped and import remaining data. For large files, it's often easier to drop everything and start fresh with split files.