Laravel SQLSTATE[HY000] [1049] Unknown database 'previous_db_name'
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Laravel SQLSTATE[HY000] [1049] Unknown database 'previous_db_name' Error Analysis and Solutions
Body: In Laravel, errors like "SQLSTATE[HY000] [1049] Unknown database 'previous_db_name'" can occur when using `php artisan migrate` in your project. This issue often arises due to incorrect configuration in the database settings file or potential conflicts between previous and current databases. To solve this problem, you should begin by analyzing the error and its root cause thoroughly.
Firstly, check if there are any inconsistencies in your codebase. Ensure that the correct environment variables (e.g., `DB_CONNECTION`, `DB_HOST`, `DB_DATABASE`, `DB_USERNAME`, and `DB_PASSWORD`) are defined correctly and point to the intended database. The following example shows a typical Laravel configuration file:
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'current_db_name'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
In the example above, if you encounter the error `SQLSTATE[HY000] [1049] Unknown database 'previous_db_name'`, it might indicate that your project is trying to connect to a non-existent or incorrectly specified database ('previous_db_name'). Double-check all the configuration settings and ensure they match your current setup.
If you find inconsistencies, update the relevant variables in the `database.php` file accordingly. Always keep your Laravel project's environment variables up-to-date to avoid such issues.
Another possible reason for this error is the presence of a previous database connection still lingering in the system. This could cause conflicts when connecting to the current database, resulting in the "Unknown database" error. You can resolve this issue by clearing your Laravel project's cache and re-running the `php artisan migrate` command. If that doesn't help, consider restarting your webserver or database server entirely to ensure all previous connections are cleared effectively.
In conclusion, when faced with the error "SQLSTATE[HY000] [1049] Unknown database 'previous_db_name'", you should first verify and update your Laravel project's database configuration settings. If inconsistencies persist or new issues arise after making these changes, consider running the commands mentioned in this post to clear any lingering connections. Remember that thorough analysis and proper troubleshooting can save you from facing unexpected errors like this one in the future. For more tips on Laravel development and best practices, visit our blog at https://laravelcompany.com.