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.
You need to have a module called pdo_mysql. Looking for following in phpinfo(),
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:
Install the php5-mysql package if you do not have it.
In Ubuntu/Debian you can use:
sudo apt-get install php5-mysql
sudo apt-get install php7.0-mysql
Lastly, to get it working, you will need to restart your web-server:
sudo /etc/init.d/apache2 restart
sudo /etc/init.d/nginx restart
For newer versions of Ubuntu that have PHP 7.0 you can get the
php-mysql
package:Then restart your server:
worked well on ubuntu and php 7
I spent the last day trying to figure out why I was getting the following error. I am running Ubuntu 14.04.
The Problem:
I noticed that my PHP-CLI version was running php7.0 but php_info() (the web version) was displaying php 5.5.9. Even though php_info() said pdo was enabled, using the command line (CLI) wasn't recognizing the pdo_mysql command. It turns out that mysql was enabled for my old version but not the CLI version. All I did was install mysql for php7.0 and it was able to work.
This is what worked:
To check the version:
To install mysql for php7.0
1) make sure your CLI version is the same as your web version
2) If they are different, make sure your CLI version has the mysql plug-in since it doesn't come with it as a default.
In my case my DSN string was incorrect, specifically it did not contain
mysql://.
I would have expected a different error message, perhaps something like 'DSN string does not specify driver/protocol.'Adding
mysql://
to the beginning of the DSN string resolved the issue.