11

根据文档:

此外,当 MySQL 容器启动时,它将确保存在名称与 DB_DATABASE 环境变量的值匹配的数据库。

我的.env文件看起来像这样。

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=

然而,当我尝试通过 运行迁移时sail artisan migrate,我得到了这个错误: SQLSTATE[HY000] [1049] Unknown database 'test' (SQL: select * from information_schema.tables where table_schema = test and table_name = migrations and table_type = 'BASE TABLE')

我试过的:

  • 一起删除 docker 容器和图像。
  • 运行sail build --no-cache(尝试完全重建所有内容)
  • 运行时sail shell,我进入 MySQL 并显示所有数据库。我可以在那里看到默认的 laravel 数据库。

你如何告诉帆创建正确的DB_DATABASE

我的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
            - redis
    mysql:
        image: 'mysql:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_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
    redis:
        image: 'redis:alpine'
        ports:
            - '${FORWARD_REDIS_PORT:-6379}:6379'
        volumes:
            - 'sailredis:/data'
        networks:
            - sail
    mailhog:
        image: 'mailhog/mailhog:latest'
        ports:
            - 1025:1025
            - 8025:8025
        networks:
            - sail
networks:
    sail:
        driver: bridge
volumes:
    sailmysql:
        driver: local
    sailredis:
        driver: local
4

3 回答 3

28

案例 - 重用已创建的 Docker 卷

如果使用 停止 Sail sail down,则数据卷将保留在 Docker 主机上而不会被删除。

当 Sail 停止时,用于sail down -v删除现有的 Docker 卷数据。

首先sail up,DB_DATABASE=forge

首次启动 Sail 时,会在 Docker 主机上创建一个卷。

grep DB_DATABASE .env
DB_DATABASE=forge

docker volume ls
DRIVER    VOLUME NAME

sail up -d
Creating network "test_sail" with driver "bridge"
Creating volume "test_sailmysql" with local driver
Creating volume "test_sailredis" with local driver
Creating test_mailhog_1 ... done
Creating test_mysql_1   ... done
Creating test_redis_1   ... done
Creating test_laravel.test_1 ... done

docker volume ls
DRIVER    VOLUME NAME
local     test_sailmysql
local     test_sailredis

sail mysql
mysql> show databases;
| forge              |

但是,当我退出 Sail 时,Docker 容器被删除,但卷没有被删除。

sail down
Stopping test_laravel.test_1 ... done
Stopping test_mailhog_1      ... done
Stopping test_redis_1        ... done
Stopping test_mysql_1        ... done
Removing test_laravel.test_1 ... done
Removing test_mailhog_1      ... done
Removing test_redis_1        ... done
Removing test_mysql_1        ... done
Removing network test_sail

docker volume ls
DRIVER    VOLUME NAME
local     test_sailmysql
local     test_sailredis

sail up、DB_DATABASE=test

如果您在相同的目录名称中启动第二个 Sail,则已创建的 Docker 卷将被重用。

grep DB_DATABASE .env
DB_DATABASE=test

sail up -d
Creating network "test_sail" with driver "bridge"
Creating test_mysql_1   ... done
Creating test_redis_1   ... done
Creating test_mailhog_1 ... done
Creating test_laravel.test_1 ... done

docker volume ls
DRIVER    VOLUME NAME
local     test_sailmysql
local     test_sailredis

由于数据存在于test_sailmysql第一次运行时创建的卷中,因此不会执行新的数据库创建任务。

sail mysql
  ERROR 1049 (42000): Unknown database 'test'
sail artisan migrate
  Illuminate\Database\QueryException
  SQLSTATE[HY000] [1049] Unknown database 'test' (SQL: select * from information_schema.tables where table_schema = test and table_name = migrations and table_type = 'BASE TABLE')

删除现有卷后启动

