🎉🎉 Larasense is officially launched 🎉🎉
- Your Hub for Laravel News, Trends & Updates

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

database
errors
Nabil Hassen
Nabil Hassen
Sep 4, 2025
Fix "SQLSTATE[HY000] [1049] Unknown database" in Laravel
Last updated on Sep 25, 2025
Table of contents:

Fixing SQLSTATE[HY000] [1049] Unknown Database in Laravel — Deep Dive

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 to laravel 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:clear
    php artisan config:cache

    Doing 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’s DB_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

  1. Correct .env

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306 # or whatever your MySQL uses
    DB_DATABASE=my_app_db
    DB_USERNAME=my_user
    DB_PASSWORD=secret
  2. Ensure the database exists

    CREATE DATABASE my_app_db;
  3. Flush privileges if needed

    GRANT ALL PRIVILEGES ON my_app_db.* TO 'my_user'@'localhost';
    FLUSH PRIVILEGES;
  4. Clear Laravel cache and reload config

    php artisan cache:clear
    php artisan config:cache
  5. Restart or reload your server / environment If using php artisan serve or Docker, stop and start it again.

  6. Check for config overrides Search for duplicate .env files or environment‐specific config paths that might override your intended settings.

  7. 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.

Nabil Hassen
Nabil Hassen
Full Stack Web Developer

Stay Updated.

I'll you email you as soon as new, fresh content is published.

Thanks for subscribing to my blog.

Latest Posts