Laravel 5.6下php artisan migrate无法执行(Mac MAMP环境)
Hey there, let’s work through this Laravel migration issue on MAMP together! That SQLSTATE[HY000] [2002] No such file or directory error usually boils down to Laravel not being able to locate the MySQL socket file MAMP uses. Your earlier attempts are on the right track—let’s refine them with MAMP-specific details to get it working.
Step 1: Confirm MAMP’s MySQL Socket Path
First off, MAMP (non-Pro version) stores its MySQL socket at this default path:/Applications/MAMP/tmp/mysql/mysql.sock
You can double-check this by opening MAMP, going to Preferences > Ports—the socket path might be listed here, or you can verify via MAMP’s MySQL log files if needed.
Step 2: Update Database Configuration
Option A: Tweak the .env File (Cleanest Approach)
Add the socket path directly to your .env file so Laravel pulls it in automatically:
DB_CONNECTION=mysql DB_HOST=localhost DB_PORT=8889 DB_DATABASE=your_database_name DB_USERNAME=root DB_PASSWORD=root DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
Option B: Update config/database.php (If .env Changes Don’t Stick)
If the .env update doesn’t take effect, modify the MySQL connection block in config/database.php to explicitly set the socket:
'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', '8889'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', '/Applications/MAMP/tmp/mysql/mysql.sock'), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ],
Step 3: Clear Laravel’s Config Cache
After making changes, you need to clear Laravel’s cached configuration so it picks up the new settings. Run these commands in your project root:
php artisan config:clear php artisan cache:clear
Step 4: Double-Check MAMP’s MySQL Status
Don’t forget to confirm MAMP’s MySQL server is running! Open MAMP and make sure the MySQL indicator is green—it’s easy to overlook a stopped server.
Bonus: Symlink the Socket (Alternative Fix)
If you’d rather keep using /tmp/mysql.sock like you initially tried, create a symlink from MAMP’s socket to the /tmp directory. Run this in your terminal (you might need sudo for permissions):
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
This lets Laravel find the socket at the path you first attempted.
After trying these steps, run php artisan migrate -v again—it should connect to your MAMP MySQL database without issues!
内容的提问来源于stack exchange,提问作者Blaze Rowland

