Laravel Sail数据库&用户未创建 [英] Laravel Sail Database & User not created

查看:312
本文介绍了Laravel Sail数据库&用户未创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是用laravel sail设置了一个laravel项目,由于某种原因,在构建过程中它没有创建给定的数据库和用户.我对为什么感到很困惑,因为我觉得自己已经正确配置了一切.

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.

执行 ./vendor/bin/sail artisan migration 时,出现以下错误,指示我的.env中指定的用户不存在或凭据错误:

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

以root用户身份检查数据库时,我确实没有配置 Laravel 用户:

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)

这是我的.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
...

这是我的docker-compose.yml:

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

我当然可以自己创建数据库和用户,但是由于Sail/Docker-Compose附带了这些选项,因此我想在使用Sail设置项目时使它们开箱即用.有谁知道为什么不创建数据库和用户?

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?

推荐答案

好吧,似乎我在没有.env文件的情况下通过运行sail意外创建了一个mysql卷,该文件一直存在,因此当然没有用户和数据库已配置.

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.

我执行了 ./vendor/bin/sail down --rmi all -v 来删除所有图像和卷,然后只运行了 ./vendor/bin/sail up ,并从头开始创建图像和卷.现在一切都解决了,我可以迁移数据了.

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.

这篇关于Laravel Sail数据库&用户未创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