0

I have another MySQL 5.7 container. I wanted the MySQL 8 that comes with Laravel 9 to use port 3308 instead of 3306 so I have this .env:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:yiU0J8TWntz0c20bwsC1D/LqyZ6Hc+/BUczEiGYfPxM=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3308
DB_DATABASE=store
DB_USERNAME=root
DB_PASSWORD=

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

MEMCACHED_HOST=memcached

REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6380

MAIL_MAILER=smtp
MAIL_HOST=mailhog
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_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}"

SCOUT_DRIVER=meilisearch
MEILISEARCH_HOST=http://meilisearch:7700

and docker-compose.yml:

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.2
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.2/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - '8088:80'
            - '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
            - redis
            - meilisearch
            - mailhog
            - selenium
    mysql:
        image: 'mysql/mysql-server:8.0'
        ports:
            - '3308:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ROOT_HOST: '%'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            #MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 1
        volumes:
            - 'sail-mysql:/var/lib/mysql'
            - './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
            retries: 3
            timeout: 5s
    redis:
        image: 'redis:alpine'
        ports:
            - '6380:6379'
        volumes:
            - 'sail-redis:/data'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "redis-cli", "ping"]
            retries: 3
            timeout: 5s
    meilisearch:
        image: 'getmeili/meilisearch:latest'
        ports:
            - '${FORWARD_MEILISEARCH_PORT:-7700}:7700'
        volumes:
            - 'sail-meilisearch:/meili_data'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "wget", "--no-verbose", "--spider",  "http://localhost:7700/health"]
            retries: 3
            timeout: 5s
    mailhog:
        image: 'mailhog/mailhog:latest'
        ports:
            - '${FORWARD_MAILHOG_PORT:-1025}:1025'
            - '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
        networks:
            - sail
    selenium:
        image: 'selenium/standalone-chrome'
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        volumes:
            - '/dev/shm:/dev/shm'
        networks:
            - sail
networks:
    sail:
        driver: bridge
volumes:
    sail-mysql:
        driver: local
    sail-redis:
        driver: local
    sail-meilisearch:
        driver: local

However, trying ./vendor/bin/sail php artisan migrate leads to MySQL error 2002:

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = store and table_name = migrations and table_type = 'BASE TABLE')

What is the issue there and how could I solve it.

Update (1)

In .env I changed The DB_HOST and DB_PORT to:

DB_HOST=store-mysql-1
DB_PORT=3306
DB_DATABASE=store
DB_USERNAME=sail

Where store-mysql-1 is the name of MySQL8 container of Laravel. I got the following Error:

SQLSTATE[HY000] [1045] Access denied for user 'sail'@'172.20.0.7' (using password: NO) (SQL: select * from information_schema.tables where table_schema = store and table_name = migrations and table_type = 'BASE TABLE')

The most important notice here, the IP 172.20.0.7 is not the IP of the container named `store-mysql-1 which I could determine it using the command:

docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' store-mysql-1 and it returns: 172.20.0.2

Update (2)

Using the command:

$ ./vendor/bin/sail mysql

returns the error:

ERROR 1045 (28000): Access denied for user 'sail'@'localhost' (using password: NO)

Update (3) the solution

Using the following command I removed the images and then run sail up again, everything is solved with the same settings of of both .env and docker-compose.yml files except, to set a password in .env DB_PASSWORD=pwd123+ and uncomment #MYSQL_PASSWORD: '${DB_PASSWORD}' in docker-compose.yml:

./vendor/bin/sail down --rmi all -v

However, I saw this solution somewhere in this community but I have no idea why does it solve the issue and what is the root cause of it?

SaidbakR
  • 13,303
  • 20
  • 101
  • 195

1 Answers1

1

The .env must be using the docker composer container name, that is how the container knows where to point to (host), so DB_HOST must be mysql literally, because that is the one you want (MySQL 8).

And the DB_PORT must be equal to the internal port 3306. You are exposing 3308 to outside connections, but internally is still using 3306.

Then, whatever command you execute, it must be run inside the PHP container, I think sail already does this for you, but if you want to manually go inside the PHP container just do docker compose exec laravel.test bash

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
  • Please, checkout the **Update (1)**. – SaidbakR Jan 17 '23 at 22:46
  • @SaidbakR can you also update the docker compose file? I do not see any `store-mysql-1` in there – matiaslauriti Jan 17 '23 at 23:02
  • I updated the docker composer, but the same. The second IP is for the `store-laravel.test-1` container and I think it has MySQL client inside it. – SaidbakR Jan 17 '23 at 23:07
  • I have no idea what you are doing, you should not care at all about the IP, just the container name, that is why I am asking you to update the question with the new docker compose file so I can see what image you are using and all of that as there seems to be something missing on that container declaration – matiaslauriti Jan 17 '23 at 23:36
  • The docker-compose file is still the same as regarded in the question. Changes have been done on the .env only. – SaidbakR Jan 18 '23 at 14:14
  • You do not have any `store-mysql-1` container defined in your docker compose file... If it is outside the file network, you must use the same network... – matiaslauriti Jan 18 '23 at 15:09
  • No, the docker container named `store-mysql-1` is always there. However, take a look at the **Update (3)** that solves the issue, but I don't know why! – SaidbakR Jan 18 '23 at 18:42
  • Again, in your docker compose there is no `store-mysql-1` (at least in your question), second: you have uncommented `MYSQL_PASSWORD` because you don't need a password... see that you have `MYSQL_ALLOW_EMPTY_PASSWORD: 1`, so the `MYSQL_USER` does not require a password (`DB_PASSWORD=` literally when trying to connect)... – matiaslauriti Jan 18 '23 at 19:04