Fix "SQLSTATE[HY000] [1049] Unknown database" in Laravel

![Fix "SQLSTATE[HY000] [1049] Unknown database" in Laravel](https://nabilhassen.com/storage/post-images/01K4ASZQ0XWVNG4FBDHTW714GM.png)
- Fixing SQLSTATE[HY000] [1049] Unknown Database in Laravel — Deep Dive
- Why Laravel Might Throw This Error
- Step-by-Step Laravel‑centric Fixes
- How to Solve
SQLSTATE[HY000] [1049] Unknown Database
in Laravel — Deep Dive
Fixing When working with Laravel, one error you might see is: SQLSTATE[HY000] [1049] Unknown database 'mydatabase'
This indicates that the database Laravel is configured to use can’t be found by MySQL/MariaDB. Below is a more robust guide, combining community wisdom, common pitfalls, and specific Laravel‑centric fixes.
Why Laravel Might Throw This Error
Here are recurring causes in Laravel setups:
-
Default database leftover in config Laravel’s
config/database.php
often sets defaults like:'database' => env('DB_DATABASE', 'laravel'),'username' => env('DB_USERNAME', 'laravel'),If your
.env
isn’t loaded or config is cached, Laravel may fall back tolaravel
and attempt to connect to it. -
Cached or stale configuration Changing
.env
isn’t enough if the config is cached. You must clear Laravel’s config cache:php artisan cache:clearphp artisan config:cacheDoing this forces Laravel to re‑read
.env
and refresh its internal settings. -
Wrong
.env
settings / port mismatch If your MySQL server is running on a nonstandard port (e.g. 3308 instead of 3306), Laravel’sDB_PORT
must match. Also, double-check for typos, stray spaces, or line breaks in the.env
file. -
Database not created or dropped If the configured database doesn’t exist on the server, Laravel will not be able to connect. Creating the database usually resolves the issue.
Step-by-Step Laravel‑centric Fixes
Here’s a checklist you can follow to resolve this reliably:
Step | Action | Why / Notes |
---|---|---|
1 | Open your .env and verify database details |
Ensure DB_DATABASE , DB_USERNAME , DB_PASSWORD , DB_HOST , DB_PORT are exactly correct and match your MySQL setup |
2 | Create the database if missing | CREATE DATABASE my_app_db; via MySQL client or admin UI |
3 | Clear Laravel’s cache and re‑cache config | php artisan cache:clear and then php artisan config:cache , this ensures Laravel reads the new settings |
4 | Restart the development server / services | If running via php artisan serve or Docker, restart so changes apply |
5 | Validate port and host match | If MySQL is listening on a non‑default port, your DB_PORT must reflect it |
6 | Grant necessary privileges to the DB user | GRANT ALL PRIVILEGES ON my_app_db.* TO 'my_user'@'localhost'; FLUSH PRIVILEGES; |
7 | Run migrations | php artisan migrate to boot up your schema |
How to Solve
-
Correct
.env
DB_CONNECTION=mysqlDB_HOST=127.0.0.1DB_PORT=3306 # or whatever your MySQL usesDB_DATABASE=my_app_dbDB_USERNAME=my_userDB_PASSWORD=secret -
Ensure the database exists
CREATE DATABASE my_app_db; -
Flush privileges if needed
GRANT ALL PRIVILEGES ON my_app_db.* TO 'my_user'@'localhost';FLUSH PRIVILEGES; -
Clear Laravel cache and reload config
php artisan cache:clearphp artisan config:cache -
Restart or reload your server / environment If using
php artisan serve
or Docker, stop and start it again. -
Check for config overrides Search for duplicate
.env
files or environment‐specific config paths that might override your intended settings. -
Run migrations
php artisan migrate
If after doing all these steps, you still see the error, double-check for stray spaces or hidden characters in your .env
keys.
Stay Updated.
I'll you email you as soon as new, fresh content is published.
Latest Posts