PDOException “找不到驱动程序”

发布于 2024-09-01 17:11:19 字数 428 浏览 8 评论 0原文

我刚刚安装了带有 Apache、MySQL 和 PHP 的 Debian Lenny,但收到 PDOException could not find driver

这是它所引用的特定代码行:

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

DB_HOSTDB_NAMEDB_USERDB_PASS 是我定义的常量。它在生产服务器(以及我之前的 Ubuntu 服务器设置)上运行良好。

这和我安装PHP有关系吗?

网上搜索没有帮助,我得到的只是专家交流和示例,但没有解决方案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

逆夏时光 2024-09-08 17:11:20

我有同样的问题。解决方案取决于操作系统。就我而言,我有 debian,所以要解决它:

  • 从 (php5 到 php7)
  • 安装 php-mysql 和 php7.0-mysql

    apt-get install php-mysql
    apt-get安装php7.0-mysql
    
  • 我编辑了我的 php.ini ,位于 /etc/php/7.0/apache2/php.ini

    取消注释行:extension=php_pdo_mysql.dll
    
  • 然后重新启动 apache:

    服务 apache2 重新启动
    

这解决了我的问题

I had the same issue. The solution depends on OS. In my case, i have debian, so to solve it:

  • Updated my php version from (php5 to php7)
  • Install php-mysql and php7.0-mysql

    apt-get install php-mysql
    apt-get install php7.0-mysql
    
  • I edited my php.ini locate at /etc/php/7.0/apache2/php.ini

    uncomment the line : extension=php_pdo_mysql.dll
    
  • Then restart apache:

    service apache2 restart
    

This solves my problem

小兔几 2024-09-08 17:11:20

在我的 Windows 机器上,我必须在 php.ini 中提供扩展目录的绝对路径:

extension_dir = "c:\php5\ext"

On my Windows machine, I had to give the absolute path to the extension dir in my php.ini:

extension_dir = "c:\php5\ext"

玩套路吗 2024-09-08 17:11:20

使用 php -m | 检查该模块是否可用grep pdo_mysql。

如果没有,对于 PHP 7.2,您可以使用 sudo apt install php7.2-mysql 来安装相关软件包。

在其他 PHP 版本和包管理器上使用类似的命令。

Check if the module is available with php -m | grep pdo_mysql.

If not, for PHP 7.2, you can install relevant package with sudo apt install php7.2-mysql.

Use similar command on other PHP versions and package managers.

冰雪之触 2024-09-08 17:11:20

在 Ubuntu 上只需执行

sudo apt-get install php5-mysql

On Ubuntu just execute

sudo apt-get install php5-mysql
兲鉂ぱ嘚淚 2024-09-08 17:11:20
sudo apt-get install php-mysql 

在 ubuntu 和 php 7 上运行良好

sudo apt-get install php-mysql 

worked well on ubuntu and php 7

凹づ凸ル 2024-09-08 17:11:20

对于 Windows 8.1/10,在 :\\php.ini 文件中,您应该取消注释行“extension=pdo_mysql”

for Windows 8.1/10 in :\\php.ini file you should uncomment line "extension=pdo_mysql"

噩梦成真你也成魔 2024-09-08 17:11:20

将这些添加到 php.ini 时,请确保 php_pdo.dll 引用位于数据库驱动程序 dll 之前,否则也会导致此错误消息。像这样添加它们:

[PHP_PDO]
extension=php_pdo.dll
[PHP_PDO_MYSQL]
extension=php_pdo_mysql.dll

When adding these into your php.ini ensure the php_pdo.dll reference is first before the db drivers dlls otherwise this will also cause this error message too. Add them like this:

[PHP_PDO]
extension=php_pdo.dll
[PHP_PDO_MYSQL]
extension=php_pdo_mysql.dll
红焚 2024-09-08 17:11:20

您检查 php.ini(使用 phpinfo() 检查位置是否正确)是否正确安装了 MySQL 和驱动程序?

Did you check your php.ini (check for the correct location with phpinfo()) if MySQL and the driver is installed correctly?

