0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravel x MariaDBで、急に「Malformed communication packet」エラーでSQLに接続できなくなった際の対応メモ

Last updated at Posted at 2020-11-04

さっきまで動いていたLaravelが下記エラーで動かなくなった。。:sob:

image.png

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

原因がわからないが、dockerのローカル環境と、別途用意している外部サーバーのテスト環境で同じDBサーバーに接続しており、両方とも同じタイミングで同じエラーになったので、
起因はDBサーバー側にありそうな。
追って余力があれば情報追記します。

追記:
yumの自動アップデートにより、MariaDBのバージョンがあがった事が原因のようです。

/var/log/yum.log
Nov 05 04:22:12 Updated: MariaDB-common.x86_64 10.2.35-1.el7.centos
Nov 05 04:22:13 Updated: MariaDB-compat.x86_64 10.2.35-1.el7.centos
Nov 05 04:22:15 Updated: MariaDB-client.x86_64 10.2.35-1.el7.centos
Nov 05 04:22:16 Updated: galera.x86_64 25.3.31-1.el7.centos
Nov 05 04:22:32 Updated: MariaDB-server.x86_64 10.2.35-1.el7.centos
Nov 05 04:22:32 Updated: MariaDB-shared.x86_64 10.2.35-1.el7.centos

対処...

laravelのconfig/database.phpに、

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

を追加する。

config/database.php
// 〜省略〜
'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST_MASTER', '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],
// 〜省略〜

これで無事エラーはなくなりました。。:smile:

※PHPや、MariaDBのバージョンを適宜更新するのが良いという情報もありますが、アップデートの影響の方が大きそうなので、今回はconfigファイルを変更で汗

ATTR_EMULATE_PREPARES はfalseの方が良さげですが、
色々言及しているサイトがあるのでそちらを参考にした方が良いかと思います。
参考:https://teratail.com/questions/233051

ATTR_EMULATE_PREPARESは、PDOの型一致の「===」に影響があるみたいなのですので要注意

追記:
ATTR_EMULATE_PREPARESをtrueにすると、PDOで比較演算子の===を使うと判定がおかしくなり、
いままでと違う条件分岐をしてしまう事が判明し、phpのバージョンを7.4上げることで対応しました。。:angel_tone2:

参考サイト:
https://jira.mariadb.org/browse/MDEV-24121
https://xenforo.com/community/threads/mariadb-10-3-26-1-breaks-php-7-2.187331/
https://teratail.com/questions/233051
https://stackoverflow.com/questions/64678367/laravel-mysql-error-sqlstatehy000-general-error-1835-malformed-communicat

環境

  • PHP 7.2.31
  • Laravel Framework 5.5.48
  • 10.2.35-MariaDB-log
0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?