Home > Back-end >  Cannot access mysql database after migrating from Homestead to Sail
Cannot access mysql database after migrating from Homestead to Sail

Time:05-11

I am trying to migrate a Laravel 8 project from Homestead to Sail. Sail seems to be set up correctly since I can get to my project's website locally. But I can't get into mysql to look around in the database:

➜  myproject git:(master) ✗ sail mysql
ERROR 1045 (28000): Access denied for user 'myproject'@'localhost' (using password: YES)

I have tried restarting Docker, and restarting my terminal and running sail build --no-cache && sail up to no avail.

Here is the relevant portion of my .env file:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=myproject
DB_USERNAME=myproject
DB_PASSWORD=secret

And here is the docker-compose.yml file:

# 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:-80}:80'
            - '${HMR_PORT:-8080}:8080'
        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:-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
networks:
    sail:
        driver: bridge
volumes:
    sail-mysql:
        driver: local

EDIT: When I change my .env to

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

Then the error is

➜  myproject git:(master) ✗ sail mysql
ERROR 1044 (42000): Access denied for user 'sail'@'%' to database 'myproject'

CodePudding user response:

I Just tried to connect to a fresh laravel 9 app and I think you only need to change a few lines in order to connect to Sail's Mysql:

.env

DB_CONNECTION=mysql 
DB_HOST=mysql # <-- Default "mysql" for sail NOT locahost or 127.0.0.1
DB_PORT=3306
DB_DATABASE=myproject 
DB_USERNAME=sail # <-- "sail" as default user name
DB_PASSWORD=password # <-- "password" as default password

This is needed because "mysql" is the name of the instance of the database section in the docker container that sail uses to interact with the database.

All other settings still the same.

Then as you do before sail build --no-cache && sail up.

  • Related