skip to Main Content

Laravel: 9.2
Mac OS: 12.3.1
Docker Desktop: 4.5.0

I have installed a default Laravel project using sail. My docker-compose.yml file includes the following:

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:
            - '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

And my default environment variables are:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=example_2012
DB_USERNAME=sail
DB_PASSWORD=password

But when I try to connect to the database from sail artisan and http://localhost. I receive the following error:

Host 'XXX.XXX.X.X' is not allowed to connect to this MySQL server"

"docker ps -a | grep mysql" returns:

XXXXXXXXXXXX   mysql/mysql-server:8.0        "/entrypoint.sh mysq…"   3 minutes ago   Up 3 minutes (healthy)      33060-33061/tcp, 0.0.0.0:3308->3306/tcp                example-2022_mysql_1

I’ve tried amending the ports to 3307 but without success.

2

Answers


  1. According to your output it should be port 3308 which is forwarded to 3306 in the container

    Login or Signup to reply.
  2. I just ran into this issue with a MariaDB container. While searching for a solution I came across this Github answer. Long story short:

    docker-compose down -v
    docker volume prune
    docker container prune
    sail up 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search