PDOException “could not find driver”

I have just installed Debian Lenny with Apache, MySQL, and PHP and I am receiving a PDOException could not find driver.

This is the specific line of code it is referring to:

$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS)

DB_HOST, DB_NAME, DB_USER, and DB_PASS are constants that I have defined. It works fine on the production server (and on my previous Ubuntu Server setup).

Is this something to do with my PHP installation?

Searching the internet has not helped, all I get is experts-exchange and examples, but no solutions.


Solution 1:

You need to have a module called pdo_mysql. Looking for following in phpinfo(),

pdo_mysql

PDO Driver for MySQL, client library version => 5.1.44

Solution 2:

The dsn in your code reveals you are trying to connect with the mysql driver. Your error message indicates that this driver is unavailable.

Check that you have the mysql extension installed on your server.

In Ubuntu/Debian you check for the package with:

dpkg --get-selections | grep php | grep mysql

Install the php5-mysql package if you do not have it.

In Ubuntu/Debian you can use:

  • PHP5: sudo apt-get install php5-mysql
  • PHP7: sudo apt-get install php7.0-mysql

Lastly, to get it working, you will need to restart your web-server:

  • Apache: sudo /etc/init.d/apache2 restart
  • Nginx: sudo /etc/init.d/nginx restart

Solution 3:

Update: newer versions should use php-sqlite3 package instead of php5-sqlite. So use this, if you are using a recent ubuntu version:

sudo apt-get install sqlite php-sqlite3

Original answer to question is here:

sudo apt-get install sqlite php5-sqlite
sudo /etc/init.d/apache2 restart

If your phpinfo() is not showing the pdo_sqlite line (in my case, on my Ubuntu Server), you just need to run the lines above and then you'll be good to go.

Solution 4:

For newer versions of Ubuntu that have PHP 7.0 you can get the php-mysql package:

sudo apt-get install php-mysql

Then restart your server:

sudo service apache2 restart