'Unable to connect to host when creating separate testing database with Laravel Sail

I want to have a separate testing database for my Laravel application. I'm trying to do this via Sail but it's not working, I'm constantly getting errors like Unable to connect to host or Access denied. I've read the other answers of similar posts on here regarding the same issue so please don't close as a dupe - they haven't worked for me. The only thing I haven't tried is manually creating the DB myself as I want this to be as automated as possible using docker through Sail. Any one have any ideas? I'll share my code below:

docker-compose.yml

mysql:
        image: 'mysql/mysql-server:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}: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:
            - 'sailmysql:/var/lib/mysql'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
            retries: 3
            timeout: 5s
mysql_test:
        image: 'mysql/mysql-server:8.0'
        environment:
            MYSQL_ROOT_PASSWORD: "${DB_PASSWORD}"
            MYSQL_DATABASE: "${DB_DATABASE}"
            MYSQL_ROOT_HOST: "%"
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 1
        networks:
            - sail
        healthcheck:
            test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
            retries: 3
            timeout: 5s

.env.testing

DB_CONNECTION=mysql
DB_HOST=mysql_test
DB_DATABASE=my-db
DB_USERNAME=testuser
DB_PASSWORD=testpassword

Trying to manually log in using Sequel Ace I get:

Unable to connect to host 127.0.0.1, or the request timed out.
Be sure that the address is correct and that you have the necessary privileges, or try increasing the connection timeout (currently 10 seconds).

MySQL said: Can't connect to MySQL server on '127.0.0.1' (61)

Have also tried adding ports to docker-compose as 3307 but nothing works.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source