Phpartisan migration does not work in a Docker environment

Asked 1 years ago, Updated 1 years ago, 292 views

I use laravel,vue.js,docker (local uses nginx for docker) on my Mac to create a web app locally. I think it's because the .env configuration is strange, but it doesn't connect to the local mysql that I use as db, and the phpartisan migrate gets stuck in error.I'd like to know which setting is wrong and it doesn't connect to mysql, thank you.

### Add
I forgot to explain that I am multiposting.The content is exactly the same, but I would like to add it.
https://teratail.com/questions/dudp4gwhsdoz12

Error

root@8be8ddaa6767:/var/www/html#phpartisan 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_BASE)

  at/var/www/html/vendor/larvel/framework/src/Illuminate/Database/Connection.php:669
    665 | // If an exception occurred 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 installed of just the database's errors.
    668 | catch(Exception$e){
  >669 | through 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/larvel/framework/src/Illuminate/Database/Connectors/Connector.php: 70

  2PDO::__construct("mysql:host=db; port=3306; dbname=sample", "hoge", "777", [ ])
      /var/www/html/vendor/larvel/framework/src/Illuminate/Database/Connectors/Connector.php: 70

  Please use the argument-v to see more details.

Add

When I tried what you advised me in the comments, I found out that the process was not running well.

%docker-compositions
NAME COMMAND SERVICE STATUS PORTS
portfolio-app-1 "docker-php-entrypoi..." app running 9000/tcp
portfolio-db-1 "docker-entrypoint.s…" db expired(1)          
portfolio-web-1"/docker-entrypoint...."web running 0.0.0.0:80->80/tcp

Additional 2

%docker-compose logs db says 'utf8mb4_0900_ai_ci' is strange

%docker-compose logs db
portfolio-db-1 | 2022-03-05 10:29:56 + 09:00 [Note] [Entrypoint]—Entrypoint script for MySQL Server 5.7.37-1 debian10 started.
portfolio-db-1 | 2022-03-05 10:29:57 + 09:00 [ERROR] [Entrypoint]—mysqld failed while attempting to check config
portfolio-db-1 | command was: mysqld -- verbose --help --log-bin-index=/tmp/tmp.Ef2AJ4XEgw
portfolio-db-1 | 2022-03-05T10:29:57.143207 + 09:00 [ERROR] Unknown collation: 'utf8mb4_0900_ai_ci'
portfolio-db-1 | 2022-03-05T10:29:57.148263 + 09:00 [ERROR] Aborting
masa@MasaakinoMacBook-Airportfolio% 



docker/mysql/my.cnf has this description 'utf8mb4_0900_ai_ci'

portfolio/docker/mysql/my.cnf

[mysqld]
user=mysql
character_set_server=utf8mb4
// This part
collation_server=utf8mb4_0900_ai_ci

# timezone
default-time-zone=SYSTEM
log_timestamps=SYSTEM

# Error Log
log-error=mysql-error.log

# Slow Query Log
slow_query_log = 1
slow_query_log_file=mysql-slow.log
long_query_time = 1.0
log_queries_not_using_indexes=0

# General Log
general_log = 1
general_log_file=mysql-general.log

[mysql]
default-character-set=utf8mb4

[client]
default-character-set=utf8mb4

Also, when I checked the environment variables in thinker, some of them didn't seem to be reflected, so there seems to be a cause here.

 masa @MasaakinoMacBook-Air portfolio%docker-compose exec app bash  
root@7b0ccf8be086:/var/www/html#phpartisantinker
Psy Shell v0.11.1 (PHP7.4.1 — cli) by Justin Hileman
>>env('DB_HOST');
=>"db"
>>env('WEB_PORT');
=>null
>>env('DB_PORT');
=>"3306"
>>env('DB_HOSTURL');
=>null
>>env('DB_NAME');
=>null
>>env('DB_USER');
=>null
>>env('DB_PASSWORD');
=>"777"
>>env('DB_ROOT_PASSWORD');
=>null
>> 

US>Version
php —7.4.1
laravel: 6.20.
MySQL version:8.0.28

The docker, docker-compose.yml is available on the following page.

How to create a Ravel+Vue running environment (LEMP environment) with a Docker that never fails~Previous part~

portfolio (start $docker-compose up-d here)
├- docker
│    --php
│    - - Dockerfile
│    - - php.ini
│    --nginx
│    - - Dockerfile
│    │ - default.conf
│    --mysql
│         - - Dockerfile
│         --my.cnf
│
├- src (includes laravel app, config, resumes, .env, etc.)
│    
│ 
│- .env (below)
│- .gitignore   
└- docker-compose.yml (below)

portfolio /.env

WEB_PORT=80
DB_PORT=3306
// db in docker-compose.yml
DB_HOSTURL=db
DB_NAME=sample
DB_USER=hoge
DB_PASSWORD=777
DB_ROOT_PASSWORD=secret

portfolio/src/.env

APP_NAME=docker-larvel-vue
APP_ENV = local
APP_KEY = omitted
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 File Version
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}

# You can also use web_server
  web:
    build:
      context:.
      dockerfile:./docker/nginx/Dockerfile
    ports:
      - ${WEB_PORT}:80
    depend_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      

Additional
src/config/database.php

