Database Intermediate

Troubleshooting: MySQL Server Has Gone Away – Packet Too Large Query Failure on CentOS Stream / Rocky Linux

Resolve the 'MySQL server has gone away packet too large' error on CentOS Stream/Rocky Linux by adjusting max_allowed_packet in your MariaDB/MySQL configuration.

👨‍💻
Senior Systems Architect • Verified in Staging Labs

Resolve the 'MySQL server has gone away packet too large' error on CentOS Stream/Rocky Linux by adjusting max_allowed_packet in your MariaDB/MySQL configuration.

When your application encounters a database operation that involves sending a large amount of data to MySQL or MariaDB, you might hit an abrupt connection closure resulting in the "MySQL server has gone away" error, often accompanied by "packet too large query failure". This guide will walk you through diagnosing and resolving this common issue on CentOS Stream and Rocky Linux environments.

Symptom & Error Signature

Users typically experience an application error, a failed database operation, or a broken page on their website. The exact error message depends on the client library or application framework used, but it generally points to a lost connection or a packet size issue.

You might see errors similar to these in your application logs (e.g., PHP-FPM logs, web server error logs like Nginx/Apache, or custom application logs):

PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away in /var/www/html/app/db.php:123
Stack trace:
#0 /var/www/html/app/db.php(123): PDOStatement->execute()
#1 /var/www/html/app/index.php(45): some_function_insert_large_data()
#2 {main}
  thrown in /var/www/html/app/db.php on line 123

Or directly from a MySQL client or application attempting a large query:

ERROR 2006 (HY000): MySQL server has gone away

Sometimes, more detailed information might be present, explicitly mentioning a packet size:

Got a packet bigger than 'max_allowed_packet' bytes

This specific variant of "MySQL server has gone away" indicates the server refused a query due to its size exceeding a configured limit.

Root Cause Analysis

The "MySQL server has gone away packet too large" error almost invariably points to the max_allowed_packet configuration variable. This variable defines the maximum size of one packet or any generated/intermediate string, or any parameter sent by the mysql_stmt_send_long_data() C API function.

When a client (your application) attempts to send a query, a result set, or a replication event that is larger than the max_allowed_packet value configured on the MySQL/MariaDB server, the server will prematurely close the connection. This prevents the server from allocating excessive memory for a single, potentially malicious or erroneous, request.

Common scenarios leading to this error include:

  • Large INSERT or UPDATE statements: Particularly when dealing with BLOB or TEXT data types storing images, files, or extensive text content.
  • LOAD DATA INFILE operations: Importing large CSV or other data files.
  • Database backups or dumps: Exporting data, especially with INSERT statements that contain many rows.
  • Replication: If a replicated statement exceeds the limit on the replica.
  • Application logic: Poorly optimized queries that bundle too much data into a single statement.

While other factors can cause a generic "MySQL server has gone away" error (like wait_timeout or network issues), the "packet too large" context specifically narrows it down to max_allowed_packet.

Step-by-Step Resolution

The solution involves increasing the max_allowed_packet value in your MariaDB/MySQL server configuration and restarting the service.

1. Determine Current max_allowed_packet Value

First, connect to your MariaDB/MySQL server and check the current max_allowed_packet setting.

mysql -u your_username -p

Enter your password when prompted. Then, within the MySQL/MariaDB client:

SHOW VARIABLES LIKE 'max_allowed_packet';

You will see output similar to this:

+--------------------+---------+
| Variable_name      | Value   |
+--------------------+---------+
| max_allowed_packet | 4194304 |
+--------------------+---------+

The Value is in bytes. 4194304 bytes equals 4MB, which is a common default. If your application sends data larger than this, you'll encounter the error.

2. Locate MariaDB/MySQL Configuration File

On CentOS Stream and Rocky Linux, the main configuration file is typically /etc/my.cnf, but often includes files from the /etc/my.cnf.d/ directory. For MariaDB, a common specific server configuration file is /etc/my.cnf.d/mariadb-server.cnf. For MySQL, it might be /etc/my.cnf.d/mysql-server.cnf or /etc/my.cnf.

