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

In my case, I was using PDO with php-cli, and it worked fine.

Only when I tried to connect from apache, I got the "missing driver" issue, which I didn't quite understand.

A simple apt-get install php-mysql solved it. (Ubuntu 16.04 / PHP7. credits go to the selected answer & Ivan's comment)

Hope it can help.

查看更多
萌妹纸的霸气范
3楼-- · 2018-12-31 05:48

For PHP 5.5 on CentOS I fixed this by installing the php55-mysqlnd package.

sudo yum -y install php55w-mysqlnd # For Webtatic
sudo yum -y install php55u-mysqlnd # For Remi

For help installing, write a comment as it depends on the way PHP is installed on your system. Available repo's are webtatic and remi.

查看更多
孤独寂梦人
4楼-- · 2018-12-31 05:48

I extremely recommend mysqllnd instead of mysql because of you would have a lot of problems like number converting and bit type evaluates problem with mysql extension.

on ubuntu install mysqllnd with following command:

sudo apt-get install php5-mysqlnd
查看更多
萌妹纸的霸气范
5楼-- · 2018-12-31 05:48
$DB_TYPE = 'mysql'; //Type of database<br>
$DB_HOST = 'localhost'; //Host name<br>
$DB_USER = 'root'; //Host Username<br>
$DB_PASS = ''; //Host Password<br>
$DB_NAME = 'database_name'; //Database name<br><br>

$dbh = new PDO("$DB_TYPE:host=$DB_HOST; dbname=$DB_NAME;", $DB_USER, $DB_PASS); // PDO Connection

This worked for me.

查看更多
君临天下
6楼-- · 2018-12-31 05:48

Just one other thing to confirm as some people are copy/pasting example code from the Internet to get started. Make sure you have MySQL entered here:

... $dbh = new PDO ("mysql: ...  

In some examples this shows

$dbh = new PDO ("dblib ...
查看更多
怪性笑人.
7楼-- · 2018-12-31 05:49

For those using Symfony2/3 and wondering why you're getting this error. If you're using "mapping_types", you might encounter this error. The reason is that "mapping_types" is placed at the wrong level. For instance :

doctrine:
  dbal:
    mapping_types:
        set: string

This "mapping_types" must be placed at this level :

doctrine:
dbal:
    #To counter the error caused by 'mapping_types'
    connections:
        default:
            server_version: %database_server_version%
            mapping_types:
                set: string

I hope this helps

I found the solution here : https://github.com/doctrine/DoctrineBundle/issues/327

查看更多
登录 后发表回答