Ffile2fix
Sign in Get started

How to fix "PDOException: could not find driver" in PHP

PHP Fatal error:  Uncaught PDOException: could not find driver in /home/user/public_html/src/Database.php:15

PDO is installed, but the database-specific driver your DSN asks for (mysql, pgsql or sqlite) is not loaded. It usually happens on a new server, after a PHP version upgrade, or when the CLI and web server use different php.ini files.

Also appears as: Fatal error: Uncaught PDOException: could not find driver in /home/user/public_html/vendor/doctrine/dbal/src/Driver/PDO/MySQL/Driver.php · Illuminate\Database\QueryException: could not find driver (Connection: mysql, SQL: select * from `users`) · PDOException: could not find driver (pgsql) · could not find driver (Connection: sqlite)

Common causes

  • The pdo_mysql, pdo_pgsql or pdo_sqlite extension is not installed
  • The extension is installed for one PHP version but the site runs another
  • The extension line is commented out in php.ini (common on Windows/XAMPP)
  • CLI and PHP-FPM use different configurations, so one has the driver and one does not
  • A Docker image built without docker-php-ext-install pdo_mysql
  • The DSN prefix is misspelled, for example mysqli: instead of mysql:

How to fix it

  1. List available drivers. Run php -r 'print_r(PDO::getAvailableDrivers());' on the CLI, and check the PDO section of phpinfo() on the website. The driver for your database must be listed in both.
  2. Install the driver package. On Ubuntu/Debian run sudo apt install php8.3-mysql (or php8.3-pgsql / php8.3-sqlite3) matching your PHP version. On RHEL-based systems install php-mysqlnd or php-pgsql. Restart PHP-FPM or Apache.
  3. Enable it in php.ini on Windows. In XAMPP or a manual install, open php.ini and remove the ; before extension=pdo_mysql. Restart Apache from the control panel.
  4. Add it to your Docker image. In a php: official image Dockerfile add RUN docker-php-ext-install pdo_mysql, then rebuild the image.
  5. Check the DSN. The DSN must start with the driver name: mysql:host=localhost;dbname=app;charset=utf8mb4, pgsql:host=... or sqlite:/path/to/db.sqlite.

Install and verify the MySQL PDO driver on Ubuntu

sudo apt install php8.3-mysql
sudo systemctl restart php8.3-fpm
php -m | grep -i pdo
# expected: PDO, pdo_mysql

How to stop it happening again

  • Declare ext-pdo_mysql (or ext-pdo_pgsql) in composer.json require
  • Reinstall extensions every time you add a new PHP version
  • Keep Dockerfiles in version control with all needed extensions
  • Compare php -m on CLI and web after server changes

Frequently asked questions

Artisan or the CLI works but the website fails. Why?

The CLI and PHP-FPM read different php.ini files. Install or enable the driver for the FPM or Apache PHP and restart it.

Is mysqli the same as pdo_mysql?

No. They are separate extensions. Code using new PDO('mysql:...') needs pdo_mysql even if mysqli is installed. On Debian/Ubuntu the php-mysql package installs both.

Does this mean my database is down?

No. PHP failed before connecting. Once the driver loads, any real connection problem will show a different SQLSTATE error.