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
- 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.
- 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.
- 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.
- Add it to your Docker image. In a php: official image Dockerfile add RUN docker-php-ext-install pdo_mysql, then rebuild the image.
- 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