Kritim Yantra
Jan 08, 2026
A super common beginner experience is: you update the .env file, refresh the page, and boom—database connection error. You start clearing caches, restarting servers, doubting your life choices. I learned this the hard way.
Connecting Laravel to MySQL is actually straightforward—once you know the exact steps and the couple of gotchas that trip people up.
Laravel’s own docs still recommend configuring MySQL primarily through your .env variables, which feed into config/database.php.
Make sure you have:
shop_db)If you’re on your own machine, this is usually done via MySQL CLI, phpMyAdmin, or a GUI like TablePlus.
Here’s the idea (SQL example):
CREATE DATABASE shop_db;
CREATE USER 'shop_user'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON shop_db.* TO 'shop_user'@'localhost';
FLUSH PRIVILEGES;
Why it matters: Laravel can’t connect to a database that doesn’t exist, and a user without permission will fail even if the password is correct.
.env FileOpen your Laravel project’s .env file and set these:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=shop_db
DB_USERNAME=shop_user
DB_PASSWORD=strong_password
Laravel’s installation/docs show this exact MySQL .env structure.
DB_HOST (this trips people up)127.0.0.1 or localhostmysql (depends on your compose file)localhost, sometimes a provided hostdb-xyz.provider.com)A frequent real-world issue is putting a domain name or wrong host and wondering why nothing connects.
Warning: If your
.envvalues are correct but Laravel still behaves like you never changed them, it’s usually a cached config problem (we’ll fix that below).
Run these from your project folder:
php artisan config:clear
php artisan cache:clear
php artisan optimize:clear
Why this matters: Laravel can cache config, and then it keeps using old DB credentials even though your .env file is updated. People hit this a lot when switching from SQLite to MySQL or after deployment.
The easiest beginner-friendly test:
php artisan migrate
If Laravel can connect, it will run migrations successfully (or at least fail for a migration reason—not a connection reason). The Laravel docs explicitly mention creating the DB and running migrations after switching to MySQL.
If you already have tables and don’t want to migrate, you can still do a quick check using Tinker:
php artisan tinker
Then:
DB::select('SELECT 1');
If you get a result back, your connection is working.
Laravel’s database connections live in:
.env (your real settings)config/database.php (reads from .env)In config/database.php, the MySQL section typically includes environment-driven values like:
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'laravel'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
Analogy: Think of .env as the sticky note with your real settings, and config/database.php as the rulebook that says “read the sticky note.”
This usually means DB_DATABASE points to a database name that doesn’t exist.
Fix:
.envThis usually means the username/password is wrong, or the user doesn’t have permission.
Fix:
.env, but Laravel ignores it”This is the config cache issue.
Fix:
php artisan optimize:clear (and friends)Set a strong MySQL password (not root / blank)
Don’t commit .env to Git
In production, run:
php artisan config:cache (after confirming .env is correct)Make sure MySQL is not publicly exposed unless absolutely needed (use firewall rules / private networking)
If you’re adding screenshots to your post, these are the most helpful for beginners:
.env showing the DB variablesphp artisan optimize:clearphp artisan migrate success(These are the points where beginners usually get stuck, so visuals help a lot.)
To connect Laravel (2026-era Laravel 12.x style) to MySQL:
DB_CONNECTION=mysql and the DB_* values in .envoptimize:clear) php artisan migrate If you do only one thing right: clear config cache after editing .env. That’s the hidden “why isn’t it working?” culprit for a lot of people.
localhost or 127.0.0.1 for DB_HOST?Usually either works locally, but some setups resolve them differently. If you’re unsure, start with 127.0.0.1. For hosted databases, use the exact host your provider gives you.
.env but the error didn’t change. Why?Because Laravel might be using cached config. Run:php artisan optimize:clear and try again.
php artisan migrate to connect to MySQL?Not strictly, but it’s an easy test to confirm the connection works. Laravel’s docs also suggest running migrations after switching away from SQLite.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google
Kritim Yantra