Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel / MySQL Error: SQLSTATE[HY000]: General error: 1835 Malformed communication packet

Tags:

php

mysql

laravel

Good morning. I'm using Laravel on VPS server.

Short situation description: Yesterday everything was working fine (for months our website were working fine), this morning I got woke up from my colleagues that both of our websites are down.

When trying to access them we receive error:

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

I have checked online and can't find a solution. I tried to upgrade MySQL to newest version (Maria DB 10.3)

I tried to reset password for database user. (Also no changes)

I checked and tried sollution to set read_rnd_buffer_size=256K in my.cnf file for mysql settings

When I try to call this function directly in phpMyAdmin select * from users where id = 1 limit 1 it returns expected results.

I will appreciate every help, as all of our business depends on these platforms, I need to make them work as soon as possible.

With greetings, Artis.

Edit: When I try to disable function that causes error, it just shows next function, and all over like that. So I believe that Laravel can't conect with mysql at all.

After deeper research I found out that only Laravel can't connect to database. On same server I have 2x Laravel applications, Codeigniter and wordpress. Both Laravel applications stopped to work at same time, but codeigniter and wordpress works as expected.

like image 765
Crelix Avatar asked Nov 04 '20 10:11

Crelix


2 Answers

for a quick fix just add this

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

to config/database.php in

'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],
    ],

the proper solution is to upgrade your php from 7.2 to 7.4 or downgrade your mariadb

like image 128
aitbella Avatar answered Nov 17 '22 19:11

aitbella


The new update requires PHP 7.3 to connect with MariaDB.

Since the issue is caused by the latest upgrade from MariaDB MDEV-24121, a more suitable solution is to downgrade MariaDB and yum-locking the MariaDB packages in place to avoid the packages from being updated and unlock them when they're patched. More details here on how to do it: Updating MariaDB to v10.2.35 or v10.3.26, causes MySQL Databases interface to show MySQL as offline

If you cannot downgrade your MariaDB or cannot update your PHP, a possible workaround is to set "PDO::ATTR_EMULATE_PREPARES" to true.

like image 1
Alex Avatar answered Nov 17 '22 19:11

Alex