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:22

Everywhere I go I read that the path of extension_dir should be changed from ext to an absolute path. It worked for me. However, when trying to build a server of my colleague's PC, I had to let the value to ext instead of putting an absolute path.

If you did put an absolute path and it does the extension is still not found, considerer trying both with the absolute path and ext.

查看更多
梦寄多情
3楼-- · 2018-12-31 05:25

For Linux Mint

I had the same issue whilst using PhpBrew ( 5.5.9 / 7.0.14 ) and trying to create a PDO connection.

After I had tried most of the solutions on this post I did the following:

  1. Powered off PhpBrew

  2. Executed sudo apt-get install php7.0 (Linux Mint 17.2 / PHP7.0.16) - installed fresh php version

查看更多
不再属于我。
4楼-- · 2018-12-31 05:26

I had the same exception when trying to use pdo_sqlite. The problem was that the automatically created 20-pdo_sqlite.ini and 20-sqlite3.ini symlinks (in /etc/php5/mods-enabled) were broken.

Removeing the broken symlinks and adding them manually with:

  • sudo ln -s /etc/php5/mods-avaliable/pdo_sqlite.ini /etc/php5/mods-enabled/20-pdo_sqlite.ini
  • sudo ln -s /etc/php5/mods-avaliable/sqlite3.ini /etc/php5/mods-enabled/20-sqlite3.ini

fixed the problem.

Note: this won't work for older php versions as they did not look for configs in /etc/php5/mods-enabled

查看更多
情到深处是孤独
5楼-- · 2018-12-31 05:27

On Ubuntu just execute

sudo apt-get install php5-mysql
查看更多
不流泪的眼
6楼-- · 2018-12-31 05:27

I Fixed this issue on my Debian 6. Normally I just had installed php5-common package. After installation, you have to restart your web server (apache or nginx depending on which one you installed). Then I just do an lsof on the apache process id (lsof -p process_id) as followed :

sudo lsof -p 1399   #replace 1399 by your apache process id
apache2 1399 root  mem    REG  254,2    80352 227236 /usr/lib/php5/20090626/xmlrpc.so
apache2 1399 root  mem    REG  254,2   166496 227235 /usr/lib/php5/20090626/suhosin.so
apache2 1399 root  mem    REG  254,2    31120 227233 /usr/lib/php5/20090626/pdo_mysql.so
apache2 1399 root  mem    REG  254,2   100776 227216 /usr/lib/php5/20090626/pdo.so
apache2 1399 root  mem    REG  254,2   135864 227232 /usr/lib/php5/20090626/mysqli.so

As you can see above, the modules are installed on a file path not known or guided by common library path: /usr/lib/php5/20090626/. For your installation, it may be different, but only the path of pdo_mysql.so, pdo.so, mysqli.so. So, this is why Drupal or any other php engine couldn't find the library and shows that error: PDOException: could not find driver

I just don't know why it is installed on such a weird path, for me it's just a bug in the library package installation script in debian 6. I solved the issue by creating a symbolic for all the files under /usr/lib/php5/20090626/ to /usr/lib/php5/ with this command :

ln -s /usr/lib/php5/20090626/* /usr/lib/php5/

查看更多
永恒的永恒
7楼-- · 2018-12-31 05:29

If you read all answer above and it still does not work...

Make sure that your PHP PDO connection string is fine. Not like mine:

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

There is no information in error message what driver was not found.

After reinstalling all possible PDO and MySQL libraries I found out that there was " at start of my connection string.

查看更多
登录 后发表回答