0
0

More than 1 year has passed since last update.

仮想環境上のLaravelプロジェクトでmigrationが実行できない。

Last updated at Posted at 2021-11-18

やりたいこと

  • 仮想環境上(vagrantとvirtualbox)のLaravelで「php artisan migrate」を実行したい

エラー内容

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known (SQL: select * from information_schema.tables where table_schema = laravel-quest and table_name = migrations and table_type = 'BASE TABLE')
at /var/www/html/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::("PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known")
      /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  2   PDO::__construct("mysql:host=mysql:5.7;port=3306;dbname=laravel-quest", "root", "root", [])
      /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  Please use the argument -v to see more details.

調査した内容

実施した内容

  • 「php artisan migrate」を「vagrant ssh」で接続した先で実行

    • 結果は変わらず。。
  • phpMyAdmin上で作成した「laravel-quest」(掲示板アプリのDB名)のホスト名の確認

    • ホスト名は「%」か「localhost」 スクリーンショット 2021-11-06 14.25.44.png
  • .envの設定を確認

    現状の内容は以下の通り。

    APP_NAME=Laravel
    APP_ENV=local
    APP_KEY=自身のAPIキー
    APP_DEBUG=true
    APP_URL=自身で設定したURL
    
    LOG_CHANNEL=stack
    
    DB_CONNECTION=mysql
    DB_HOST=mysql5.7
    DB_PORT=3306
    DB_DATABASE=データベース名
    DB_USERNAME=root
    DB_PASSWORD=
    
    BROADCAST_DRIVER=log
    CACHE_DRIVER=file
    QUEUE_CONNECTION=sync
    SESSION_DRIVER=file
    SESSION_LIFETIME=120
    
    REDIS_HOST=127.0.0.1
    REDIS_PASSWORD=null
    REDIS_PORT=6379
    
    MAIL_DRIVER=smtp
    MAIL_HOST=smtp.mailtrap.io
    MAIL_PORT=2525
    MAIL_USERNAME=null
    MAIL_PASSWORD=null
    MAIL_ENCRYPTION=null
    MAIL_FROM_ADDRESS=null
    MAIL_FROM_NAME="${APP_NAME}"
    
    AWS_ACCESS_KEY_ID=
    AWS_SECRET_ACCESS_KEY=
    AWS_DEFAULT_REGION=us-east-1
    AWS_BUCKET=
    
    PUSHER_APP_ID=
    PUSHER_APP_KEY=
    PUSHER_APP_SECRET=
    PUSHER_APP_CLUSTER=mt1
    
    MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
    MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
    
    • BB_HOSTのところを「localhost」とかに変更
      • 結果は変わらず。。

原因と思うところ

  • 「php artisan migrate」を実行する場所に問題があるのでは??
  • 「.env」のデータベース設定に問題がある??
  • そもそもphpMyAdmin上からデータベースを作成はしない方がよかった??

解決策

  • 「.env」の「DB_PASSWORD=」の設定がされていなかったことが原因でした。。

    こちらを、、

    DB_PASSWORD=
    

    以下のように変更

    DB_PASSWORD=root
    

なんてことない原因でした。笑
「.env」の設定の確認は怠らないことが重要ですね!

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