Common SQL Errors

MySQL Error 2006: CR_SERVER_GONE_ERROR – How to Fix and Prevent

Galaxy Team
August 5, 2025

The client tried to read from or write to MySQL, but the server had already closed the TCP connection.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL error 2006?

MySQL Error 2006: CR_SERVER_GONE_ERROR happens when the server closes a client connection or the network drops. Check server logs, raise wait_timeout and max_allowed_packet, then reconnect to resolve the issue.

Error Highlights

Typical Error Message

MySQL server has gone away

Error Type

Connection Error

Language

MySQL

Symbol

CR_SERVER_GONE_ERROR

Error Code

2006

SQL State

Explanation

Table of Contents

What is MySQL Error 2006: CR_SERVER_GONE_ERROR?

MySQL error 2006 appears when a client issues a request but the TCP socket to the MySQL server is already closed. The client library detects the broken pipe and throws CR_SERVER_GONE_ERROR with the message "MySQL server has gone away."

The disconnect usually results from server timeouts, oversized packets, crashes, or network interruptions.

Fixing the root cause restores reliable connectivity and prevents data-loss scenarios.

What Causes This Error?

Idle sessions that exceed wait_timeout or interactive_timeout are silently closed by mysqld. The next statement sent by the client fails with error 2006.

Queries or BLOB transfers bigger than max_allowed_packet make the server abort the session to avoid memory exhaustion, triggering the error.

Slow queries can hit net_read_timeout or net_write_timeout.

When the client waits too long for a response it assumes the server is gone.

Server restarts, OOM kills, firewall resets, or flaky VPN links break open sockets instantly, so every client sees CR_SERVER_GONE_ERROR on the next operation.

How to Fix MySQL Error 2006

Inspect the MySQL error log for "Aborted connection" or packet-size messages to locate the trigger.

The log shows the user, host, and SQL involved.

Raise wait_timeout, interactive_timeout, net_read_timeout, net_write_timeout, and max_allowed_packet temporarily, then repeat the failing workload to confirm stability.

Refactor long-running statements or batch uploads so they stay within packet limits and finish before timeouts expire.

Enable automatic reconnect in drivers (autoReconnect=true in JDBC, MYSQL_OPT_RECONNECT for C, or autoreconnect=True in Python) so transient drops self-heal.

Common Scenarios and Solutions

A web page that runs a giant SELECT times out and shows "server has gone away." Paginating the query or adding indexes removes the error.

An ETL script fails on a 300 MB INSERT.

Executing SET GLOBAL max_allowed_packet = 1073741824; (1 GB) lets the load finish.

A cron job idles two hours then queries the DB. Adding a SELECT 1 heartbeat every five minutes or reopening the connection resolves the failure.

Best Practices to Avoid This Error

Use connection pooling and close idle handles quickly. Short-lived sessions lower the chance of hitting timeout limits.

Size wait_timeout and max_allowed_packet to real workload patterns, not default values.

Review them after every schema or traffic change.

Monitor Aborted_clients, Aborted_connects, and Threads_connected with Galaxy dashboards or other observability tools, alerting on spikes.

Leverage Galaxy's query history to identify slow, heavy queries and refactor them before they cause disconnects.

Related Errors and Solutions

Error 2013 "Lost connection to MySQL server during query" occurs when the link dies while streaming results.

The fixes mirror error 2006.

Error 2055 "Lost connection to MySQL server at '%s', during handshake" points to SSL or DNS issues in the initial handshake.

Error 1040 "Too many connections" means mysqld refused new clients rather than dropping existing ones. Increase max_connections or use pooling.

.

Common Causes

Idle timeout reached

The server closes connections that sit idle past wait_timeout or interactive_timeout. The next statement fails with error 2006.

Packet size exceeded

A packet larger than max_allowed_packet makes mysqld abort the connection, returning CR_SERVER_GONE_ERROR.

Server crash or restart

Mysqld restarts or crashes close all sockets.

Clients attempt to reuse them and receive the error.

Network interruption or firewall reset

Transient outages, VPN drops, or firewalls that silently drop idle TCP sessions yield error 2006 when data is next exchanged.

.

Related Errors

FAQs

Does increasing wait_timeout always fix error 2006?

It prevents idle disconnects but will not fix packet size issues or server crashes. Diagnose the log before changing the value.

Is max_allowed_packet safe at 1 GB?

A high limit consumes more memory per connection. Set it only when importing large data and reduce it afterward.

How can Galaxy help avoid CR_SERVER_GONE_ERROR?

Galaxy surfaces slow or oversized queries, lets you tune parameters, and provides keepalive snippets, lowering disconnect risk.

Will auto reconnect lose transaction state?

Yes. When the session is recreated, any uncommitted work is rolled back. Commit frequently if you rely on auto reconnect.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo