~/drupal/drupal-mysql-error-got-packet-bigger-max-allowed-packet.md

Fix Drupal MySQL Error: Got a Packet Bigger Than max_allowed_packet

2 min readApril 04, 2020by Gorav Singal#drupal#mysql#mariadb
// TL;DR

Increase the max_allowed_packet setting in your MySQL/MariaDB configuration to resolve the communication link failure caused by large query packets.

Fix Drupal MySQL Error: Got a Packet Bigger Than max_allowed_packet

Introduction

While this topic may applicable to all mysql/mariadb users who faced this issue. The exact error is:

Uncaught PHP Exception Drupal\\Core\\Database\\DatabaseExceptionWrapper: 
"SQLSTATE[08S01]: Communication link failure: 1153 Got a packet bigger than 
'max_allowed_packet' bytes: INSERT INTO {key_value_expire} (name, collection, value, expire) VALUES

.
.
.

Drupal\\Core\\Database\\DatabaseExceptionWrapper: SQLSTATE[HY000]: 
General error: 2006 MySQL server has gone away: UPDATE {users_field_data} 
SET access=:db_update_placeholder_0\nWHERE uid = :db_condition_placeholder_0; Array\n(\n    
[:db_update_placeholder_0] => 1586010179\n    [:db_condition_placeholder_0] => 1\n)\n in 
core/modules/user/src/UserStorage.php on line 59 #0 core/lib/Drupal/Core/Database/Connection.php(659): 
Drupal\\Core\\Database\\Connection->handleQueryException(Object(PDOException),

When I got this error

This error comes generally when you are doing a heavy insert operation on db, and your db instance is not configired to handle such big chunk of data in one query.

In drupal when I added a field in a content type where a field is allowed multiple number of times. In drupal, it is configured with “Unlimited” occurrence. And on the website, when we click on “Add More” for that particular field. This error comes up. Note that on the web, the http status will be 500. You need to check your drupal logs at backend.

The Solution

You should first check this particular variable in your db configuration. Run following query:

SHOW VARIABLES LIKE 'max_allowed_packet'; 

The result is in bytes. The default value should be around 1MB. You should consider increasing it to at least 10 MB.

Open your mysql configuration file. In centos, its at

/etc/my.cnf

Edit the file, and under section: [mysqld]. See example below:

[mysqld]
datadir=/var/lib/mysql
.
.
.

max_allowed_packet=16M

In above example, it is set to 16MB in size. Note, there might be other sections in your config file like [mysqld_safe]. It should be changed under [mysqld] section.

After this change, you need to restart mysql server.

service mysqld restart

OR 

service mariadb restart

This will solve the problem.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Python SMTP Email Code - Sender Address Rejected - Not Owned By User

Python SMTP Email Code - Sender Address Rejected - Not Owned By User

Introduction In a normal email sending code from python, I’m getting following…

Drupal Mysql Query to Fetch User Field Details and its Alias

Drupal Mysql Query to Fetch User Field Details and its Alias

Introduction In this posr, we will see how to prepare mysql query to fetch user…

Drupal DB Query Code to Fetch Active Users and Accessed Website Within last One Year

Drupal DB Query Code to Fetch Active Users and Accessed Website Within last One Year

Introduction Here, we will see the drupal code to fetch all the active users…

Drupal 8 - How to Theme Form and its Fields with reordering fields

Drupal 8 - How to Theme Form and its Fields with reordering fields

Introduction In this post, we will see how to theme form and its fields…

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Introduction I needed a report page, where I wanted to have some information…

Drupal 7 - Code for Exporting all your content nodes in json files

Drupal 7 - Code for Exporting all your content nodes in json files

Introduction When I migrated all of my drupal-7 website to drupal-8, I wrote…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…