According to docs:
Also, when the MySQL container is starting, it will ensure a database exists whose name matches the value of your DB_DATABASE environment variable.
My .env
file looks like this.
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=
Yet when I try to run the migrations via sail artisan migrate
, I get back this error:
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')
What I’ve tried:
- Removing the docker container and images all together.
- Running
sail build --no-cache
(to try and rebuild everything altogether) - When running
sail shell
, I went into MySQL and showed all databases. I could see the default laravel database there.
How do you tell sail to create the correct DB_DATABASE
?
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
- 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
2
Answers
Case – Reusing an already created Docker volume
If you stop Sail with
sail down
, the data volume remains on the Docker host without being deleted.When Sail is stopped, use
sail down -v
to delete the existing Docker volume data.First
sail up
, DB_DATABASE=forgeWhen you first start Sail, a volume is created on the Docker host.
However, when I exit Sail the Docker container is deleted but the volume is not deleted.
Second
sail up
, DB_DATABASE=testIf you start a second Sail in the same directory name, the already created Docker volume will be reused.
Since data exists in
test_sailmysql
, which is the volume created in the first run, a new database creation task is not executed.Start after deleting the existing volume
sail down
optionsYour problem is that you created your database without
DB_PASSWORD
set in your.env
file and also tried to start it asroot
user.If you start your container with no password or with
DB_USERNAME
set toroot
you will get these errors when you runsail up
respectively:The password error can be misleading because even if you set
MYSQL_ALLOW_EMPTY_PASSWORD: 1
in yourdocker-compose.yml
you still need to put a password in otherwise the user is not created.