我不会写诗 2024-09-08 17:11:20

对于 CentOS 上的 PHP 5.5,我通过安装 php55-mysqlnd 包修复了这个问题。

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

要获得安装帮助,请编写注释,因为这取决于 PHP 在您的系统上的安装方式。可用的存储库是 webtaticremi

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.

Smile简单爱 2024-09-08 17:11:20

我在使用单独的 php.ini 运行测试时遇到了同样的问题。我必须将这些行添加到我自己的 php.ini 文件中:

[PHP]
extension = mysqlnd.so
extension = pdo.so
extension = pdo_mysql.so

注意:完全按照此顺序

I had the same problem during running tests with separate php.ini. I had to add these lines to my own php.ini file:

[PHP]
extension = mysqlnd.so
extension = pdo.so
extension = pdo_mysql.so

Notice: Exactly in this order

冷血 2024-09-08 17:11:20

调用 PHP 的安装不正确

我遇到了同样的问题。我希望这能帮助那些和我有类似问题的人。

场景

OS = Windows 10  
Platform = XAMPP  
PHP Version = 7 (Multiple Version seem to have been installed in the PC)  

我在public文件夹中创建了phpinfo.php文件并运行phpinfo()来查找位置我的 php.ini 文件。

PHP.ini 位置 = c:\xampp\php\php.ini

问题
调用 c:\xampp\htdocs> php -v 返回 PHP 7.2.3,但 phpinfo.php 显示 PHP 7.2.2

解决方案
我没有调用

php artisan migrate:install   

这给了我这个错误,而是使用了它

c:\xampp\php\php artisan migrate:install

并且它起作用了。

Incorrect installation of PHP was being called

I was experiencing the same problem. And I hope this would help someone who is having a similar issue as me.

Scenario

OS = Windows 10  
Platform = XAMPP  
PHP Version = 7 (Multiple Version seem to have been installed in the PC)  

I created phpinfo.php file in the public folder and run the phpinfo() to look for the location of my php.ini file.

PHP.ini Location = c:\xampp\php\php.ini

Problem
Calling c:\xampp\htdocs> php -v returned PHP 7.2.3 but phpinfo.php showed PHP 7.2.2.

Solution
Instead of calling

php artisan migrate:install   

which gave me this error, I used

c:\xampp\php\php artisan migrate:install

and it worked.

梦在深巷 2024-09-08 17:11:20

检查php配置文件中的extension_dir设置是否正确。尝试注释/取消注释一些扩展,看看它是否反映在 phpinfo() 上。
如果没有,则无法加载 php 配置文件(位置错误)extension_dir 被注释或设置为错误的位置。

Check if extension_dir in php configuration file set correctly. Try to comment/uncomment some extensions and see if it's reflected on phpinfo().
If it doesn't then either php config file cannot be loaded (wrong location) extension_dir is commented or set to the wrong location.

卖梦商人 2024-09-08 17:11:20

就我而言,我的 DSN 字符串不正确,特别是它不包含 mysql://。 我本来预期会出现不同的错误消息,可能类似于“DSN 字符串未指定驱动程序/协议”。

mysql:// 添加到 DSN 字符串的开头解决了该问题。

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.

蓝天白云 2024-09-08 17:11:20

我花了最后一天的时间试图找出为什么会出现以下错误。我运行的是 Ubuntu 14.04。

问题:
我注意到我的 PHP-CLI 版本运行的是 php7.0,但 php_info()(网络版本)显示的是 php 5.5.9。尽管 php_info() 表示 pdo 已启用,但使用命令行 (CLI) 无法识别 pdo_mysql 命令。事实证明,我的旧版本启用了 mysql,但 CLI 版本未启用。我所做的就是安装 php7.0 的 mysql,它就可以工作了。

这就是有效的:

检查版本:

php -v

为php7.0安装mysql

sudo apt-get install php7.0-mysql

1)确保您的CLI版本与您的Web版本相同
2) 如果它们不同,请确保您的 CLI 版本具有 mysql 插件,因为它默认不附带该插件。

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.

