Macでlaravel,vue.js,docker(ローカルではdockerにnginxを使用)を使用してローカルでWEBアプリを制作しています。.envの設定がおかしいからだとは思いますが、dbとして使用しているローカルのmysqlに繋がらず、そのために$php artisan migrateがエラーとなって詰まってしまっています。どこの設定が間違っているためにmysqlに繋がらないのかを知りたいのですがよろしくお願いします。
エラー
コード root@8be8ddaa6767:/var/www/html# php artisan migrate Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (SQL: select * from information_schema.tables where table_schema = sample 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=db;port=3306;dbname=sample", "hoge", "777", []) /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70 Please use the argument -v to see more details.
追記
DB_HOSTが異なる場合
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
となるらしいのでその辺に問題があるかもしれません。調査中ですが、
(参考
https://qiita.com/mineaki27th/items/2fec99060f1c97ec2892
)
バージョン
php: 7.4.1
laravel: 6.20.
MySQL version: 8.0.28
(docker,docker-compose.ymlはサイト・https://qiita.com/shimotaroo/items/29f7878b01ee4b99b951
を利用)
コード portfolio($docker-compose up -dをここで起動) ├─ docker │ ├─ php │ │ └─ Dockerfile │ │ └─ php.ini │ ├─ nginx │ │ └─ Dockerfile │ │ └─ default.conf │ └─ mysql │ └─ Dockerfile │ └─ my.cnf │ ├─ src(laravelのapp、config,resousesや.env等が入っています) │ │ │─ .env(下) │─ .gitignore └─ docker-compose.yml(下)
portfolio/.env
コード WEB_PORT=80 DB_PORT=3306 //docker-compose.yml内のdb DB_HOSTURL=db DB_NAME=sample DB_USER=hoge DB_PASSWORD=777 DB_ROOT_PASSWORD=secret
portfolio/src/.env
コード APP_NAME=docker-laravel-vue APP_ENV=local APP_KEY=省略 APP_DEBUG=true APP_URL=127.0.0.1 LOG_CHANNEL=stack DB_CONNECTION=mysql DB_HOST=db DB_PORT=3306 DB_DATABASE=sample
docker-compose.yml
コード # Composeファイルのバージョン version: '3.8' volumes: mysql-volume: services: app: build: context: . dockerfile: ./docker/php/Dockerfile volumes: - ./src/:/var/www/html environment: - DB_CONNECTION=mysql - DB_HOST=${DB_HOSTURL} - DB_PORT=3306 - DB_DATABASE=${DB_NAME} - DB_USERNAME=${DB_USER} - DB_PASSWORD=${DB_PASSWORD} # web_serverでも可 web: build: context: . dockerfile: ./docker/nginx/Dockerfile ports: - ${WEB_PORT}:80 depends_on: - app volumes: - ./src/:/var/www/html db: build: context: . dockerfile: ./docker/mysql/Dockerfile ports: - ${DB_PORT}:3306 environment: MYSQL_DATABASE: ${DB_NAME} MYSQL_USER: ${DB_USER} MYSQL_PASSWORD: ${DB_PASSWORD} MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD} TZ: 'Asia/Tokyo' volumes: - mysql-volume:/var/lib/mysql
ターミナルに打ち込んだ時に得られる情報
localのmysql詳細
コード masa@MasaakinoMacBook-Air portfolio % pwd /Users/masa/Desktop/portfolio masa@MasaakinoMacBook-Air portfolio % mysql -u hoge -p sample; Enter password: //『777』を入力 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 27 Server version: 8.0.28 Homebrew Copyright (c) 2000, 2022, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. mysql> select user( ); +----------------+ | user( ) | +----------------+ | hoge@localhost | +----------------+ 1 row in set (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | sample | +--------------------+ 2 rows in set (0.01 sec) mysql> SELECT DATABASE(); +------------+ | DATABASE() | +------------+ | sample | +------------+ 1 row in set (0.00 sec) mysql> use sample; Database changed mysql> SELECT DATABASE(); +------------+ | DATABASE() | +------------+ | sample | +------------+ 1 row in set (0.00 sec) mysql> exit Bye masa@MasaakinoMacBook-Air portfolio % mysql -u root -p Enter password: //『secret』を入力 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 30 Server version: 8.0.28 Homebrew Copyright (c) 2000, 2022, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
実際のphp artisan migrateがうまく行かない部分の詳細
コード masa@MasaakinoMacBook-Air portfolio % mysql.server restart Shutting down MySQL . SUCCESS! Starting MySQL .. SUCCESS! masa@MasaakinoMacBook-Air portfolio % docker-compose down [+] Running 4/4 ⠿ Container portfolio-db-1 Removed 0.1s ⠿ Container portfolio-web-1 Removed 0.3s ⠿ Container portfolio-app-1 Removed 0.3s ⠿ Network portfolio_default Removed 0.1s masa@MasaakinoMacBook-Air portfolio % docker builder prune & docker volume prune [1] 17257 WARNING! This will remove all local volumes not used by at least one container. Are you sure you want to continue? [y/N] WARNING! This will remove all dangling build cache. Are you sure you want to continue? [y/N] Total reclaimed space: 0B masa@MasaakinoMacBook-Air portfolio % docker-compose up -d [+] Running 4/4 ⠿ Network portfolio_default Created 0.1s ⠿ Container portfolio-app-1 Started 4.3s ⠿ Container portfolio-db-1 Started 1.4s ⠿ Container portfolio-web-1 Started 5.1s masa@MasaakinoMacBook-Air portfolio % docker-compose exec app bash root@8be8ddaa6767:/var/www/html# pwd /var/www/html root@8be8ddaa6767:/var/www/html# ls -a . .editorconfig .env.testing .gitignore .styleci.yml app bootstrap composer.lock database package-lock.json phpunit.xml resources server.php tests webpack.mix.js .. .env .gitattributes .phpunit.result.cache README.md artisan composer.json config node_modules package.json public routes storage vendor root@8be8ddaa6767:/var/www/html# php artisan config:cache Configuration cache cleared! Configuration cached successfully! root@8be8ddaa6767:/var/www/html# php artisan config:clear Configuration cache cleared! root@8be8ddaa6767:/var/www/html# php artisan cache:clear Application cache cleared! root@8be8ddaa6767:/var/www/html# php artisan migrate Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (SQL: select * from information_schema.tables where table_schema = sample 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=db;port=3306;dbname=sample", "hoge", "777", []) /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70 Please use the argument -v to see more details. root@8be8ddaa6767:/var/www/html#

回答1件
あなたの回答
tips
プレビュー