LoginSignup
2
1

More than 3 years have passed since last update.

【エラー解決備忘録】[Laravel6.0]migrate時のエラー解決方法の巻き

Posted at

本記事の目的

 Laravel勉強中の筆者がmigrate時に経験したエラーと解決方法をまとめました。
筆者と同様に勉強中の方やエラーで悩まれている方の参考になればと思い投稿いたしました。
 なお勉強中のため内容が足りなかったり、誤った記載があるかもしれません。
その際は指摘をビシバシしてもらえると嬉しいです!
宜しくお願いいたします。

環境

  • macOS Catalina(バージョン10.15.4)
  • Laravel Framework 6.18.18
  • MAMP

遭遇したエラー

事前にphpMyAdminで作成したデータベースにファイルを追加するため、
ターミナルで%php artisan migrateを実行すると
「DBサーバーに接続できないよ!」「そのようなファイルまたはディレクトリはないよ!」
下記エラーメッセージで指摘を受けました。

   Illuminate\Database\QueryException:
SQLSTATE[HY000] [2002] No such file or directory 
(SQL: select * from information_schema.tables where table_schema 
= laravel_task and table_name = migrations and table_type = 'BASE TABLE')

  at・・・・/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
    665| // If an exception occurs when attempting to run a query, we'll format the error
    666| // message to include the bindings with SQL, which will make this exception a
    667| // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673| 

  Exception trace:

  1   PDOException::("SQLSTATE[HY000] [2002] No such file or directory")
     ・・・・/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  2   PDO::__construct("mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;dbname=〜省略〜[])
     ・・・・/vendorvendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  Please use the argument -v to see more details.

解決方法

結論、DB_HOSTを「127.0.0.1」から「localhost」に修正するとエラーが解消されマイグレーションができました。

※ターミナル
%php artisan migrate
Migrating: 2020_06_04_****_create_名称_table
Migrated:  2020_06_04_****_create_名称_table (0.04 seconds)
(before)config>database.php
省略
'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
省略
(after)config>database.php
省略
'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
省略

解決に至るまでのステップ

今回、下記ステップで作業を行いエラーの解消とphpMyAdminへのデータ登録が実現できました。

①.evファイルの確認
まず、.evファイルの中身を確認してみると以下の記載でした。

〜省略〜
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=データベースの名前
DB_USERNAME=ユーザーネーム
DB_PASSWORD=パスワード
〜以下省略〜

②次に、phpMyAdmiでMySQLの設定を確認
Hostの指定が「localhost」であることを確認。

MySQL can be administered with phpMyAdmin.
To connect to the MySQL server from your own scripts use the following connection parameters:

Host    localhost
Port    3306
〜省略〜

③config>database.phpを確認

(before)config>database.php
'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            以下省略

とHostの指定が.evとphpMyAdminで異なっていることがわかりました。
エラーの原因としてHostの指定が異なっていたと考え、
Hostの指定を統一するとエラーが解消されmigrateを無事に行うことができました。

※ターミナル
%php artisan migrate
Migrating: 2020_06_04_****_create_名称_table
Migrated:  2020_06_04_****_create_名称_table (0.04 seconds)

参考サイト

Laravel6.0でmigrateできなかった時の対処方法

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