Laravel SQLSTATE[HY000] [1049] Unknown database 'previous_db_name'
When you run php artisan migrate and encounter a PDOException with the message SQLSTATE[HY000] [1049] Unknown database 'previous_db_name', the immediate instinct is often to panic or assume a complex configuration error. In reality, this specific MySQL error is usually a straightforward symptom of a mismatch between your environment variables and your local filesystem state.
The error code HY000 indicates a general SQL error, while 1049 specifically means MySQL cannot find the database you are trying to connect to. Laravel's migration system operates under the assumption that it is connecting to a fresh or existing database defined in your configuration. If the database.php file points to a connection name like 'mysql', but the underlying .env file defines the actual database name as something different from what MySQL expects, you get this failure.
Looking at the provided database.php configuration:
'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,
],
],
The configuration itself is syntactically correct. It relies entirely on the .env file to resolve the actual values for DB_DATABASE. The error message explicitly states it cannot find previous_db_name. This implies that your .env file likely contains:
DB_DATABASE=previous_db_name
However, your MySQL server (running locally or on a host) simply does not have a database named previous_db_name created yet. Laravel attempts to connect, fails because the database is missing, and throws this exception before it can even attempt to run the migration scripts.
The Root Cause: Missing Database Schema
MySQL requires the target database to exist before you can execute standard SQL commands like CREATE TABLE, unless you are using specific privileges that allow implicit creation (which is not the default behavior for many setups). When Laravel's migration engine tries to drop and recreate tables, it first needs to authenticate against the server. If the authentication succeeds but the USE database_name command fails because the schema doesn't exist, MySQL returns error 1049.
This is distinct from a permission error or a connection timeout. You are authenticated as root (or your specified user), but you cannot access the specific namespace requested.
Resolving the Issue
To fix this, you must ensure that the database named in your .env file actually exists on the MySQL server before running migrations. There are two primary approaches depending on your workflow.
Option 1: Create the Database Manually
If you have a production database name that you intend to use for development (e.g., previous_db_name), you must create it in MySQL first. Connect to your MySQL client using your credentials and run:
CREATE DATABASE previous_db_name CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Once the database exists, ensure your .env file matches this name exactly. Afterward, clear the configuration cache if you have done any caching work that might hold stale configuration data:
php artisan config:clear
php artisan cache:clear
Then retry the migration.
Option 2: Reset to a Fresh State
If previous_db_name is a leftover from a previous experiment and you want to start fresh, simply remove or rename that database in your MySQL client. Then, update your .env file to point to a new, empty database name (e.g., current_db_name). Ensure this new name matches the default fallback in your database.php if your .env is missing the key.
# Remove the old database if you want to start completely clean
mysql -u root -p -e "DROP DATABASE IF EXISTS previous_db_name;"
Update your .env:
DB_DATABASE=current_db_name
Best Practices for Development Environments
A common pitfall in Laravel development is mixing up local and production database names. It is highly recommended to keep a dedicated local database name in your .env file during development to avoid accidentally running migrations against a production database or creating confusion between environments.
Always verify that the connection parameters in your .env match the actual state of your MySQL server. The error SQLSTATE[HY000] [1049] is almost exclusively a "database not found" issue, and resolving it requires ensuring the schema exists on the server side before Laravel attempts to manipulate it. For more robust database management in large-scale applications, consider exploring tools that handle environment-specific database provisioning, such as those discussed at https://laravelcompany.com.