感情废物 2024-09-08 17:11:20

问题是缺少 php 到 mysql 库。在 CentOs 中我通过运行修复了它
# yum install php-mysql 然后使用 # /bin/systemctl restart httpd.service 重新启动 apache 请注意,命名与基于 debian/ubuntu 的发行版 php- 略有不同>php5 和 httpd->apache2。

The problem is a missing php to mysql library. In CentOs i fixed it by running
# yum install php-mysql and then restarting apache with # /bin/systemctl restart httpd.service Note that the naming is slightly different from debian/ubuntu based distros, php->php5 and httpd->apache2.

如痴如狂 2024-09-08 17:11:20

我强烈推荐 mysqllnd 而不是 mysql 因为你会遇到很多问题,比如数字转换和位类型评估 mysql 扩展的问题。

在 ubuntu 上使用以下命令安装 mysqllnd:

sudo apt-get install php5-mysqlnd

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
〗斷ホ乔殘χμё〖 2024-09-08 17:11:20

就我而言,我将 PDO 与 php-cli 一起使用,并且运行良好。

只有当我尝试从apache连接时,我才遇到“缺少驱动程序”的问题,我不太明白。

一个简单的 apt-get install php-mysql 解决了它。 (Ubuntu 16.04 / PHP7。学分转到所选答案和 Ivan 的评论)

希望它能有所帮助。

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.

猛虎独行 2024-09-08 17:11:20
PHP Fatal error:  Uncaught PDOException: could not find driver

我在“apt install php-mysql php7toInfinity 并且不要忘记 sqlite-what-ever's” 中苦苦挣扎,直到我回到基础知识并重置网站上的文件权限之前,我无法摆脱此错误消息有问题。

这 3 个命令重置网站上的文件和文件夹权限并使其重新工作。

cd /var/www/web-site-name.com/web/

# find (sub) directories and change permissions
find . -type d -exec chmod 755 {} \;

# find files and change permissions
find . -type f -exec chmod 664 '{}' \;
PHP Fatal error:  Uncaught PDOException: could not find driver

I struggled and struggled with "apt install php-mysql php7toInfinity and don't forget sqlite-what-ever's" and just could not get rid of this error message until I went back to basics and reset the file-permissions on the web-site in question.

These 3 commands reset file and folder permissions on the web-site and got it to work again.

cd /var/www/web-site-name.com/web/

# find (sub) directories and change permissions
find . -type d -exec chmod 755 {} \;

# find files and change permissions
find . -type f -exec chmod 664 '{}' \;
澉约 2024-09-08 17:11:20

我在 Debian 6 上修复了这个问题。
通常我刚刚安装了 php5-common 包。安装后,您必须重新启动 Web 服务器(apache 或 nginx,具体取决于您安装的服务器)。
然后我只对 apache 进程 id (lsof -p process_id) 执行 lsof 操作,如下所示:

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

如您在上面看到的,模块安装在未知的文件路径上或由公共库路径引导:/usr/lib/php5/20090626/。对于你的安装,可能会有所不同,但只是pdo_mysql.so、pdo.so、mysqli.so的路径。所以,这就是为什么 Drupal 或任何其他 php 引擎找不到该库并显示错误: PDOException: 找不到驱动程序

我只是不知道为什么它安装在如此奇怪的路径上,对我来说这只是 debian 6 中库包安装脚本中的一个错误。
我通过为 /usr/lib/php5/20090626/ 下的所有文件创建符号解决了这个问题
/usr/lib/php5/ 使用此命令:

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

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/

说好的呢 2024-09-08 17:11:20
$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

这对我有用。

$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.

緦唸λ蓇 2024-09-08 17:11:20

在删除 php5 软件包(也包括所有驱动程序)以安装 php7 软件包后,我遇到了同样的问题。我实际上安装了没有 mysql 模块的 php7 包。

我设法通过在终端中输入来解决它:

1) $ apt-cache search php7
其中列出了所有模块,浏览我发现的模块,

php7.0-mysql - PHP 2 的 MySQL 模块

) $ sudo apt-get install php7.0-mysql

