dockerdocker-composelaravel-9mysql-8.0laravel-sail

Laravel 9 Sail port forward error for mysql


I have port forwarded applications mysql port to 3307 because I need my host mysql to keep running at 3306, but it gives below error.

Also I am able to get welcome page after running sail up

I am using laravel 9 latest version

Error

Illuminate\Database\QueryException

PHP 8.1.9
9.26.1

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed: Temporary failure in name resolution

SELECT count(*) AS aggregate FROM `users` WHERE `email` = test@test.com

.env

APP_URL=http://127.0.0.1
APP_PORT=81

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
FORWARD_DB_PORT=3307

docker-composer.yml

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.1
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.1/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - '${APP_PORT:-81}:80'
            - '${VITE_PORT:-5174}:${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
    mysql:
        image: 'mysql/mysql-server:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3307}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '{DB_PASSWORD}'
            MYSQL_ROOT_HOST: '{DB_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
networks:
    sail:
        driver: bridge
volumes:
    sail-mysql:
        driver: local

Update 1

My terminal ouput is as follows

sm_v2-laravel.test-1   "start-container"   laravel.test        exited (0)          
Shutting down old Sail processes...
[+] Running 0/1
 ⠙ Network sm_v2_sail  Creating                                                                                      0.2s
[+] Running 3/3d orphan containers ([sm_v2-service-1]) for this project. If you removed or renamed this service in your co ⠿ Network sm_v2_sail              Created                                                                           0.2s
 ⠿ Container sm_v2-mysql-1         Created                                                                           1.5s
 ⠿ Container sm_v2-laravel.test-1  Created                                                                           0.5s
Attaching to sm_v2-laravel.test-1, sm_v2-mysql-1
sm_v2-mysql-1         | [Entrypoint] MySQL Docker Image 8.0.30-1.2.9-server
sm_v2-mysql-1         | [Entrypoint] Starting MySQL 8.0.30-1.2.9-server
sm_v2-mysql-1         | 2022-08-30T15:19:04.087084Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
sm_v2-mysql-1         | 2022-08-30T15:19:04.092964Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.30) starting as process 1
sm_v2-mysql-1         | 2022-08-30T15:19:04.148193Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
sm_v2-mysql-1         | 2022-08-30T15:19:04.303213Z 1 [ERROR] [MY-012960] [InnoDB] Cannot create redo log files because data files are corrupt or the database was not shut down cleanly after creating the data files.
sm_v2-mysql-1         | 2022-08-30T15:19:04.755173Z 1 [ERROR] [MY-010334] [Server] Failed to initialize DD Storage Engine
sm_v2-mysql-1         | 2022-08-30T15:19:04.755609Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
sm_v2-mysql-1         | 2022-08-30T15:19:04.755681Z 0 [ERROR] [MY-010119] [Server] Aborting
sm_v2-mysql-1         | 2022-08-30T15:19:04.757223Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.30)  MySQL Community Server - GPL.
sm_v2-mysql-1 exited with code 1
sm_v2-laravel.test-1  | 2022-08-30 15:19:07,746 INFO Set uid to user 0 succeeded
sm_v2-laravel.test-1  | 2022-08-30 15:19:07,751 INFO supervisord started with pid 1
sm_v2-laravel.test-1  | 2022-08-30 15:19:08,756 INFO spawned: 'php' with pid 16
sm_v2-laravel.test-1  | 2022-08-30 15:19:09,759 INFO success: php entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
sm_v2-laravel.test-1  | 
sm_v2-laravel.test-1  |    INFO  Server running on [http://0.0.0.0:80].  
sm_v2-laravel.test-1  | 
sm_v2-laravel.test-1  |   Press Ctrl+C to stop the server
sm_v2-laravel.test-1  | 
sm_v2-laravel.test-1  |   2022-08-30 15:19:21 ................................................... ~ 1s
sm_v2-laravel.test-1  |   2022-08-30 15:19:23 /favicon.ico ...................................... ~ 0s
sm_v2-laravel.test-1  |   2022-08-30 15:19:23 ................................................... ~ 1s
sm_v2-laravel.test-1  |   2022-08-30 15:19:24 /build/assets/app.ac81e540.css .................... ~ 0s
sm_v2-laravel.test-1  |   2022-08-30 15:19:24 /build/assets/app.ab93cf8a.js ..................... ~ 0s
sm_v2-laravel.test-1  |   2022-08-30 15:19:24 /favicon.ico ...................................... ~ 0s
sm_v2-laravel.test-1  |   2022-08-30 15:19:27 ................................................... ~ 0s
sm_v2-laravel.test-1  |   2022-08-30 15:19:29 /favicon.ico ...................................... ~ 0s
    sm_v2-laravel.test-1  |   2022-08-30 16:07:14 ................................................... ~ 0s

Update 2

I get different error now

SQLSTATE[HY000] [1045] Access denied for user 'root'@'192.168.128.3' (using password: YES)

Solution

  • I finally solved it after mental frustation of more than a week. But it is very strange that no one was able to provide any answer in any forums, yes I tried all the famous forums possible.

    I made sure that two users are added on my host(main computer) machine not the docker mysql, and I granted them full grant using mysql cli, there were 2 entries like these along with other entries

    root | %
    root | localhost
    

    I ran following commands one after another. I don't know which commands exactly solved the problem as I am a beginner in docker and sail but here are my steps that I tried after which it started working.

    I was getting Docker is not running. , so I tried following to make docker running.

    sudo systemctl enable docker.service
    sudo systemctl enable docker.socket
    

    After that I tried sail up but it did not work, so ran following

    sudo systemctl stop docker
    sudo systemctl start docker
    sudo systemctl disable docker.service
    sudo systemctl enable docker.service
    sail up
    

    After that I rebooted my computer (I am on Ubuntu 22.04)

    reboot
    

    Removed some unnecessary files, also I got some failed error in docker service which I solved by running line 2&3 of the code below

    sudo rm /etc/systemd/system/docker.service.d/override.conf
    sudo systemctl reset-failed docker.service
    sudo systemctl start docker.service
    systemctl daemon-reload
    sudo systemctl start docker.service
    
    sail down
    sail build --no-cache
    sail up
    
    php artisan config:clear
    

    After that I migrated database and it worked

    sail artisan migrate
    

    After that

    sudo systemctl enable docker
    sail up
    sail build
    sail ps
    sudo usermod -aG docker ${USER}
    

    Removed daemon.json

    sudo rm daemon.json
    

    Removed old volumes

    I think this was helpful

    sail down --rmi all -v
    sail up / (you can use sail up --no-cache)
    

    Now mysql works on host computer port 3306 as well as other ports used for docker 3307,3308 simultaneously

    I Appreciate @Mihai effort becoz only @Mihai responded in the comments

    Update 2

    I had to add platform: 'linux/x86_64' in docker-compose.yml file

    mysql:
            image: 'mysql/mysql-server:8.0'
            platform: 'linux/x86_64'
            ports:
                - '${FORWARD_DB_PORT:-3307}:3306'