PDOException “could not find driver”

PDOException “could not find driver”

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PDOException "could not find driver"

When you instantiate a PDO object with the mysql:host=... DSN on Debian Lenny, PHP throws a PDOException stating it cannot find the driver. This error is distinct from connection timeouts or authentication failures; it indicates that the PHP runtime simply does not know how to speak the MySQL protocol. While your production server and Ubuntu setup work perfectly, this discrepancy usually points to an incomplete package installation on the Debian system rather than a code logic issue.

The core of the problem lies in the separation between the database engine itself and the PHP extension required to interact with it. Installing Apache, MySQL, and PHP via the standard Debian apt repositories often provides the base binaries but leaves specific extensions out of the default installation footprint. Unlike some environments where these packages are pre-bundled or auto-configured, Debian relies on explicit package installation for each language binding.

To resolve this, you must locate and install the specific driver extension that bridges PHP and MySQL. For legacy systems running older versions of MySQL (common in Lenny-era setups), the required package is often php5-mysql. However, if you are using a newer PHP version or have upgraded the stack, the package name might differ, such as php-mysql for PHP 7+ or potentially just pdo_mysql depending on whether the base PDO extension is already present.

You can identify the missing dependency by inspecting your installed packages against the list of available ones. Run the following command to check if the specific driver package exists in the repository:

dpkg -l | grep php.*mysql

If the output is empty or lacks pdo_mysql and php-mysql, the extension is indeed missing. You can install it using your package manager. Assuming you are on a standard Debian setup with APT, execute:

sudo apt-get update
sudo apt-get install php5-mysql
# OR for newer PHP versions
sudo apt-get install php-mysql

After installation, the extension must be loaded into the PHP runtime. This is typically handled by updating the php.ini configuration file. Locate the active configuration file using:

php --ini

You may need to ensure that extension=pdo_mysql.so (or similar) is uncommented or added. If you are running PHP under Apache, remember that a restart of the web server is mandatory for these changes to take effect:

sudo service apache2 restart

A common pitfall in this scenario involves the distinction between pdo_mysql and the legacy mysqlnd driver. The error specifically says "could not find driver," which confirms the extension is absent. However, some developers attempt to fix this by altering the DSN string or changing connection parameters, which will not resolve a missing binary dependency. The solution must be at the system library level.

Another nuance arises if you are mixing different PHP versions (e.g., CLI vs. Apache). Debian often installs multiple PHP instances, and installing php5-mysql might only affect one of them. If your application runs via CGI-FastCGI or a specific FPM pool, ensure the extension is loaded in that specific pool's configuration, not just the global system-wide php.ini.

If you are porting code from Ubuntu to Debian, note that Ubuntu's package naming conventions and default repositories sometimes differ. A package available on Ubuntu might require manual compilation or a different repository source (PPA) on Debian if the official repos do not provide the latest driver version. Always verify the exact PHP version your application targets and match the extension package accordingly.

For projects using frameworks like Laravel, ensuring the correct PHP extensions are loaded is part of the initial setup checklist. If you encounter persistent issues where the error appears despite installing the package, check if php-mysql conflicts with a newer pdo_mysql package or if the extension directive is being overridden by a user-specific configuration file in your web directory.

Our network