4

我刚刚用 laravelsai 建立了一个 laravel 项目,由于某种原因,在构建过程中它没有创建给定的数据库和用户。我很困惑为什么,因为我觉得我已经正确配置了一切。

执行时./vendor/bin/sail artisan migrate出现以下错误,表明我的 .env 中给出的用户不存在或凭据错误:

 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配置用户:

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)

经过进一步检查,我发现甚至没有创建数据库:

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:

# 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 设置我的项目时让它们开箱即用。有谁知道为什么没有创建数据库和用户?

4

1 回答 1

20

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

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

于 2021-02-06T14:51:16.920 回答