SQLSTATE[HY000]: General error: 1835 Malformed communication packet on LARAVEL
Recently, my application stopped working raising 500 server error while performing any database operation even when connecting to database. I was so confused about what has caused the problem. I was curious about whether the problem was caused by system bug, code error or a hacking attempt. Then I switched my application to debug mode and found the stack trace of error. It was:
SQLSTATE[HY000]: General error: 1835 Malformed communication packet (SQL: actual sql query)
Then I started searching for the solution but fortunately, there were so many applications having similar issues. I then discovered that the root cause of this issue.
Cause Of The Error
This issue started happening for a lot of people after the recent MariaDB update 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 dependencies 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(config/database.php):
'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', '**'), 'username' => env('DB_USERNAME', '**'), '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]** ],
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.