Skip to content

Laravel error

Laravel: PDOException could not find driver

The error

PDOException: could not find driver

PHP has no PDO driver for the database you configured. It is an extension that is missing, disabled or installed for a different PHP binary than the one running your code.

The error

PDOException

could not find driver

Laravel usually wraps it:

Illuminate\Database\QueryException
could not find driver (Connection: mysql, SQL: select ...)

What it means

PDO is installed, but the driver for your specific database is not. PHP has a separate extension per database - pdo_mysql, pdo_pgsql, pdo_sqlite - and the connection you configured needs the one that is missing.

Note what it is not. It is not wrong credentials, which say access denied. It is not an unreachable server, which says connection refused. The database has not been contacted at all; PHP cannot make the attempt.

Confirm which driver is missing

php -m | grep pdo
php -r "var_dump(PDO::getAvailableDrivers());"

Then check what the application is asking for:

php artisan tinker --execute="echo config('database.default');"

If config('database.default') is pgsql and the driver list contains only mysql and sqlite, that is the whole fault.

Install the right extension, for the right PHP

The version in the package name has to match the PHP that runs your application:

php -v                                   # check this first
 
# Debian / Ubuntu
sudo apt install php8.3-mysql            # or php8.3-pgsql
sudo systemctl restart php8.3-fpm
 
# RHEL / Alma / Rocky
sudo dnf install php-mysqlnd             # or php-pgsql
sudo systemctl restart php-fpm

Installing php-mysql without the version prefix on a machine with several PHP versions installs it for the default one, which is often not the one your site runs. That is the most common reason this error survives an apparently successful install.

In Docker

The official images include no database drivers:

FROM php:8.3-fpm
 
RUN docker-php-ext-install pdo_mysql
 
# for Postgres, the client library is needed at build time
RUN apt-get update \
 && apt-get install -y libpq-dev \
 && docker-php-ext-install pdo_pgsql

Rebuild the image rather than restarting the container - the extension is baked in at build time, so a restart changes nothing.

The CLI and web mismatch

Migrations run, the site does not. Two PHP configurations:

php -i | grep "Loaded Configuration File"
<?php phpinfo();   // load this over HTTP and read the same line

Two different paths means two ini files. Enable the extension in the web server's, then restart PHP-FPM - editing the file is not enough on its own.

After it is installed and still missing

php -i | grep extension_dir
ls $(php -r "echo ini_get('extension_dir');") | grep pdo

If the .so file is not in that directory, the package was built for a different PHP version. If it is there and still not loaded, the extension= line is in an ini file that is not being read - check the directory that ini files are scanned from, which php -i | grep "Scan this dir" will name.

One more that costs people an afternoon: with Octane, or any long-running worker, the process holds the PHP it started with. Restarting FPM does not restart those. They need stopping and starting themselves.

Once the driver loads, the next error on a fresh machine is usually about the credentials. Access denied for user looks similar and is not.

The long-running-process case is the general one. A worker holds the code and the runtime it booted with, which is also why Octane exposes a class of bug PHP used to clean up between requests.

Related questions

It works on the command line but not in the browser.
Then you have two PHP configurations. The CLI binary and the web server's PHP use separate ini files, and an extension enabled in one is not enabled in the other. Compare phpinfo() from the browser against php -i on the command line and look at the Loaded Configuration File line in each.
We installed php-mysql and it still fails.
Usually because it was installed for a different PHP version than the one running. A machine with 8.1 and 8.3 side by side will happily install the extension for whichever is default and leave the other without it. Check php -v and install the version-matched package.
It started after a server upgrade.
A PHP minor-version upgrade installs a new extension directory, and extensions compiled for the previous version are not carried across. The package manager usually reinstalls the common ones and misses anything that was added by hand.
Is this different in Docker?
The official PHP images ship almost no database extensions - they have to be added with docker-php-ext-install, and pdo_pgsql additionally needs the Postgres client library present at build time. A Dockerfile that only runs composer install produces exactly this error.
Call us+1 848 272 7583WhatsApp+90 850 308 5436Emailinfo@codefacture.comContact page