50

Suddenly got

SQLSTATE[HY000]: General error: 1835 Malformed communication packet (SQL: select * from tb_users where (username = 121211) limit 1)

on Laravel.

I already checked this: MySQL: ERROR 2027 (HY000): Malformed packet, but it seems a different case.

  1. I've successfully logged in to MySQL after previously login using SSH (using: mysql -u -p).
  2. I've successfully logged in to MySQL directly from a remote PC (using: mysql -h [IP] -u -p).

But my Laravel got the error I mentioned before. Any experience in this?

kmoser
  • 8,780
  • 3
  • 24
  • 40
wbhuana
  • 845
  • 1
  • 7
  • 8
  • Update your client and libs, seems something is outdated? What versions are you using on server and client? – Daniel W. Nov 04 '20 at 10:01
  • The laravel application already run for several month. No upgrade both on MYSQL or Laravel version. – wbhuana Nov 04 '20 at 10:16
  • If something is working and then out of a sudden it's not anymore working, how do you assume "nothing changed" ? Of course something has changed in your software. – Daniel W. Nov 04 '20 at 12:56
  • Has anyone with this problem tried to upgrade the clients to the same version the server upgraded to? – Daniel W. Nov 04 '20 at 14:22
  • Is this actually MySQL or is it MariaDB? A client has a CPanel server and it updated both the MariaDB server and clients this AM and that's when the problems started. – Craig Jacobs Nov 04 '20 at 14:46
  • 1
    @CraigJacobs I think MariaDB too but OP is not sharing any version information. Can someone try to open an issue on their JIRA, I don't open bug tickets outside of github. – Daniel W. Nov 04 '20 at 14:50
  • 1
    I am also having the issue with MariaDB, version 10.3. Woke up to it this morning. The workaround below changes integers to strings in responses. It is not a solution. – matticustard Nov 04 '20 at 16:03
  • @matticustard what are your server, client and driver versions? – Daniel W. Nov 04 '20 at 16:12
  • @DanielW. Server version: 10.3.26-MariaDB | cpsrvd 11.90.0.16 | Database client version: libmysql - 5.6.43 ... Sorry if this is a stupid question, but where do I find the driver version? – matticustard Nov 04 '20 at 16:26
  • @everyone it may be that this issue is related to a MariaDB update, see: https://mariadb.com/kb/en/mariadb-10326-release-notes/ – Raddicus Nov 04 '20 at 16:42
  • On our server: mysql Ver 15.1 Distrib 10.3.26-MariaDB, for Linux (x86_64) using readline 5.1 – Raddicus Nov 04 '20 at 16:43
  • 4
    Additional info: Switching from PHP 7.2 to PHP 7.3 seems to alleviate the error on my server. However, other issues could be introduced with this change. – matticustard Nov 04 '20 at 17:40
  • For us, one domain using PHP 7.1.33 has the issue, but our other domain running 7.3.24 does not have it. – Richard Lovejoy Nov 04 '20 at 19:51
  • 8
    To confirm, this is a bug in all of the following MariaDB versions released yesterday: 10.1.48, 10.2.35, 10.3.26, 10.4.16, 10.5.7 A case has been opened: https://jira.mariadb.org/browse/MDEV-24121 and I provided a reproducible POC. This issue occurs when `Emulate Prepares` is set to false (by default in Laravel) and `PDO::ERRMODE_EXCEPTION` is set together. (also default in Laravel) - The correct workaround is to rollback and lock in the previous version until a fix is released. – Peter B Nov 05 '20 at 01:55

16 Answers16

38

All my Laravel apps running PHP 7.2 had this error but those running on PHP 7.3 did not. So I changed the PHP version to 7.3 and the problem was fixed. (Running Laravel 7)

felixmensah6
  • 391
  • 3
  • 4
  • Yep for beginners : cPanel -> "Logiciel" -> "Gestionnaire MultiPHP". Select "PHP 7.3 in the right dropdown. Check your domain(s) and click to apply. The update is instantaneous – bArraxas Nov 05 '20 at 09:49
  • I upgraded my php to 7.3 but still had the same issue but my project is still on 5.4 – Ibrahim Usman Nov 05 '20 at 11:33
