14 days ago
Hello everyone! I'm struggling with a Laravel deployment and would be incredibly grateful for any insights.
The Problem:
My Laravel service consistently fails to connect to the managed MySQL service with a SQLSTATE[HY000] [2002] Connection refused error. This happens during the php artisan migrate step in my startup script.
What I've already tried and confirmed:
Network Reachability is OK: My entrypoint script uses netcat to wait for the database port, and it successfully connects. The deploy log clearly shows:
Generated code
Connection to mysql.railway.internal:3306 ... succeeded!
Use code with caution.
The connection is refused only at the application (PDO) level, after the network path is confirmed to be open.
Environment Variables are Correct:
I've double- and triple-checked that DB_HOST, DB_PORT, DB_USERNAME, DB_PASSWORD, and DB_DATABASE in my Laravel service perfectly match the credentials provided by the MySQL service.
I've also tried using the full DATABASE_URL string directly, with the same result.
SSL/TLS:
We suspected an SSL issue. We tried setting DB_SSLMODE=REQUIRED and also MYSQL_ATTR_SSL_CA=/etc/ssl/certs/railway.pem. Neither resolved the issue.
Docker Setup:
The container builds successfully. We are using a multi-stage Dockerfile.
The final image runs an entrypoint.sh script that prepares the storage volume and then starts php-fpm and nginx.
The entrypoint.shsuccessfully creates the storage/logs/laravel.log file, but because the migrate command fails, the container never reaches a healthy state, so I can't inspect the file afterwards.
Here is the simplified entrypoint.sh that demonstrates the issue:
Generated bash
#!/bin/sh
set -e
# Wait for DB
while ! nc -z -w2 $DB_HOST $DB_PORT; do sleep 2; done
echo "Database is ready!"
# This next line is where it fails
php artisan migrate --force
# These lines are never reached
php-fpm &
nginx -g "daemon off;"
Use code with caution.Bash
I'm out of ideas. It feels like there's a specific configuration or policy in the Railway environment that's blocking the PDO connection, even though the port is open. Has anyone encountered something similar?
Any help would be massively appreciated!
2 Replies
6 days ago
What happens if you SSH in and run php artisan migrate
?
6 days ago
Saw this mentioned in another place might be worth trying Railway pre-deploy command to handle the migration step https://docs.railway.com/guides/pre-deploy-command
Could be a cleaner way to run it before the service fully spins up