Laravel Sail Database & User not created
Asked Answered
B

2

8

I just set up a laravel project with laravel sail and for some reason during build process it doesn't create the given database and user. I am quite confused as to why because I feel like I've configured everything right.

When executing ./vendor/bin/sail artisan migrate I get following error which indicates that the user given in my .env either doesn't exist or has wrong credentials:

 Illuminate\Database\QueryException 

  SQLSTATE[HY000] [1045] Access denied for user 'Laravel'@'192.168.0.5' (using password: YES) 
(SQL: select * from information_schema.tables where table_schema = shop and table_name = migrations and table_type = 'BASE TABLE')

Checking my Database as root user showed me that it indeed hasn't got the Laravel user configured:

mysql> SELECT user, host FROM mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| root             | %         |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

After further inspection I found out that not even the database was created:

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

This is my .env:

PP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:nzYXOsqDlSiRuBsaDVcrUiKfQhekjhhpJlq3VKSo0M8=
APP_DEBUG=true
APP_URL=http://domain.test

LOG_CHANNEL=stack
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=shop
DB_ROOT_PASSWORD=
DB_USERNAME=Laravel
DB_PASSWORD=123456
...

This is my docker-compose.yml:

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        ports:
            - '${APP_PORT:-80}:80'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
            # - pgsql
            - redis
            # - selenium
    # selenium:
    #     image: 'selenium/standalone-chrome'
    #     volumes:
    #         - '/dev/shm:/dev/shm'
    #     networks:
    #         - sail
    mysql:
        image: 'mysql:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_ROOT_PASSWORD}'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        volumes:
            - 'sailmysql:/var/lib/mysql'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "mysqladmin", "ping"]
#    pgsql:
#        image: postgres:13
#        ports:
#            - '${FORWARD_DB_PORT:-5432}:5432'
#        environment:
#            PGPASSWORD: '${DB_PASSWORD:-secret}'
#            POSTGRES_DB: '${DB_DATABASE}'
#            POSTGRES_USER: '${DB_USERNAME}'
#            POSTGRES_PASSWORD: '${DB_PASSWORD:-secret}'
#        volumes:
#            - 'sailpostgresql:/var/lib/postgresql/data'
#        networks:
#            - sail
#        healthcheck:
#          test: ["CMD", "pg_isready", "-q", "-d", "${DB_DATABASE}", "-U", "${DB_USERNAME}"]
    redis:
        image: 'redis:alpine'
        ports:
            - '${FORWARD_REDIS_PORT:-6379}:6379'
        volumes:
            - 'sailredis:/data'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "redis-cli", "ping"]
    # memcached:
    #     image: 'memcached:alpine'
    #     ports:
    #         - '11211:11211'
    #     networks:
    #         - sail
    mailhog:
        image: 'mailhog/mailhog:latest'
        ports:
            - '${FORWARD_MAILHOG_PORT:-1025}:1025'
            - '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
        networks:
            - sail
networks:
    sail:
        driver: bridge
volumes:
    sailmysql:
        driver: local
#    sailpostgresql:
#        driver: local
    sailredis:
        driver: local

Of course I could create the database and user myself but since Sail/Docker-Compose ship with these options I want to make them work out of the box when setting up my project with Sail. Does anyone have an idea why the database and user are not created?

Bowerbird answered 6/2, 2021 at 14:10 Comment(2)
not familiar with this syntax. ${FORWARD_DB_PORT:-3306} do you think it is ok?Plaintive
@AbdurRahman I think this means that it'll try reading the value for FORWARD_DB_PORT from ENV and if this doesnt exist it'll default to 3306. I think it's a default feature of bash called parameter-substitution.Bowerbird
B
38

Okay, it seems like I accidentally created a mysql volume by running sail without an .env file, which was persistent the whole time thus of course having no user and database configured.

I executed ./vendor/bin/sail down --rmi all -v to remove all images and volumes and then just ran ./vendor/bin/sail up and it created the images and volumes from scratch. Now everything worked out and I can migrate my data.

Bowerbird answered 6/2, 2021 at 14:51 Comment(6)
This was exactly my problem too! Thanks for sharingBuddhism
So useful for me, not only in this case but also for the cases that I have already created image and I really don't want them. Thank youPendant
Does anybody know what would be the pw when u did not set one :thinking:Francklyn
This was my problem too, thanks!Terminology
So I was able to get it to create the database but for whatever reason it's not creating any of the tables, specifically the migrations table. W/o it I can't run any of the migrations through artisan... I have no idea what's going on. :(Campanulaceous
I needed to do this. I also needed to run ./vendor/bin/sail build --no-cache before sailing up again.Kirwan
S
1

You need the root role to access

  1. stop Sail
  2. set DB_DATABASE= empty
  3. set DB_USERNAME=root
  4. run sail $sail up
  5. run MySQL $sail mysql
  6. done. create update delete user or databases
Scarrow answered 1/6, 2022 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.