How to increase max allowed packet size on mariaDB (mysql) on CentOs

Published: September 11, 2017 (Updated: Sep 11, 2017)

Enjoying this content? Subscribe to the Channel!

The Ultimate Fix for “MySQL Has Gone Away”: Increasing MariaDB’s max_allowed_packet Size on CentOS

Hey Tech Fans, and welcome back to Darren’s Tech Tutorials!

If you’ve ever tried to upload a large file, import a significant database backup, or run an intensive operation on a platform like WordPress, Drupal, or Moodle, only to be slapped with the frustrating and vague error message, “MySQL has gone away,” you know how much time it can waste.

The good news? This isn’t usually a sign of database doom. It often means your database is dropping the connection because the data packet being sent is larger than its defined limit. Specifically, we need to adjust the max_allowed_packet setting in MariaDB.

In this quick, focused tutorial, we’re going to show you exactly how to increase this limit on a CentOS server, guaranteeing smooth sailing for your large uploads and preventing that pesky “gone away” error once and for all!


What is max_allowed_packet and Why Do I Need to Change It?

The max_allowed_packet variable dictates the maximum size (in bytes) of a single packet or message that can be sent to or received from the MariaDB server.

When you install MariaDB (or MySQL), this setting defaults to a relatively small size—often just a few megabytes. While this is fine for standard queries, it becomes a bottleneck when:

  1. Uploading large files through web applications (especially within LMS platforms like Moodle).
  2. Importing database dumps via phpMyAdmin or the command line.
  3. Transferring large BLOB fields (like high-resolution images or videos) across the network.

If the data packet exceeds the current setting, MariaDB simply disconnects, resulting in the dreaded “MySQL has gone away” error, because the server has aborted the transfer.


Step-by-Step Guide: Increasing max_allowed_packet on CentOS

We will be working directly in the command line on your CentOS server. Remember to always run these commands with sudo permissions!

Step 1: Locate and Edit the MariaDB Configuration File

The primary configuration file for MariaDB on CentOS systems is typically /etc/my.cnf. We will use the powerful vi editor, but feel free to use nano if you prefer it.

Open the configuration file using the following command:

sudo vi /etc/my.cnf

Step 2: Set the New max_allowed_packet Value

Once the configuration file is open, you need to navigate to the section designated for the MySQL daemon, which is marked by [mysqld].

If this section already exists, place the new setting directly underneath it. If the section doesn’t exist, create it.

We recommend setting this value high enough to handle virtually any common web application upload, such as 500 Megabytes (500M).

Add or modify the following lines in the configuration file, ensuring it is under the [mysqld] heading:

[mysqld]
max_allowed_packet=500M

Note: While we are using 500M as a robust example, you could also use a smaller value like 64M or 128M if you know your largest typical transfer won’t exceed that limit. For ultimate peace of mind when dealing with large WordPress media libraries or Moodle courses, 500M is a safe choice.

Step 3: Save and Exit the File

In the vi editor:

  1. Press Esc to ensure you are in command mode.
  2. Type :wq (write and quit) and press Enter.

Step 4: Restart the MariaDB Service

For the new configuration setting to take effect, you must restart the MariaDB service. Failing to restart the service means the old settings will still be active, and your large transfers will still fail.

Use the following command to gracefully restart the service:

sudo service mariadb restart

If the restart is successful, you are good to go! Your MariaDB instance now accepts packets up to 500MB, effectively eliminating the “MySQL has gone away” error for large file operations.


Conclusion

It’s amazing how one small configuration change can completely solve a major headache, especially when dealing with critical database operations and platform maintenance on systems like WordPress, Drupal, and Moodle. By simply increasing the max_allowed_packet setting, you’ve stabilized your database and ensured your server can handle the heavy lifting!

If this tutorial helped you get rid of that frustrating upload error, smash that like button! Subscribe to Darren’s Tech Tutorials for more practical, straightforward fixes, and let me know in the comments below what other tricky server configuration issues you want us to tackle next.

Happy coding!