Troubleshooting Laravel's "Connection Refused" Error
When deploying Laravel projects to VPS platforms like Digital Ocean, it's common to encounter the infamous "SQLSTATE[HY000] [2002] Connection refused" error. This issue occurs when the application cannot establish a connection to the database.
Cause of the Error
This particular error often stems from an incorrect database configuration in the .env file. The DB_HOST parameter, which designates the hostname or IP address of the database, may be incorrectly set to "127.0.0.1". This address typically refers to the local machine and is not applicable when deploying to a remote server.
Solving the Issue
To resolve the error, navigate to your .env file and adjust the DB_HOST parameter. Replace "127.0.0.1" with "localhost". This modification instructs Laravel to connect to the database hosted on the same server as the application.
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 # Updated to 'localhost' DB_PORT=3306 DB_DATABASE=form DB_USERNAME=root DB_PASSWORD=my pass
Re-attempting Database Migration
After making the necessary changes, rerun the php artisan migrate command to initiate the database migration process. You should no longer encounter the "Connection refused" error, and the migrations will proceed successfully.
The above is the detailed content of Why Does Laravel Show \"Connection Refused\" Error After Deployment?. For more information, please follow other related articles on the PHP Chinese website!