PDOException “could not find driver”

2018-12-31 04:53发布

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.

标签: php mysql pdo lamp
30条回答
荒废的爱情
2楼-- · 2018-12-31 05:36

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
查看更多
泪湿衣
3楼-- · 2018-12-31 05:37

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
查看更多
柔情千种
4楼-- · 2018-12-31 05:38

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
查看更多
萌妹纸的霸气范
5楼-- · 2018-12-31 05:38
sudo apt-get install php-mysql 

worked well on ubuntu and php 7

查看更多
琉璃瓶的回忆
6楼-- · 2018-12-31 05:38

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:

php -v

To install mysql for php7.0

sudo apt-get install php7.0-mysql

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.

查看更多
无色无味的生活
7楼-- · 2018-12-31 05:38

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.

查看更多
登录 后发表回答