'mysql'=>[
'driver' = > 'mysql',
'url' = > env('DATABASE_URL'),
'host' = > env('DB_HOST', '127.0.0.1',
'port' = > env('DB_PORT', '3306'),
'database' = > env('DB_DATABASE', 'forge',
'username' = > env('DB_USERNAME', 'forge'),
'password' = > env('DB_PASSWORD', '),
'unix_socket' = > env('DB_SOCKET', '),
'charset' = > 'utf8mb4',
'collation' = > 'utf8mb4_unicode_ci',
'prefix' = >',
'prefix_indexes' = > true,
'strict' = > true,
'engine' = > null,
US>'options'=>extension_loaded('pdo_mysql') ?array_filter([
PDO::MYSQL_ATTR_SSL_CA=>env('MYSQL_ATTR_SSL_CA'),
]) : [],
],

Information you get when you type into the terminal

local mysql details

masa@MasaakinoMacBook-Airportfolio%pwd
/Users/masa/Desktop/portfolio

masa@MasaakinoMacBook-Air portfolio%mysql-uhoge-p sample;
Enter password: 
// Enter '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/orits affiliates.

Oracle is a registered trademark of Oracle Corporation and/orits
affiliate.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: 
// Enter 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/orits affiliates.

Oracle is a registered trademark of Oracle Corporation and/orits
affiliate.Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

phpartisan migrate doesn't work

 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 dancing build cache. Are you sure you want to continue? [y/N] 
Total requested 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#phpartisan config:cache 
Configuration cache cleared!
Configuration cached successfully!
root@8be8ddaa6767:/var/www/html#phpartisan config:clear 
Configuration cache cleared!
root@8be8ddaa6767:/var/www/html#phpartisan cache:clear 
Application cache cleared!
root@8be8ddaa6767:/var/www/html#phpartisan 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_BASE)

  at/var/www/html/vendor/larvel/framework/src/Illuminate/Database/Connection.php:669
    665 | // If an exception occurred 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 installed of just the database's errors.
    668 | catch(Exception$e){
  >669 | through 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/larvel/framework/src/Illuminate/Database/Connectors/Connector.php: 70

  2PDO::__construct("mysql:host=db; port=3306; dbname=sample", "hoge", "777", [ ])
      /var/www/html/vendor/larvel/framework/src/Illuminate/Database/Connectors/Connector.php: 70

  Please use the argument-v to see more details.

php macos docker laravel docker-compose

2022-09-30 22:02

1 Answers

docker-compose logs db results

 portfolio-db-1 | 2022-03-05 10:29:56 + 09:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.37-1 debian 10 started.
portfolio-db-1 | 2022-03-05 10:29:57 + 09:00 [ERROR] [Entrypoint]—mysqld failed while attempting to check config
portfolio-db-1 | command was: mysqld -- verbose --help --log-bin-index=/tmp/tmp.Ef2AJ4XEgw
portfolio-db-1 | 2022-03-05T10:29:57.143207 + 09:00 [ERROR] Unknown collation: 'utf8mb4_0900_ai_ci'
portfolio-db-1 | 2022-03-05T10:29:57.148263 + 09:00 [ERROR] Aborting

'utf8mb4_0900_ai_ci' as shown in , so

(Resolved)Unknown collaboration:utf8mb4_0900_ai_ci


with reference to the site in the docker COLLECTION to 'utf8_general_ci'
CHARACTER is set to 'utf8'
When I changed to

(There is this description in docker/mysql/my.cnf, 'utf8mb4_0900_ai_ci', so I would like to put the code on it↓)

portfolio/docker/mysql/my.cnf (originally)

 [mysqld]
user=mysql
character_set_server=utf8mb4

collation_server=utf8mb4_0900_ai_ci

# timezone
default-time-zone=SYSTEM
log_timestamps=SYSTEM

# Error Log
log-error=mysql-error.log

# Slow Query Log
slow_query_log = 1
slow_query_log_file=mysql-slow.log
long_query_time = 1.0
log_queries_not_using_indexes=0

# General Log
general_log = 1
general_log_file=mysql-general.log

[mysql]
default-character-set=utf8mb4

[client]
default-character-set=utf8mb4

After running docker-compose logs db again, the error message changed as follows:

masa@MasaakinoMacBook-Airportfolio% 
portfolio-db-1 | 2022-03-05 10:58:18 + 09:00 [Note] [Entrypoint]—Entrypoint script for MySQL Server 5.7.37-1 debian 10 started.
portfolio-db-1 | 2022-03-05 10:58:18 + 09:00 [ERROR] [Entrypoint]—mysqld failed while attempting to check config
portfolio-db-1 | command was: mysqld -- verbose --help --log-bin-index=/tmp/tmp.u6hnMzU23j
portfolio-db-1 | 2022-03-05T 10:58:18.873878 + 09:00 [ERROR] COLLATION 'utf8_general_ci' is not valid for CHARACTER SET 'utf8mb4'
portfolio-db-1 | 2022-03-05T 10:58:18.876518 + 09:00 [ERROR] Aborting

'utf8_general_ci' is not valid for CHARACTER 'utf8mb4' so

all in laravel and docker COLLECTION to 'utf8_general_ci'
CHARACTER is set to 'utf8'
When unified to , phpartisan migrate passed.I have solved it.Thank you!🙇♂️


2022-09-30 22:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.