shinozaki0815
@shinozaki0815

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Laravel DB作成エラー

解決したいこと

laravelでdb作成するときににエラーが発生しました。
解決方法を教えて下さい。

発生している問題・エラー

masanori@MacBook-Air kgb_zak_laravel % docker compose exec app bash
root@0de1bd993dd1:/var/www# cd laravel-project
root@0de1bd993dd1:/var/www/laravel-project# php artisan migrate

Deprecated: PHP Startup: Use of mbstring.internal_encoding is deprecated in Unknown on line 0

   Illuminate\Database\QueryException 

  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for db failed: Name or service not known (Connection: mysql, SQL: select table_name as `name`, (data_length + index_length) as `size`, table_comment as `comment`, engine as `engine`, table_collation as `collation` from information_schema.tables where table_schema = 'laravel-project' and table_type in ('BASE TABLE', 'SYSTEM VERSIONED') order by table_name)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:829
    825▕                     $this->getName(), $query, $this->prepareBindings($bindings), $e
    826▕                 );
    827▕             }
    828▕ 
  ➜ 829▕             throw new QueryException(
    830▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    831▕             );
    832▕         }
    833▕     }

      +39 vendor frames 

  40  artisan:35
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

2024_08_15_140055_create_home_table.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Home;


class PagesController extends Controller
{
    // Homeを表示
    public function getHome(){

      return view('home');
    }

    // 投稿された内容を表示するページ
    public function create(Request $request) {

      // バリデーションチェック
      $request->validate([
        'name' => 'required|max:10',
        'email' => 'required|email',
        'comment' => 'required|min:5|max:140',
    ]);

      // 投稿内容の受け取って変数に入れる
      $name = $request->input('name');
      $email = $request->input('email');
      $comment = $request->input('comment');


      // 変数をビューに渡す
      return view('home')->with([
          "name" => $name,
          "comment"  => $comment,
          "email"=>$email ,
          
      ]);
    }

}

docker-compose.yml

services:
  app:
    container_name: app 
    build: ./docker/php
    volumes:
      - .:/var/www

  nginx:
    image: nginx
    container_name: nginx 
    ports:
      - 8000:80
    volumes:
      - .:/var/www
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
    working_dir: /var/www
    depends_on:
      - app

  db:
    image: mysql:8.0
    platform: linux/amd64
    container_name: db
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: database
      MYSQL_USER: db-user
      MYSQL_PASSWORD: db-pass
      TZ: 'Asia/Tokyo'
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    volumes:
      - ./docker/db/data:/var/lib/mysql
      - ./docker/db/my.cnf:/etc/mysql/conf.d/my.cnf
      - ./docker/db/sql:/docker-entrypoint-initdb.d
    ports:
      - 3306:3306

.env

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:MICHU9P/9Au3EESVU9g/soO3qJdRr6jv/Ney4rlNAIk=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel-project
DB_USERNAME=root
DB_PASSWORD=secret

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailpit
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1

VITE_APP_NAME="${APP_NAME}"
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}

0

2Answer

db サービスが起動していないようです。 docker-compose.yml に以下のように追記して docker compose start した後もう一度試してみてください。

services:
  app:
    
    depends_on:
      - db
0Like

Your answer might help someone💌