SQLSTATE[HY000]: General error: 1835 Malformed communication packet on LARAVEL

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)


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.


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.