Best Cosmetic Hospitals Near You

Compare top cosmetic hospitals, aesthetic clinics & beauty treatments by city.

Trusted • Verified • Best-in-Class Care

Explore Best Hospitals

Fixing “Got a packet bigger than ‘max_allowed_packet’ bytes” Error in Linux (LAMPP & Native MySQL)

Absolutely! Below is a complete, combined tutorial guide beginner-friendly tutorial for learners. This tutorial covers both native MySQL on Linux (installed via apt) and MySQL inside LAMPP (XAMPP for Linux


🔎 What Causes This Error?

The error:

SQLSTATE[08S01]: Communication link failure: 1153 Got a packet bigger than 'max_allowed_packet' bytes

This happens when the size of the query or data being sent to MySQL exceeds the allowed packet size configured in MySQL.

This is common when:

  • Importing large SQL files (migrations, seeds).
  • Inserting or updating large JSON data, images, or files in the database.
  • Running large bulk queries.

Step 1: Determine Your MySQL Setup

Option 1 – Native MySQL (installed via apt)

If you installed MySQL directly via:

sudo apt install mysql-server

Your config file is:

/etc/mysql/mysql.conf.d/mysqld.cnf

Option 2 – MySQL in LAMPP (XAMPP for Linux)

If you’re using LAMPP (XAMPP for Linux), your MySQL config file is:

/opt/lampp/etc/my.cnf

Step 2: Edit MySQL Configuration File

For Native MySQL (Installed via apt)

Edit the config file with:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

For LAMPP MySQL (XAMPP on Linux)

Edit the LAMPP MySQL config file with:

sudo nano /opt/lampp/etc/my.cnf

Step 3: Increase max_allowed_packet Setting

Inside the file, find the [mysqld] section. If it doesn’t exist, add it.

Add this line under [mysqld]:

max_allowed_packet=256M

You can choose a size that fits your needs:

  • 64M for 64MB
  • 128M for 128MB
  • 256M for 256MB (recommended for larger imports)

Step 4: Save and Exit

For nano, save and exit using:

  • CTRL + O (Write the file)
  • ENTER (Confirm save)
  • CTRL + X (Exit the editor)

Step 5: Restart MySQL Service

For Native MySQL (Installed via apt)

Restart MySQL with:

sudo systemctl restart mysql

For LAMPP MySQL (XAMPP on Linux)

Restart all LAMPP services with:

sudo /opt/lampp/lampp restart

Step 6: Confirm New Setting

You can check if max_allowed_packet is updated correctly.

Native MySQL

mysql -u root -p -e "SHOW VARIABLES LIKE 'max_allowed_packet';"

LAMPP MySQL (XAMPP)

/opt/lampp/bin/mysql -u root -p -e "SHOW VARIABLES LIKE 'max_allowed_packet';"

Example Output (for 256M)

+--------------------+------------+
| Variable_name      | Value      |
+--------------------+------------+
| max_allowed_packet | 268435456  |
+--------------------+------------+

268435456 bytes = 256MB.


Optional: Laravel Connection Configuration

Most of the time, increasing max_allowed_packet at the MySQL level is enough.

But if you want to enforce it within Laravel’s connection options (for some custom queries), you can add this to your config/database.php:

'mysql' => [
    'options' => [
        PDO::MYSQL_ATTR_MAX_ALLOWED_PACKET => 256 * 1024 * 1024,  // 256MB
    ],
],

This is rarely needed if the MySQL server itself is configured correctly.


Complete Process Summary

StepNative MySQL (apt)LAMPP (XAMPP for Linux)
Edit Config File/etc/mysql/mysql.conf.d/mysqld.cnf/opt/lampp/etc/my.cnf
Edit max_allowed_packetmax_allowed_packet=256M under [mysqld]Same
Restart Servicesudo systemctl restart mysqlsudo /opt/lampp/lampp restart
Check New Valuemysql -u root -p -e "SHOW VARIABLES LIKE 'max_allowed_packet';"/opt/lampp/bin/mysql -u root -p -e "SHOW VARIABLES LIKE 'max_allowed_packet';"

⚠️ Additional Notes

  • If you are importing a very large SQL file (like over 500MB), you may want to split the file into smaller chunks.
  • Some servers (especially shared hosting) have strict limits on max_allowed_packet, and you may not be able to increase it beyond a certain size.
  • You can temporarily raise the limit when importing data, then lower it for normal usage.

🚀 Do You Want a Shell Script to Automate This Entire Process?

I can prepare a ready-to-run shell script that:

  • Detects if you’re using native MySQL or LAMPP.
  • Edits the correct config file.
  • Sets max_allowed_packet=256M.
  • Restarts the appropriate service.
  • Confirms the new setting.

Perfect! Below is a ready-to-use shell script that detects whether you’re using native MySQL (installed via apt) or LAMPP MySQL (XAMPP for Linux). It:

✅ Edits the correct configuration file.
✅ Sets max_allowed_packet=256M.
✅ Restarts either MySQL service or LAMPP services.
✅ Confirms the new setting using SHOW VARIABLES LIKE 'max_allowed_packet';.


📜 Shell Script: set-max-packet.sh

Create the script:

nano set-max-packet.sh

Paste this code into the file:

#!/bin/bash

echo "🔎 Detecting MySQL Installation..."

# Check if LAMPP exists
if [ -d "/opt/lampp" ]; then
    echo "✅ Detected LAMPP (XAMPP for Linux)."
    CONFIG_FILE="/opt/lampp/etc/my.cnf"
    MYSQL_COMMAND="/opt/lampp/bin/mysql -u root -p"
    RESTART_COMMAND="sudo /opt/lampp/lampp restart"
else
    echo "✅ Detected Native MySQL (installed via apt)."
    CONFIG_FILE="/etc/mysql/mysql.conf.d/mysqld.cnf"
    MYSQL_COMMAND="mysql -u root -p"
    RESTART_COMMAND="sudo systemctl restart mysql"
fi

# Backup config file
echo "🛠️ Backing up $CONFIG_FILE to ${CONFIG_FILE}.backup"
sudo cp "$CONFIG_FILE" "${CONFIG_FILE}.backup"

# Check if max_allowed_packet already exists
if grep -q "max_allowed_packet" "$CONFIG_FILE"; then
    echo "🔄 Updating existing max_allowed_packet value..."
    sudo sed -i 's/^max_allowed_packet.*/max_allowed_packet=256M/' "$CONFIG_FILE"
else
    echo "➕ Adding max_allowed_packet to [mysqld] section..."

    # Ensure [mysqld] section exists
    if ! grep -q "\[mysqld\]" "$CONFIG_FILE"; then
        echo "[mysqld]" | sudo tee -a "$CONFIG_FILE"
    fi

    # Append setting
    echo "max_allowed_packet=256M" | sudo tee -a "$CONFIG_FILE"
fi

# Restart MySQL or LAMPP
echo "🔄 Restarting MySQL Service..."
$RESTART_COMMAND

# Confirm the new value
echo "✅ Verifying new max_allowed_packet value:"
$MYSQL_COMMAND -e "SHOW VARIABLES LIKE 'max_allowed_packet';"

echo "✅ Process complete!"

✅ Save and Exit

Press:

  • CTRL+O (save)
  • ENTER (confirm filename)
  • CTRL+X (exit)

✅ Make It Executable

chmod +x set-max-packet.sh

✅ Run It

sudo ./set-max-packet.sh

✅ What This Script Does

StepAction
1Detects if you’re using LAMPP (XAMPP) or Native MySQL
2Backs up the current config file (so you can restore if needed)
3Adds or updates max_allowed_packet=256M under [mysqld]
4Restarts either MySQL service or LAMPP services
5Runs SHOW VARIABLES LIKE 'max_allowed_packet'; to verify the setting

Best Cardiac Hospitals Near You

Discover top heart hospitals, cardiology centers & cardiac care services by city.

Advanced Heart Care • Trusted Hospitals • Expert Teams

View Best Hospitals
<p data-start="140" data-end="435">I’m Abhishek, a DevOps, SRE, DevSecOps, and Cloud expert with a passion for sharing knowledge and real-world experiences. I’ve had the opportunity to work with <a class="decorated-link" href="https://www.cotocus.com/" target="_new" rel="noopener" data-start="300" data-end="335">Cotocus</a> and continue to contribute to multiple platforms where I share insights across different domains:</p> <ul data-start="437" data-end="922"> <li data-start="437" data-end="514"> <p data-start="439" data-end="514"><a class="decorated-link" href="https://www.devopsschool.com/" target="_new" rel="noopener" data-start="439" data-end="485">DevOps School</a> – Tech blogs and tutorials</p> </li> <li data-start="515" data-end="599"> <p data-start="517" data-end="599"><a class="decorated-link" href="https://www.holidaylandmark.com/" target="_new" rel="noopener" data-start="517" data-end="569">Holiday Landmark</a> – Travel stories and guides</p> </li> <li data-start="600" data-end="684"> <p data-start="602" data-end="684"><a class="decorated-link" href="https://www.stocksmantra.in/" target="_new" rel="noopener" data-start="602" data-end="647">Stocks Mantra</a> – Stock market strategies and tips</p> </li> <li data-start="685" data-end="764"> <p data-start="687" data-end="764"><a class="decorated-link" href="https://www.mymedicplus.com/" target="_new" rel="noopener" data-start="687" data-end="732">My Medic Plus</a> – Health and fitness guidance</p> </li> <li data-start="765" data-end="841"> <p data-start="767" data-end="841"><a class="decorated-link" href="https://www.truereviewnow.com/" target="_new" rel="noopener" data-start="767" data-end="814">TrueReviewNow</a> – Honest product reviews</p> </li> <li data-start="842" data-end="922"> <p data-start="844" data-end="922"><a class="decorated-link" href="https://www.wizbrand.com/" target="_new" rel="noopener" data-start="844" data-end="881">Wizbrand</a> – SEO and digital tools for businesses</p> </li> </ul> <p data-start="924" data-end="1021">I’m also exploring the fascinating world of <a class="decorated-link" href="https://www.quantumuting.com/" target="_new" rel="noopener" data-start="968" data-end="1018">Quantum Computing</a>.</p>

Related Posts

The Definitive Guide to Certified FinOps Professional: Skills, Tracks, and Career Impact

The shift toward cloud-native architectures has fundamentally changed how organizations manage their finances, moving from fixed capital expenses to variable operational spend. This guide focuses on the…

Read More

A Complete Guide to the Certified FinOps Manager Credential

Cloud infrastructure spending has grown significantly, creating an urgent demand for professionals who understand the intersection of engineering, finance, and business strategy. The Certified FinOps Manager credential,…

Read More

Certified FinOps Engineer: The Definitive Career Guide for Modern Cloud Professionals

The shift toward cloud-native infrastructure has transformed how organizations consume resources, moving from fixed capital expenses to variable operational costs. In this landscape, the Certified FinOps Engineer…

Read More

Certified FinOps Architect: A Step-by-Step Guide to Mastery and Career Growth

Introduction The Certified FinOps Architect designation represents the highest tier of technical leadership in the intersection of finance and cloud engineering. As organizations scale their cloud footprint,…

Read More

The Professional Path to Certified DataOps Manager (CDOM): Scaling Data Reliability and Operational Excellence

Introduction The role of data in modern enterprise environments has shifted from a backend storage concern to the primary engine of business value. As organizations struggle to…

Read More

The Complete Roadmap to Becoming a Certified MLOps Manager: Skills, Tracks, and Real-World Impact

Introduction The transition from traditional software development to machine learning requires a robust operational framework that ensures reliability and scalability. A Certified MLOps Manager plays a pivotal…

Read More
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x