Laravel Sail rebuild default database
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
When working with Laravel Sail on a MySQL container for your database, you might encounter issues when running migrations or performing other database-related tasks. One of these problems could be that the default database does not exist even though it is defined in your environment variables (as shown by
DB_DATABASE=test). Let's delve deeper into the problem and provide possible solutions to overcome this issue.
Firstly, make sure you have removed any legacy Laravel installations or previous database migrations from your project directory. If your project has used a different database before, it might be causing conflicts and preventing Sail from creating the correct database when running sail artisan migrate.
Next, double-check that you've updated all references to the database connection within your application configuration file (`.env`) with the correct information for your new database. This includes changing the DB_CONNECTION, DB_HOST, DB_PORT, and DB_DATABASE variables accordingly.
Assuming your environment is correctly configured for the database, it might be helpful to clean up the MySQL container and rebuild it from scratch when you encounter a problem with an unknown database. To do this:
1. Stop the Laravel Sail docker-compose by running sail stop.
2. Remove any containers and images associated with your current project by executing docker system prune -a. This will delete all unused volumes, networks, images, build cache, and container artifacts.
3. Remove all remaining old-project folders related to previous Laravel installations or database migrations.
4. Rebuild the Laravel Sail docker-compose by running sail up -d. This will reinstall the containers, rebuilding everything from scratch.
5. Run sail build --no-cache to ensure your application is up-to-date and cached assets are refreshed.
6. Finally, run sail artisan migrate again to check if the issue persists.
If the problem still remains, it could be an error within your migration files or an incorrectly configured MySQL container. To verify this, you can go into the MySQL shell using sail shell, then run the command show databases. This will show all existing databases (including the default Laravel database) in the MySQL container.
If no proper database is listed for your application or there's any suspicion of an old database persisting, you can remove it by executing drop database if exists 'db_name', where 'db_name' refers to the name of the affected database (for example, 'test'). Finally, try running sail artisan migrate again.
By following these steps and troubleshooting strategies, you should be able to create, maintain, and manage your database using Laravel Sail effectively. Remember to regularly check for updates on Laravel Company's website, as they provide valuable resources and insights into the latest development practices for Laravel.