skip to Main Content

I’m migrating my Laravel 10 project over to Sail. I’m running on Windows 10 and have WSL installed. Everything is working apart from connecting to the database. I keep getting:

SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select * from information_schema.tables where table_schema = domainmonitor_db and table_name = migrations and table_type = ‘BASE TABLE’)

I can login via PHPmyadmin using the username sail and password password, but if i try to manually create a database through there I also get:

#1044 – Access denied for user ‘sail’@’%’ to database ‘domainmonitor1_db’

Here’s my .env setup for database

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3307
FORWARD_DB_PORT=3307
DB_DATABASE=domainmonitor_db
DB_USERNAME=sail
DB_PASSWORD=password

I have tried changing DB_HOST to many variant such as 127.0.0.1, host.docker.internal, localhost, nothing seems to work. What am I missing?

services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.1
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
                NODE_VERSION: '18'
        image: sail-8.1/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - '${APP_PORT:-80}: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}'
            IGNITION_LOCAL_SITES_PATH: '${PWD}'
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mailpit
            - phpmyadmin
            - mysql
    mailpit:
        image: 'axllent/mailpit:latest'
        ports:
            - '${FORWARD_MAILPIT_PORT:-1025}:1025'
            - '${FORWARD_MAILPIT_DASHBOARD_PORT:-8025}:8025'
        networks:
            - sail
    phpmyadmin:
        image: 'phpmyadmin:latest'
        ports:
            - '8080:80'
        networks:
            - sail
        links:
            - mysql
        environment:
            - PMA_ARBITRARY=1
    mysql:
        image: 'mysql/mysql-server:5.7'
        ports:
            - '${FORWARD_DB_PORT:-3307}: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
networks:
    sail:
        driver: bridge
volumes:
    sail-mysql:
        driver: local

2

Answers


  1. Your

    SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select * from information_schema.tables where table_schema = domainmonitor_db and table_name = migrations and table_type = ‘BASE TABLE’)

    strongly suggests that you have domainmonitor_db without the quotes being generated into the query.

    "I can login via PHPmyadmin using the username sail and password password, but if i try to manually create a database through there I also get:"

    #1044 – Access denied for user ‘sail’@’%’ to database ‘domainmonitor1_db’

    Your sail user therefore has not enough privileges. You will therefore need to log in with a user that actually has the privileges you need (root ?) and either do this db creation with this privileged user and grant some privileges to sail, or grant privileges to sail and then reconnect with sail and try again.

    Look at the

            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
    

    settings in your Docker and find out what user is it really referring to, because if it’s a user with higher privilege, then you will need to find out where and how the variables involved above are defined.

    Login or Signup to reply.
  2. Try adding

    container_name: mysql
    

    in the mysql service inside docker-compose.
    Also when you connect your application with the container name of database you should use the internal port of the container i.e. 3306.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search