Heroku执行php artisan migrate遇SQLSTATE[HY000][2002]错误求修复方案
Hey there, let's dig into why you're hitting these migration errors on Heroku and walk through how to fix them. I've dealt with this exact issue a few times, so let's break it down.
What's Causing These Errors?
First, let's unpack the two variants you're seeing:
1. SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations)
This happens for two key reasons:
- Hardcoded local database name: Your Laravel config is still using your local database name (
laravel) instead of the unique one provided by Heroku. Heroku assigns a custom database name when you add a database add-on, so hardcodinglaravelwon't work here. - Unix socket mismatch: Laravel's default local setup uses a Unix socket for MySQL connections, but Heroku's database instances require a TCP/IP connection. The "No such file or directory" error means the system can't find that local socket file on Heroku's servers.
2. SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = and table_name = migrations)
This is a clear sign your app isn't loading the DB_DATABASE environment variable at all. Either the variable isn't set on Heroku, or cached old configs are preventing your app from reading the latest values.
Step-by-Step Fixes
Let's go through the fixes in order of priority:
1. Verify Heroku's Database Environment Variables
First, confirm Heroku has set up your database correctly. Run this command in your terminal:
heroku config
You should see a DATABASE_URL entry formatted like this:mysql://username:password@host:port/unique_database_name
Heroku automatically sets this when you add a MySQL add-on (like ClearDB). If this entry is missing, add the add-on first with:
heroku addons:create cleardb:ignite
2. Update Your Laravel Database Config
Open config/database.php and ensure your MySQL connection is configured to use Heroku's DATABASE_URL and avoid Unix sockets:
'mysql' => [ 'driver' => 'mysql', 'url' => env('DATABASE_URL'), // Let Laravel parse all DB details from this single URL 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), // Leave empty—Heroku doesn't use local sockets 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'options' => extension_loaded('pdo_mysql') ? array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), // Required for some Heroku MySQL add-ons ]) : [], ],
The 'url' => env('DATABASE_URL') line is critical—it lets Laravel pull all connection details from that single URL, eliminating mismatches between individual variables like DB_DATABASE.
3. Clear Cached Configs on Heroku
You mentioned trying to clear configs, but make sure you're doing it on Heroku's servers (not just locally). Run these commands:
heroku run php artisan config:clear heroku run php artisan cache:clear
Cached configs are a common culprit—if you ran php artisan config:cache locally and pushed that cache to Heroku, it'll ignore new environment variables until you clear it.
4. Remove Hardcoded Database References
Double-check your codebase for any hardcoded mentions of laravel as the database name. This includes migration files, seeders, or custom code. Always use env('DB_DATABASE') or let Laravel handle it via the DATABASE_URL.
5. Test the Database Connection
To confirm your config works, run a quick test with Tinker:
heroku run php artisan tinker
In the Tinker shell, run:
DB::connection()->getPdo();
If this returns a PDO object instead of throwing an error, your connection is working. If it errors, double-check the DATABASE_URL and your config file.
6. Restart Heroku Dynos
Sometimes environment variable changes don't take effect until you restart your dynos. Run:
heroku restart
Quick Recap
- The core issue is mismatched database config between your local environment and Heroku.
- Use Heroku's
DATABASE_URLinstead of hardcoding connection details. - Clear cached configs on Heroku to ensure new settings load.
- Use TCP/IP connections (not Unix sockets) for Heroku databases.
内容的提问来源于stack exchange,提问作者Patrick Obafemi

