Laravel Sail 重建默认数据库
Laravel Sail rebuild default database
根据文档:
Also, when the MySQL container is starting, it will ensure a database exists whose name matches the value of your DB_DATABASE environment variable.
我的 .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 数据库。
如何告诉 sail 创建正确的 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
如果您已经将 sail 命令添加到 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)
案例 - 重复使用已创建的 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=测试
如果您在相同的目录名称中启动第二个 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.
您的问题是您创建数据库时没有在 .env
文件中设置 DB_PASSWORD
,并且还尝试以 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
密码错误可能会产生误导,因为即使您在 docker-compose.yml
中设置了 MYSQL_ALLOW_EMPTY_PASSWORD: 1
,您仍然需要输入密码,否则不会创建用户。
根据文档:
Also, when the MySQL container is starting, it will ensure a database exists whose name matches the value of your DB_DATABASE environment variable.
我的 .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 数据库。
如何告诉 sail 创建正确的 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
如果您已经将 sail 命令添加到 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)
案例 - 重复使用已创建的 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=测试
如果您在相同的目录名称中启动第二个 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.
您的问题是您创建数据库时没有在 .env
文件中设置 DB_PASSWORD
,并且还尝试以 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
密码错误可能会产生误导,因为即使您在 docker-compose.yml
中设置了 MYSQL_ALLOW_EMPTY_PASSWORD: 1
,您仍然需要输入密码,否则不会创建用户。