Troubleshooting "SQLSTATE[HY000] [2002] Connection refused" Error in Laravel Homestead
In Laravel Homestead, an error message "SQLSTATE[HY000] [2002] Connection refused" can indicate an issue with database connectivity. This problem manifests as an inability to execute the 'php artisan migrate' command within Homestead.
One potential solution to this error lies in the .env file, which contains database configuration settings. Setting 'DB_HOST' to 'localhost' may resolve the issue:
DB_HOST=localhost DB_DATABASE=tcv DB_USERNAME=homestead DB_PASSWORD=secret
Even if you can connect to the database using a database management tool (such as Sequel Pro) with the original 'DB_HOST' value set to '127.0.0.1', the 'php artisan migrate' command may fail. Modifying the setting to 'localhost' allows Laravel to establish a successful connection.
However, if the error persists even after making this change, further investigation is necessary. Additional factors that could contribute to the issue include firewall settings, network configurations, or mismatches between the database name and user/password credentials. By methodically checking these elements and ensuring their correctness, you can overcome this connectivity error and proceed with the 'php artisan migrate' command successfully.
The above is the detailed content of Why Does \'php artisan migrate\' Fail with \'SQLSTATE[HY000] [2002] Connection refused\' in Laravel Homestead?. For more information, please follow other related articles on the PHP Chinese website!