LoginSignup
44
26

More than 3 years have passed since last update.

Laravel tinker データベースの接続できるか疎通確認する

Last updated at Posted at 2020-07-23
$ php artisan tinker

データベースに接続できるか確認する

簡単なのはSELECT文が通るか実行して確認します。」

>>> DB::select('select 1');
=> [
     {#3046
       +"1": 1,
     },
   ]

このような結果が返れば、少なくともデータベースに接続できています。

データベースに接続できない場合

データベース接続設定内容を確認します。

$ php artisan tinker
>>> config('database.default')
=> "mysql"

>>> config('database.connections.mysql')
=> [
     "driver" => "mysql",
     "url" => null,
     "host" => "db",
     "port" => "3306",
     "database" => "laravel_local",
     "username" => "phper",
     "password" => "secret",
     "unix_socket" => "",
     "charset" => "utf8mb4",
     "collation" => "utf8mb4_unicode_ci",
     "prefix" => "",
     "prefix_indexes" => true,
     "strict" => true,
     "engine" => null,
     "options" => [],
   ]

>>> DB::connection()->getConfig();
=> [
     "driver" => "mysql",
     "host" => "db",
     "port" => "3306",
     "database" => "laravel_local",
     "username" => "phper",
     "password" => "secret",
     "unix_socket" => "",
     "charset" => "utf8mb4",
     "collation" => "utf8mb4_unicode_ci",
     "prefix" => "",
     "prefix_indexes" => true,
     "strict" => true,
     "engine" => null,
     "options" => [],
     "name" => "mysql",
   ]

>>> DB::connection()->getPdo();
=> PDO {#3051
     inTransaction: false,
     attributes: {
       CASE: NATURAL,
       ERRMODE: EXCEPTION,
       AUTOCOMMIT: 1,
       PERSISTENT: false,
       DRIVER_NAME: "mysql",
       SERVER_INFO: "Uptime: 859  Threads: 3  Questions: 42  Slow queries: 0  Opens: 116  Flush tables: 3  Open tables: 36  Queries per second avg: 0.048",
       ORACLE_NULLS: NATURAL,
       CLIENT_VERSION: "mysqlnd 7.4.4",
       SERVER_VERSION: "8.0.19",
       STATEMENT_CLASS: [
         "PDOStatement",
       ],
       EMULATE_PREPARES: 0,
       CONNECTION_STATUS: "db via TCP/IP",
       DEFAULT_FETCH_MODE: BOTH,
     },
   }

この結果と .env 等の設定を比較して正しい設定が行われているか確認します。

参考

44
26
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
44
26