Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Sail Database & User not created

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://ak-wear.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=ak123456
...

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?

like image 846
cscholz Avatar asked Dec 01 '22 08:12

cscholz


2 Answers

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.

like image 124
cscholz Avatar answered Dec 04 '22 00:12

cscholz


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
like image 24
Alaa Aqeel Avatar answered Dec 04 '22 00:12

Alaa Aqeel