27

Found the solution. Don't know if it's permanent or temporary:

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
            **'options'   => [PDO::ATTR_EMULATE_PREPARES => true]**
        ],

make sure that

'options' => [PDO::ATTR_EMULATE_PREPARES => true]

exist on mysql connection.

wbhuana
  • 845
  • 1
  • 7
  • 8
  • Happened to us today, too. Other SO question here: https://stackoverflow.com/questions/64677005/general-error-1835-malformed-communication-packet. **I tried your solution and it seems that the error went away!** Thanks! Although some other SO answers claim that `ATTR_EMULATE_PREPARES` comes with reduced security (https://stackoverflow.com/questions/10113562/pdo-mysql-use-pdoattr-emulate-prepares-or-not). We'll see... – Smuuf Nov 04 '20 at 11:31
  • 1
    We tried this out for our laravel application, but now nothing saves to the database.. – Bowis Nov 04 '20 at 13:25
  • 3
    This is not a solution, nor an explanation, it's rather a workaround with implications not mentioned in your answer. – Daniel W. Nov 04 '20 at 13:39
  • We tried it and it worked. But like @DanielW. said this is just a workaround – fakhruddin tahery Nov 04 '20 at 13:53
  • 15
    This workaround is changing integers to strings in responses. This causes strict conditionals `===` to fail due to mismatched types. – matticustard Nov 04 '20 at 16:06
  • 3
    This solution breaks things. I don't recommend it. The problem is that there was a MariaDB update that broke things and needs to be downgraded. See incogzito's comment. – Raddicus Nov 04 '20 at 17:28
  • @Raddicus can you try to update the client instead of downgrade the server? – Daniel W. Nov 04 '20 at 19:25
  • 4
    Upstream [MariaDB JIRA issue MDEV-24121](https://jira.mariadb.org/browse/MDEV-24121) thanks to cpanel folks. – danblack Nov 04 '20 at 23:51
  • @DanielW. Downgrading the server seemed like the best solution yesterday. Today it is looking more like the best solution might be to upgrade to PHP 7.3. I haven't read enough about updating the client to make me think that is the right direction to go in. If someone else tries it, let us know how it goes. – Raddicus Nov 05 '20 at 17:27
17

This issue started happening for a lot of people after the recent MariaDB update yesterday after Updating MariaDB to v10.3.26 (and 10.2.35). This issue is already addressed here: https://jira.mariadb.org/browse/MDEV-24121

As of now, these are the only known solutions:

1. Upgrade your PHP to 7.3: It appears these errors are shown on sites using php < 7.3. So upgrading your site's PHP to version 7.3 or 7.4 should resolve the issue.

CONS: Not a lot of applications can be easily upgraded to php 7.3 just like that. Sometimes you might need to update your platform, rewrite some codes or check all dependancies and see if they all work on 7.3. This may not be a quick fix for a lot of matured applications.

2. Downgrade MariaDB: This is a temporary fix since downgrading MariaDB will set it back to the previous state.

CONS: Downgrading MariaDB is not an easy thing to do with a click of a button from cpanel. You might need help from a network engineer to do the downgrade for you. After that you also might need to do yum-locking the MariaDB packages in order to avoid it from being updated until they're patched.

3. Add 'options' => [PDO::ATTR_EMULATE_PREPARES => true] to Database Config: This has been suggested in some answers which might solve 1 issue but open up a lot of other issues.

CONS: Adding the above to the database config file solved 1 problem for me, but it also opened a whole lot of other queries that were failing, database inserts where failing, etc. So I would not recommend this fix at all.

4. Wait for MariaDB Update: The next update should address this issue.

CONS: We don't know how long it will take to get an update that fixes this issue for older versions of PHP. It could be even days and some applications might not be able to wait that long.

So all in all, these are the only options I can see as of now. Just hoping there is a fix for it soon.

Short Term Fix: Out of all, downgrading MariaDB seems to be the only easy (sorta) temporary fix for me considering my application needs a lot of work to be ready for php 7.3. I downgraded MariaDB to 10.2.34 and locked it and the error no longer appears.

Long Term Fix: It is better to eventually get your application ready for php 7.3 and upgrade to that so MariaDB newer version also will not complain.

Neel
  • 9,352
  • 23
  • 87
  • 128
  • UPDATE: I downgraded MariaDB to 10.2.34 and locked it and the error no longer appears. – Neel Nov 06 '20 at 00:11
  • If you are on debian 9 with mariadb 10.2, you may use `sudo apt install mariadb-server-core-10.2=10.2.34+maria~stretch mariadb-server-10.2=10.2.34+maria~stretch mariadb-server=10.2.34+maria~stretch` (I had to re-enter the root key on setup, don't worry the data is still there.) – proc Nov 08 '20 at 12:07
  • 2
    [Emergency Release of MariaDB 10.5.8, 10.4.17, 10.3.27, and 10.2.36 is now available](https://mariadb.org/mariadb-10-5-8-10-4-17-10-3-27-and-10-2-36-now-available/) that corrects this. – danblack Nov 11 '20 at 21:53
7

Also ran into this issue after mariadb updated overnight. Downgrading mariadb fixed the issue for me.

https://support.cpanel.net/hc/en-us/articles/360056772334

incogzito
  • 71
  • 3
  • 3
    Not the best solution, as downgrading will bring back some glitches or security enhancements that were fixed in the upgrade :) Better is to upgrade your PHP to PHP7.3 :-) – Hasan Alsawadi Nov 05 '20 at 08:44
  • Cpanel has updated their article which no longer includes the steps to downgrade, have you got the downgrading steps you took? – Joss Bird Nov 05 '20 at 10:28
  • 2
    @JossBird This should do the downgrade. `yum downgrade MariaDB-server MariaDB-common MariaDB-shared MariaDB-client MariaDB-compat MariaDB-devel`. I would recommend backing everything up before downgrading though. – incogzito Nov 05 '20 at 16:05
  • [Different issue](https://jira.mariadb.org/browse/MDEV-24122) to which `ALTER TABLE mysql.users DROP COLUMN IF EXISTS password_lifetime, DROP COLUMN IF EXISTS password_last_changed, DROP COLUMN IF EXISTS account_locked; FLUSH PRIVILEGES;` will correct on MariaDB-10.1, 10.2 and 10.3. 10.4+ won't show this issue. – danblack Nov 14 '20 at 20:49
4

An emergency Release of MariaDB 10.5.8, 10.4.17, 10.3.27, and 10.2.36 is now available that was released to specifically solve this protocol incompatibility in old PHP versions and PDO.

danblack
  • 12,130
  • 2
  • 22
  • 41
3

The official fix is finanlly out and you can find the details on the link:
https://support.cpanel.net/hc/en-us/articles/360056772334/comments/360005577354


To quickly fix it just connect via SSH and run

sudo /scripts/autorepair fix_mariadb_show_grants_roles

For users that have applied the previous workaround involving downgrading MariaDB. Be sure to unlock MariaDB to ensure it continues to receive the appropriate updates:

yum versionlock clear
/scripts/upcp
Giacomo Torricelli
  • 764
  • 1
  • 6
  • 21
  • Note this was a lightly different issue for formerly MySQL-5.7 data directory that got upgraded. [It will be fixed](https://jira.mariadb.org/browse/MDEV-24122) in the next version however it wasn't sufficiently ready to be included in the emergency release. – danblack Nov 14 '20 at 20:44
2

Updated php version**(7.2 to 7.3)** inside cpanel for my subdomain.

Must give all the previlege to the selected database user.

It worked for me.

Zahnim77
  • 25
  • 8
2

This can happen if your query does not have the same number of input arguments as the array of argument you pass it in. Here is the code I had in NodeJS + MYSQL that was causing this issue:

const config = getMysqlConfig();

let arguments = [arg1, arg2];
const connection = await mysql.createConnection(config.db);

const result = await connection.execute('INSERT INTO mySchema.myTable(val1, val2) VALUES (?,?) ON DUPLICATE KEY UPDATE val1= val1+ ?', arguments)
.catch((err) => LogError(err));
connection.end(); 

Because I only had 2 values in arguments and yet my query was expecting 3 arguments, the malformed communication error was thrown. For me, the solution was just to add my addition argument in array

let arguments = [arg1, arg2, arg3];
Barney Chambers
  • 2,720
  • 6
  • 42
  • 78
1

I'm on Ubuntu 20 (focal), note my repo you'll need to change it depending on if your on 16 (xenial), 18 (bionic) or whatever

I don't like the options fix in Laravel with the risk it could corrupt data, and I can't upgrade PHP to 7.2+ without a lot of work so for me I downgraded a version.

Going from 10.3.26 -> 10.3.25 without restoring all the data from a dump is not recommended but I had no choice, and it appears nothing bad happened.

# stop the database

service mariadb stop

# list packages installed

dpkg -l | grep mariadb 

# remove whatever you have or the install will complain about dependencies or broken packages, you need to remove all the mariadb packages

apt remove mariadb-server-core-10.3 
apt remove mariadb-server-10.3
apt remove mariadb-server-10.2
apt remove mariadb-server-10.1

# pin the repo to v10.3.25, remember to remove any conflicting sources you have in /etc/apt/sources.list

apt-get install software-properties-common
apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://archive.mariadb.org/mariadb-10.3.25/repo/ubuntu/ focal main'

# install the old version

apt install mariadb-server

# start it back up

service mariadb start
Jack Sukerman
  • 161
  • 1
  • 3
1

What worked for me was to upgrade PHP version on the subdomain from 7.2 to 7.3. I did not change anything in the database configuration as has been suggested in some answers.

1

This sudden error message is caused by a MariaDB client upgrade, which appears to be incompatible with the PHP 7.2 version of php-mysqlnd; version 10.2.35 breaks it, but version 10.2.34 still works. With yum or dnf one can easily revert to the previous versions eg. with:

su
yum history
yum history undo 440

Temporary setting enabled=0 in /etc/yum.repo.d/mariadb.repo might also make sense.
Upgrading to PHP 7.3 might still be the better option (while available).

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
1

MariaDB just released an upgrade that fixes the issue for those who cannot run their application on PHP >= 7.3, source: https://mariadb.org/mariadb-10-5-8-10-4-17-10-3-27-and-10-2-36-now-available/

AlexWebLab
  • 846
  • 3
  • 15
  • 29
1

Mariadb has a update for php 7.2 that fix this issue, just update the server:

sudo apt update
sudo apt upgrade
Alaerock
  • 11
  • 2
0

After many workarounds i tried today this the solutions i got

1- upgrade to php 7.3 or 7.4
(many websites will be down after php upgrades )

2- downgrade to minor version ( mariadb 10.4.16 to 10.4.15)

yum downgrade MariaDB-server MariaDB-common MariaDB-shared MariaDB-client MariaDB-compat MariaDB-devel    

anyway this problem is opened case as bug for Mariadb last night update and they didnt push any fixing for now the above solution is only the 2 ways to solve the problem , it works with me on mariadb downgrade from 10.4.16 to 10.4.15 (minor downgrade)

0

Upgrade php 7.2 to php7.4 best way for me.

` sudo add-apt-repository ppa:ondrej/php

sudo apt update

sudo apt install php7.4-fpm php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-soap php7.4-zip php7.4-bcmath -y

sudo a2enmod proxy_fcgi setenvif

sudo a2enconf php7.4-fpm

sudo systemctl reload apache2

sudo systemctl status php7.4-fpm `

Fixed

muu
  • 11
  • 1
0

Easy Solution Just Update You PHP to 7.3 Work For me In Laravel & WP

Sajibe Kanti
  • 181
  • 1
  • 9