1
0

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 5 years have passed since last update.

【Larabel5.5】DB接続について※備忘録

Posted at

今回はLaravelでテーブル作成する際に、DB接続しておく必要があるので、接続設定について色々迷走したのでここに記載しておく。

毎回のことながら、自分自身の備忘録ですw

DB接続されていないと php artisan migrate しても以下エラーが出ます。

In Connector.php line 67:
  SQLSTATE[HY000] [2006] MySQL server has gone away
In Connector.php line 67:
  Packets out of order. Expected 0 received 45. Packet size=4739923

DB接続設定でどうやってすればいいのか、色々調べてみると以下2ファイルが関係していることがわかった。

blog/.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

config/database.php
<?php

return [

    'default' => env('DB_CONNECTION', 'mysql'),

    'connections' => [

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

        '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' => true,
            'engine' => null,
        ],
~省略~

「なんで2つもあるねん!」と思ったが、どうやらそれぞれ役割が違うみたい。

config/database.php

の方は .envファイル から読み込んでいるらしいことがわかった。
なので、DB設定する場合は、 .env ファイルの方を修正すれば良い。
修正箇所は

blog/.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1 //ここ
DB_PORT=3306
DB_DATABASE=homestead //ここ
DB_USERNAME=homestead //ここ
DB_PASSWORD=secret   //ここ

//ここ と記載した箇所を修正すればDB接続はOK。

これでDB接続設定は完了です。

1
0
0

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?