就是这样。它在我的 Linux 系统中对我有用。

(使用适当的php版本,你的可能是php5)

I faced the same issue after I removed the php5 package (that includes all the drivers as well) in order to install php7 package. I actually installed php7 package without a mysql module.

I managed to solve it by typing in the terminal:

1) $ apt-cache search php7
which lists all the modules, looking through the modules I found,

php7.0-mysql - MySQL module for PHP

2) $ sudo apt-get install php7.0-mysql

That's it. It worked for me in my linux system.

(use the appropriate php version, yours could be php5)

森林散布 2024-09-08 17:11:20

还有另一件事需要确认,因为有些人从互联网上复制/粘贴示例代码来开始。确保您已在此处输入 MySQL:

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

在某些示例中,这显示了

$dbh = new PDO ("dblib ...

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 ...
忘东忘西忘不掉你 2024-09-08 17:11:20

对于那些使用 Symfony2/3 并想知道为什么会收到此错误的人。如果您使用“mapping_types”,则可能会遇到此错误。原因是“mapping_types”放置在错误的级别。例如:

doctrine:
  dbal:
    mapping_types:
        set: string

这个“mapping_types”必须放在这个级别:

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

我希望这有助于

我在这里找到解决方案:https://github.com/doctrine/DoctrineBundle/issues/327

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

柠檬心 2024-09-08 17:11:20

无论我走到哪里,我都会看到 extension_dir 的路径应该从 ext 更改为绝对路径。这对我有用。然而,当尝试为我同事的电脑构建服务器时,我不得不将值设置为ext,而不是输入绝对路径。

如果您确实输入了绝对路径,但仍然找不到扩展名,请考虑尝试使用绝对路径和ext

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.

天涯离梦残月幽梦 2024-09-08 17:11:20

检查 phpinfo() 中的 extension_dir 中的正确路径。

Check correct path in extension_dir in you phpinfo().

愁杀 2024-09-08 17:11:20

有同样的问题,因为我忘记进入我的虚拟机。如果我像这样进入本地目录:

cd /www/homestead/my_project
php artisan migrate

就会出现该错误。但它可以在我的虚拟机上运行

cd ~/homestead
vagrant ssh   
cd /www/homestead/my_project
php artisan migrate

Had the same issue, because I forgot to go into my virtual machine. If I go to my local directory like this:

cd /www/homestead/my_project
php artisan migrate

that error will appear. But it works on my virtual machine

cd ~/homestead
vagrant ssh   
cd /www/homestead/my_project
php artisan migrate
心奴独伤 2024-09-08 17:11:19

您需要有一个名为 pdo_mysql 的模块。在 phpinfo() 中寻找以下内容,

pdo_mysql

PDO Driver for MySQL, client library version => 5.1.44

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
我ぃ本無心為│何有愛 2024-09-08 17:11:19

代码中的 dsn 显示您正在尝试连接 mysql 驱动程序。您的错误消息表明该驱动程序不可用。

检查您的服务器上是否安装了 mysql 扩展。

在 Ubuntu/Debian 中,您可以通过以下方式检查软件包:

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

如果没有,请安装 php5-mysql 软件包。

在 Ubuntu/Debian 中,您可以使用:

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

最后,让它工作,您需要重新启动您的网络服务器:

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

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
硬不硬你别怂 2024-09-08 17:11:19

更新:新版本应使用 php-sqlite3 包而不是 php5-sqlite。因此,如果您使用的是最新的 ubuntu 版本,请使用这个:

sudo apt-get install sqlite php-sqlite3

问题的原始答案在这里:

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

如果您的 phpinfo() 没有显示 pdo_sqlite 行(在我的情况下,在我的 Ubuntu 服务器上),您只需要运行上面的行然后你就可以开始了。

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.

浅笑依然 2024-09-08 17:11:19

对于具有 PHP 7.0 的较新版本的 Ubuntu,您可以获取 php-mysql 软件包:

sudo apt-get install php-mysql

然后重新启动服务器:

sudo service apache2 restart

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文