Troubleshooting "SQLSTATE[HY000] [2002] Connection Refused" Error in Laravel
When deploying a Laravel application to a VPS, you may encounter the "SQLSTATE[HY000] [2002] Connection refused" error. This issue arises primarily due to an incorrect database host configuration.
Cause:
Your .env file specifies the database host as "my ip", which likely represents the server's IP address. By default, Laravel assumes a localhost connection unless explicitly instructed otherwise.
Solution:
To resolve the error, modify your .env file to use "localhost" as the DB_HOST value. Here's the updated .env configuration:
APP_ENV=local APP_KEY=my app key APP_DEBUG=true APP_LOG_LEVEL=debug APP_URL=http://localhost DB_CONNECTION=mysql DB_HOST=localhost DB_PORT=3306 DB_DATABASE=form DB_USERNAME=root DB_PASSWORD=my pass
Explanation:
By setting DB_HOST to "localhost", you instruct Laravel to connect to the MySQL database running locally on the VPS. This ensures that the application can access the database successfully.
Once you modify the .env file, run the following command to recreate the database connection:
php artisan migrate
If the migration command completes without errors, the issue should be resolved.
The above is the detailed content of Here are a few title options for your article, focusing on the question format: * Laravel on VPS: Why am I getting \'SQLSTATE[HY000] [2002] Connection Refused\'? * How to Fix \'SQLSTA. For more information, please follow other related articles on the PHP Chinese website!