To confirm which files are being used, you can check the MariaDB/MySQL documentation or look for include directives in /etc/my.cnf.

grep -r "max_allowed_packet" /etc/my.cnf* 2>/dev/null

This command will show if max_allowed_packet is already defined and where. If it's not found, you'll add it to the most appropriate file. We will aim to modify /etc/my.cnf.d/mariadb-server.cnf (for MariaDB) or /etc/my.cnf (for MySQL) as the primary locations.

3. Edit MariaDB/MySQL Server Configuration

Use a text editor like vi or nano to open the relevant configuration file. For MariaDB on CentOS/Rocky:

sudo vi /etc/my.cnf.d/mariadb-server.cnf

Always back up your configuration files before making changes.

sudo cp /etc/my.cnf.d/mariadb-server.cnf /etc/my.cnf.d/mariadb-server.cnf.bak.$(date +%F)

Find the [mysqld] section within the file. If it doesn't exist, you may need to add it. Add or modify the max_allowed_packet variable under this section.

Increase the value to something suitable for your application. Common values are 64M, 128M, or even 256M. Start with 64M or 128M and adjust upwards if needed.

[mysqld]
# ... other existing configurations ...
max_allowed_packet=128M

The max_allowed_packet value needs to be larger than the largest query or data packet your application sends. Setting it excessively high can potentially allow malicious large packets to consume too much memory, but for legitimate application use, a value like 128MB is often sufficient. Be mindful of your server's available RAM.

Save the file and exit the editor.

4. Restart MariaDB/MySQL Service

For the changes to take effect, you must restart the MariaDB or MySQL service.

For MariaDB:

sudo systemctl restart mariadb

For MySQL:

sudo systemctl restart mysqld

Restarting the database service will temporarily disconnect all active clients. Schedule this during a maintenance window if possible, especially on production systems.

5. Verify the Change

After restarting the service, connect to your database again and verify that the max_allowed_packet value has been updated:

mysql -u your_username -p

Then:

SHOW VARIABLES LIKE 'max_allowed_packet';

You should now see your new, increased value:

+--------------------+-----------+
| Variable_name      | Value     |
+--------------------+-----------+
| max_allowed_packet | 134217728 |
+--------------------+-----------+

(134217728 bytes is 128MB).

6. Consider Client-Side Configuration (If Necessary)

While the server-side max_allowed_packet is the most common culprit for this error, some clients (like the mysql command-line client or specific application libraries) might have their own max_allowed_packet setting.

For the mysql command-line client, you can specify it directly:

mysql --max-allowed-packet=128M -u your_username -p

For PHP applications, php.ini doesn't typically have a max_allowed_packet setting directly. PHP's memory_limit and post_max_size are more relevant for incoming data to the PHP script itself, but the actual database packet size is usually managed by the MySQL client library used by PHP. In rare cases, an application might explicitly try to set the session max_allowed_packet with SET GLOBAL max_allowed_packet=... which would require appropriate permissions.

If the error persists after adjusting the server's max_allowed_packet, you might also want to review application code for any explicit limits being set, or check for other database "gone away" causes like wait_timeout or interactive_timeout if the packet size error message no longer appears. However, for "packet too large", the server-side max_allowed_packet is almost always the definitive fix.

👨‍💻

Johnathon Wheeler

Senior Systems Architect & DevOps Engineer • Austin, TX

Connect on LinkedIn →

Johnathon has over 16 years of hands-on experience designing, debugging, and scaling Linux web hosting stacks, container clusters, and high-availability database architectures. Every guide on ButItWorkedLocal is independently tested against Debian 12, Ubuntu 24.04/22.04 LTS, Rocky Linux, and Docker environments to guarantee reproducibility in production.

🛡️

Our Production Verification Guarantee

Encountering a bug not covered here or running a non-standard kernel configuration? Our solutions are continually refined against real production incidents. Submit an environment trace for our editorial team to replicate.