问题描述
根据文档:
此外,当 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
解决方法
案例 - 重用已创建的 Docker 卷
如果使用 sail down
停止 Sail,数据卷将保留在 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.
,
如果你已经在PATH环境变量中添加了sail命令...
如果在.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 Sail - 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)