🚗🏍️ Welcome to Motoshare!

Turning Idle Vehicles into Shared Rides & New Earnings.
Why let your bike or car sit idle when it can earn for you and move someone else forward?

From Idle to Income. From Parked to Purpose.
Earn by Sharing, Ride by Renting.
Where Owners Earn, Riders Move.
Owners Earn. Riders Move. Motoshare Connects.

With Motoshare, every parked vehicle finds a purpose. Partners earn. Renters ride. Everyone wins.

Start Your Journey with Motoshare

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

Uncategorized

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

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