How to increase max allowed packet size on mariaDB (mysql) on CentOs
Enjoying this content? Subscribe to the Channel!
** Fix Database Errors: How to Increase the max_allowed_packet Size in MariaDB on CentOS
Stop Database Errors in Their Tracks
If you run popular web applications like WordPress, Drupal, or Moodle, you’ve probably encountered those frustrating moments when a large upload, import, or—worst of all—a backup attempt fails spectacularly.
The symptom often appears as a vague “Error writing to the database” or the infamous “MySQL has gone away” message. Sound familiar?
The good news is that this problem is usually caused by one small, easily adjustable setting within your database configuration: the max_allowed_packet size. This setting defines the largest single packet that MariaDB (or MySQL) can handle. If you try to send data larger than this limit—say, uploading a huge video via Moodle or running a comprehensive backup—the connection instantly drops.
In this guide, we’re going to walk through the quick, simple process of increasing the max_allowed_packet size on your CentOS server running MariaDB. Let’s get that data flowing smoothly!
Why Adjust the max_allowed_packet Size?
Before we dive into the commands, let’s quickly look at why this setting matters, especially for content-heavy sites:
- Large Uploads: If you handle big files (images, PDFs, videos) that are stored directly in the database (or if metadata transfer is large), a small packet size will reject the upload.
- Backups: Comprehensive database backups require transferring massive amounts of data in a single process. A small packet size frequently causes the backup process to fail halfway through, leading to the “MySQL has gone away” error.
- Complex Queries: Highly complex queries that return huge result sets can also hit this limit.
By default, this size is often set conservatively (like 16MB). We’ll be boosting ours significantly to prevent future issues.
Step 1: Accessing the MariaDB Configuration File
We need to edit the main configuration file for MariaDB, typically located at /etc/my.cnf. For this tutorial, we will use the vi editor, but feel free to substitute nano or your preferred editor.
- Log into your CentOS server via SSH.
- Execute the following command to open the configuration file:
sudo vi /etc/my.cnf
- If prompted, enter your root or sudo password.
Step 2: Modifying the max_allowed_packet Setting
Once the file is open, we need to locate the [mysqld] section. This section controls the settings for the MariaDB server daemon itself.
- Navigate through the file until you find the
[mysqld]heading. - Press
ito enter “insert mode” in thevieditor. - Under the
[mysqld]section, add or modify themax_allowed_packetline. We are setting it to 500 Megabytes, which should be sufficient for even very large operations:
[mysqld]
# Add this line if it doesn't exist, or modify it if it does
max_allowed_packet=500M
Pro Tip: Make sure to use the capital M for Megabytes. Using a value like
500000000(bytes) is also acceptable, but500Mis cleaner.
- Once you have added the line, press
Escto exit insert mode. - Type
:wq!and press Enter to write (save) the changes and quit the editor.
Step 3: Restarting the MariaDB Service
For these new settings to take effect, we must restart the MariaDB service. This is crucial—the database will continue running with the old settings until this step is complete.
Execute the following command:
sudo service mariadb restart
You should see confirmation that the service has successfully restarted.
Step 4: Verification and Testing
With the service restarted, your MariaDB instance is now operating with a maximum packet size of 500MB, eliminating the common database error during large transfers.
To verify the fix, simply return to the web application that was causing the error:
- Moodle: Try running your course backup again. If the process starts and completes without the “Error writing to the database,” you are all set!
- WordPress/Drupal: Try executing the import or upload that previously failed.
By taking just a few minutes to adjust this essential configuration setting, you’ve ensured your database can handle large payloads without crashing the connection.
Ready to Tackle Your Next Tech Challenge?
That’s all there is to it! Fixing the max_allowed_packet size is one of the most useful troubleshooting tips for any site running on MariaDB or MySQL. Now you can focus on creating great content without worrying about database connection limits.
If this tutorial helped you fix that stubborn “MySQL has gone away” error, please let us know in the comments below!
Don’t forget to Like this post, Subscribe to Darren’s Tech Tutorials for more clear and accessible guides, and hit that notification bell so you never miss a tip! Thanks for watching!