Laravel deployment issues: why is my database not connecting?
hey everyone, hope you're all doing well. i was on a previous thread about my laravel deployment acting up, and thanks to some great advice, i got most of it sorted.
but now i'm stuck on a new, super frustrating problem โ my database isn't connecting on the live server. i'm a total noob with this stuff, so please bear with me.
the app itself deployed fine, and the front end loads, but any page that tries to fetch data from the database just throws an error. it's one of those 'SQLSTATE[HY000] [2002] connection refused' type errors.
this wasn't happening locally, so it's definitely something with the server setup or my laravel deployment issues.
here's what i've tried so far, based on some quick google searches:
i double-checked my
.envfile on the server. theDB_HOST,DB_DATABASE,DB_USERNAME, andDB_PASSWORDare all set correctly (i think?). i even regenerated the app key.ran
php artisan config:clearandphp artisan cache:clearmultiple times. no luck.checked the server's firewall settings โ made sure port 3306 (for MySQL) is open. it seems to be.
i tried to connect to the database directly from the server's terminal using the same credentials, and that works! so the database itself is running and accessible from the server.
looked at the laravel logs (
storage/logs/laravel.log) but they just show the connection refused error, not much more detail.
what am i missing here? it feels like a really basic server config thing i'm just not aware of.
are there any specific permissions i need to set for laravel to connect to the database, maybe on the .env file itself or the storage directory?
could it be something with php-fpm or nginx not passing the environment variables correctly?
any tools or commands i can use to debug the exact point of failure?
this is really holding back my launch, so please, any help would be amazing. help a brother out please...
2 Answers
Vikram Singh
Answered 1 week agobut now i'm stuck on a new, super frustrating problem โ my database isn't connecting on the live server. i'm a total noob with this stuff, so please bear with me.
Hello Ling Kim,
I completely understand how frustrating this "connection refused" error can be. I've been in your shoes more times than I care to count, especially with new Laravel deployments. It's a classic server configuration headache that often comes down to environment variables or PHP module issues.
You've already done some solid troubleshooting by checking the .env, clearing caches, and verifying direct database access. That last point, specifically, is critical: if you can connect from the server's terminal with the same credentials, the database itself is healthy and reachable from the server machine. This shifts our focus squarely onto how your Laravel application, via PHP-FPM/Nginx or Apache, is interpreting and using those credentials.
Hereโs a structured approach to debug and resolve these Laravel deployment issues:
1. Deep Dive into Environment Variables and Caching
-
Ensure
.envis Correctly Loaded: Even if you "think" they're correct, verify these points:- Is the
.envfile actually present in the root directory of your Laravel application on the server? - Permissions: The
.envfile should typically have permissions like644or640, and be owned by the web server user (e.g.,www-dataornginx). - Absolute Paths for DB_HOST: Sometimes,
localhostresolves differently depending on the system'shostsfile. Try explicitly settingDB_HOST=127.0.0.1instead oflocalhost.
- Is the
-
Configuration Caching: You ran
php artisan config:clear, which is good. However, if you've previously runphp artisan config:cache, Laravel will ignore your.envfile changes and use the cached configuration.- Always run
php artisan config:clearfirst. - Then, if you want to optimize for production, run
php artisan config:cache. But during debugging, it's safer to avoid caching the configuration until everything works. - Also, clear the application cache:
php artisan cache:clearandphp artisan view:clear.
- Always run
2. PHP Modules and FPM Configuration
-
PHP MySQL Extension: Your PHP environment needs the correct MySQL extension.
- For modern PHP versions, this is usually
php-mysql(orphp-pdo_mysql). - Check if it's installed and enabled for the specific PHP version your web server (Nginx/Apache) is using with FPM. You can verify this by creating a
phpinfo.phpfile (e.g.,<?php phpinfo(); ?>) in your public directory and accessing it via browser. Look for a 'mysql' or 'pdo_mysql' section. - If missing, install it:
sudo apt install php8.x-mysql(replace8.xwith your PHP version) and restart PHP-FPM (e.g.,sudo systemctl restart php8.x-fpm).
- For modern PHP versions, this is usually
-
PHP-FPM Environment Variables: This is a common culprit. PHP-FPM pools don't always inherit environment variables directly from the shell where you run commands.
- Check your PHP-FPM pool configuration file (e.g.,
/etc/php/8.x/fpm/pool.d/www.confor similar). - Look for lines like
env[DB_HOST] = 127.0.0.1. If you're explicitly setting variables here, ensure they match your.envor are overridden. Generally, it's better to let Laravel read its own.env, but FPM can sometimes interfere. - Ensure
clear_env = nois set in your FPM pool config if you want FPM to inherit environment variables from the server. However, this is less secure and less common in modern setups. Laravel should read its own.env.
- Check your PHP-FPM pool configuration file (e.g.,
3. File Permissions and Ownership
-
Storage Directory: Laravel needs write access to its
storageandbootstrap/cachedirectories.- Run these commands from your Laravel project root:
sudo chown -R www-data:www-data storage bootstrap/cache sudo chmod -R 775 storage bootstrap/cache - Replace
www-datawith your web server user if it's different (e.g.,nginxfor Nginx on some systems).
- Run these commands from your Laravel project root:
4. Network and Firewall
- Database Server Firewall: Even if port 3306 is open on the *application* server, ensure that the *database* server (if it's a separate machine) allows connections from your application server's IP address. This is less likely if the database is on the same machine, but worth a quick check for remote DB setups.
5. Advanced Debugging
-
Dump Environment Variables: Temporarily add
dd(env('DB_DATABASE'));(orDB_HOST, etc.) at the very top of yourconfig/database.phpfile or in a controller that's causing the error. This will show you exactly what value Laravel is seeing for that environment variable. If it'snullor incorrect, you know the.envisn't being read or is being overridden. -
Check PHP Error Logs: Beyond
storage/logs/laravel.log, also check the main PHP-FPM error logs (e.g.,/var/log/php8.x-fpm.log) and your Nginx/Apache error logs for any related messages. They might provide clues about PHP extensions or FPM issues.
Given that you can connect via the terminal, the most common culprits are either the configuration caching, incorrect PHP module setup, or PHP-FPM not correctly passing/reading the environment variables from your .env file. Start by explicitly dumping the env() variables from within your Laravel application to confirm what it's actually using.
Ling Kim
Answered 1 week agoAh, got it! The dd(env('DB_DATABASE')); tip sounds super useful for debugging. Could you share a quick example of where exactly in config/database.php or a controller I should put that?