DB Connects in CLI but Not in App: 3 Fixes That Work

Database Errors Intermediate 👁 14 views 📅 Jun 17, 2026

When your database works from the command line but fails in your app, it's almost always a PHP/webserver config mismatch or socket issue. Here's the fix.

1. The PHP Config Mismatch (Most Common)

I've been running a help desk blog for 6 years, and this one trips up devs constantly. Your CLI PHP uses a different php.ini than your web server (Apache/Nginx + PHP-FPM). So when you type php artisan migrate or php -r 'new PDO(...)', it connects fine. But the same credentials in your app fail.

Here's the quick check: run php --ini in your terminal. Note the path. Then create a info.php file in your web root with <?php phpinfo(); ?> and load it in your browser. Look for Loaded Configuration File. If they're different, you've found the culprit.

The fix: Copy the PDO/MySQL extension settings from the CLI php.ini into the webserver's php.ini, or symlink them. On Ubuntu 22.04 with PHP 8.1, the CLI path is often /etc/php/8.1/cli/php.ini and the FPM path is /etc/php/8.1/fpm/php.ini. The missing extension lines are usually:

extension=pdo
extension=pdo_mysql
extension=mysqli

After adding them, restart PHP-FPM: sudo systemctl restart php8.1-fpm. I've seen this fix work for Laravel, Symfony, WordPress, and raw PDO apps.

2. Socket vs TCP Port (The Sneaky One)

Your CLI might connect via a Unix socket (default on many setups), while your web server and app expect TCP on port 3306. Sound familiar? I've debugged this for a client who spent 3 hours installing and reinstalling MariaDB before calling me.

How to tell: Run php -r 'echo ini_get("pdo_mysql.default_socket");' in CLI. You'll get a path like /var/run/mysqld/mysqld.sock. Now, in your app's database config—if you're using Laravel's .env, Symfony's .env.local, or even a raw config.php—you're probably using DB_HOST=127.0.0.1 or localhost with port 3306. That tries TCP, not the socket.

The fix: Either make the app use the socket, or force PHP-FPM to use TCP. I prefer the socket approach because it's faster and more secure. In Laravel, change your .env to:

DB_HOST=localhost
DB_PORT=3306
DB_SOCKET=/var/run/mysqld/mysqld.sock

For raw PDO, your connection string becomes:

$pdo = new PDO('mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=yourdb', 'user', 'pass');

If you'd rather stick with TCP, check your MySQL/MariaDB config (/etc/mysql/mariadb.conf.d/50-server.cnf or my.cnf) and make sure skip-networking is commented out or set to 0. Then restart MySQL.

3. Environment Variables Not Loaded in Web Context

This one's more common in modern frameworks. You set DB_USER and DB_PASS in your shell's .bashrc or .env file for CLI use—and they work perfectly. But PHP-FPM runs under a different user (typically www-data) and doesn't read those files. So your app gets empty strings for credentials.

I once saw a team's staging environment fail for a week because of this. The CLI tests passed every time, but the web app threw SQLSTATE[HY000] [1045] Access denied. Turned out the .env file was in the home directory, not the project root.

The fix: Make sure your app's .env file is in the web root (or the directory your framework expects). For Laravel, it's the project root. For Symfony, same. Then check file permissions: the web server user www-data must be able to read it. sudo chown www-data:www-data .env (if you're brave) or sudo chmod 644 .env (safer).

If you're using Docker or Lando, this gets messier. The container's environment variables might come from a .env file that's only sourced in the CLI container, not the web container. I've fixed this by adding DB_USER and DB_PASS directly in the Docker Compose environment block, or by using a shared .env with absolute paths.

Quick Summary Table

Issue Why It Works in CLI Why It Fails in App Fix
PHP config mismatch CLI reads its own php.ini Web server reads different php.ini Copy PDO extensions to web php.ini
Socket vs TCP CLI uses socket by default App config tries TCP on 127.0.0.1 Set DB_SOCKET in app config
Env variables not loaded Shell sources .bashrc or .env PHP-FPM doesn't read those Put .env in web root, fix permissions

I know this error is infuriating—especially when you've confirmed the credentials work. But 9 times out of 10, it's one of these three. Start with the PHP config mismatch (step 1). That alone solves it for over half the cases I've seen. If not, move to the socket issue. And if you're still stuck, check those environment variables. You'll be back up and running in minutes.

Was this solution helpful?