sail down -v
...
Removing volume test_sailmysql
Removing volume test_sailredis
sail up -d
...
Creating volume "test_sailmysql" with local driver
Creating volume "test_sailredis" with local driver
sail mysql
mysql> show databases;
| test               |
sail artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (214.30ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (99.56ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (151.61ms)

sail down选项

sail down -h

    -v, --volumes           Remove named volumes declared in the `volumes`
                            section of the Compose file and anonymous volumes
                            attached to containers.
于 2021-01-07T06:51:56.003 回答
-1

您的问题是您创建的数据库没有DB_PASSWORD.env文件中设置,并且还尝试以root用户身份启动它。

如果您在没有密码或DB_USERNAME设置为的情况下启动容器,则分别root运行时会出现以下错误sail up

// Set DB_USERNAME to root will not create the database but you can still query mysql
bracing-mysql-1         | 2022-02-14 04:28:35+00:00 [ERROR] [Entrypoint]: MYSQL_USER="root", MYSQL_USER and MYSQL_PASSWORD are for configuring a regular user and cannot be used for the root user
bracing-mysql-1         |     Remove MYSQL_USER="root" and use one of the following to control the root user password:
bracing-mysql-1         |     - MYSQL_ROOT_PASSWORD
bracing-mysql-1         |     - MYSQL_ALLOW_EMPTY_PASSWORD
bracing-mysql-1         |     - MYSQL_RANDOM_ROOT_PASSWORD

// Did not set DB_PASSWORD results in the user not being created
bracing-mysql-1         | 2022-02-14 04:30:13+00:00 [Warn] [Entrypoint]: MYSQL_USER specified, but missing MYSQL_PASSWORD; MYSQL_USER will not be created

密码错误可能会产生误导,因为即使您设置了密码,MYSQL_ALLOW_EMPTY_PASSWORD: 1docker-compose.yml仍然需要输入密码,否则不会创建用户。

于 2022-02-14T05:12:00.453 回答
-1

如果您已将sai 命令添加到PATH 环境变量...

如果在.env文件所在的项目目录下执行sail命令,则在执行MySQL容器时会创建名为DB_DATABASE的数据库。

https://github.com/laravel/sail/blob/1.x/bin/sail#L66

    # Source the ".env" file so Laravel's environment variables are available...
    if [ -f ./.env ]; then
        source ./.env
    fi

测试 - https://laravel.build/example-app

我尝试了以下示例项目。

你的第一个 Laravel 项目 - https://laravel.com/docs/8.x/installation#getting-started-on-windows

curl -s https://laravel.build/example-app | bash

cd example-app

./vendor/bin/sail up

配置 Bash 别名 - https://laravel.com/docs/8.x/sail#configuring-a-bash-alias

vi ~/.bashrc

alias sail='bash vendor/bin/sail'

我在守护进程模式下运行它并检查了 docker 进程。

sail up -d
sail ps
           Name                         Command               State                       Ports
--------------------------------------------------------------------------------------------------------------------
example-app_laravel.test_1   start-container                  Up      0.0.0.0:80->80/tcp, 8000/tcp
example-app_mailhog_1        MailHog                          Up      0.0.0.0:1025->1025/tcp, 0.0.0.0:8025->8025/tcp
example-app_mysql_1          docker-entrypoint.sh mysqld      Up      0.0.0.0:3306->3306/tcp, 33060/tcp
example-app_redis_1          docker-entrypoint.sh redis ...   Up      0.0.0.0:6379->6379/tcp

我的.env

DB_DATABASE=example_app

MySQL 数据库列表。

sail mysql

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| example_app        |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

数据库迁移也成功。

sail artisan migrate

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (166.17ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (99.25ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (128.87ms)

测试——laravel新项目

Laravel 帆 - https://laravel.com/docs/8.x/sail

laravel new test

cd test

composer require laravel/sail --dev

php artisan sail:install

sail up -d

数据库名称跟在项目名称之后。

grep DB_DATABASE .env
DB_DATABASE=test

数据库迁移也成功。

sail artisan migrate

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (162.35ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (96.68ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (89.60ms)
于 2021-01-06T22:02:09.890 回答