Environment Configuration and Database Setup
Learn how to use the .env file and connect Laravel to a MySQL database securely.
Sophia Green
November 6, 2025
2.4k94
Laravel uses a .env file to store environment-specific settings like database credentials.
Why .env is important
- Keeps secrets out of code
- Easy to change per environment
- Makes deployment safer
Configure database
Open the .env file and update:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=root
DB_PASSWORD=
Create the database manually in MySQL.
Test connection
php artisan migrate
If it runs without errors, your database is connected.
How config flows
flowchart LR
A[.env] --> B[config/database.php]
B --> C[Laravel DB Manager]
C --> D[(MySQL)]
In the next tutorial, we will create tables using migrations instead of writing raw SQL.
#Laravel#Database#Beginner