PDOException “找不到驱动程序”
我刚刚安装了带有 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_HOST
、DB_NAME
、DB_USER
和 DB_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
我有同样的问题。解决方案取决于操作系统。就我而言,我有 debian,所以要解决它:
安装 php-mysql 和 php7.0-mysql
我编辑了我的
php.ini
,位于 /etc/php/7.0/apache2/php.ini然后重新启动 apache:
这解决了我的问题
I had the same issue. The solution depends on OS. In my case, i have debian, so to solve it:
Install php-mysql and php7.0-mysql
I edited my
php.ini
locate at /etc/php/7.0/apache2/php.iniThen restart apache:
This solves my problem
在我的 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"
使用 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.
在 Ubuntu 上只需执行
On Ubuntu just execute
在 ubuntu 和 php 7 上运行良好
worked well on ubuntu and php 7
对于 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"
将这些添加到 php.ini 时,请确保 php_pdo.dll 引用位于数据库驱动程序 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.ini(使用 phpinfo() 检查位置是否正确)是否正确安装了 MySQL 和驱动程序?
Did you check your php.ini (check for the correct location with phpinfo()) if MySQL and the driver is installed correctly?
对于
CentOS
上的PHP 5.5
,我通过安装php55-mysqlnd
包修复了这个问题。要获得安装帮助,请编写注释,因为这取决于 PHP 在您的系统上的安装方式。可用的存储库是
webtatic
和remi
。For
PHP 5.5
onCentOS
I fixed this by installing thephp55-mysqlnd
package.For help installing, write a comment as it depends on the way PHP is installed on your system. Available repo's are
webtatic
andremi
.我在使用单独的 php.ini 运行测试时遇到了同样的问题。我必须将这些行添加到我自己的 php.ini 文件中:
注意:完全按照此顺序
I had the same problem during running tests with separate php.ini. I had to add these lines to my own php.ini file:
Notice: Exactly in this order
调用 PHP 的安装不正确
我遇到了同样的问题。我希望这能帮助那些和我有类似问题的人。
场景
我在
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
。解决方案
我没有调用
这给了我这个错误,而是使用了它
并且它起作用了。
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
I created
phpinfo.php
file in thepublic
folder and run thephpinfo()
to look for the location of myphp.ini
file.PHP.ini Location =
c:\xampp\php\php.ini
Problem
Calling
c:\xampp\htdocs> php -v
returnedPHP 7.2.3
butphpinfo.php
showedPHP 7.2.2
.Solution
Instead of calling
which gave me this error, I used
and it worked.
检查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.
就我而言,我的 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.我花了最后一天的时间试图找出为什么会出现以下错误。我运行的是 Ubuntu 14.04。
问题:
我注意到我的 PHP-CLI 版本运行的是 php7.0,但 php_info()(网络版本)显示的是 php 5.5.9。尽管 php_info() 表示 pdo 已启用,但使用命令行 (CLI) 无法识别 pdo_mysql 命令。事实证明,我的旧版本启用了 mysql,但 CLI 版本未启用。我所做的就是安装 php7.0 的 mysql,它就可以工作了。
这就是有效的:
检查版本:
为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:
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.
问题是缺少 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.我强烈推荐
mysqllnd
而不是mysql
因为你会遇到很多问题,比如数字转换和位类型评估mysql
扩展的问题。在 ubuntu 上使用以下命令安装 mysqllnd:
I extremely recommend
mysqllnd
instead ofmysql
because of you would have a lot of problems like number converting and bit type evaluates problem withmysql
extension.on ubuntu install
mysqllnd
with following command:就我而言,我将 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.
我在“apt install php-mysql php7toInfinity 并且不要忘记 sqlite-what-ever's” 中苦苦挣扎,直到我回到基础知识并重置网站上的文件权限之前,我无法摆脱此错误消息有问题。
这 3 个命令重置网站上的文件和文件夹权限并使其重新工作。
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.
我在 Debian 6 上修复了这个问题。
通常我刚刚安装了 php5-common 包。安装后,您必须重新启动 Web 服务器(apache 或 nginx,具体取决于您安装的服务器)。
然后我只对 apache 进程 id (
lsof -p process_id
) 执行lsof
操作,如下所示:如您在上面看到的,模块安装在未知的文件路径上或由公共库路径引导:/
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 :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/
这对我有用。
This worked for me.
在删除 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)
还有另一件事需要确认,因为有些人从互联网上复制/粘贴示例代码来开始。确保您已在此处输入 MySQL:
在某些示例中,这显示了
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:
In some examples this shows
对于那些使用 Symfony2/3 并想知道为什么会收到此错误的人。如果您使用“mapping_types”,则可能会遇到此错误。原因是“mapping_types”放置在错误的级别。例如:
这个“mapping_types”必须放在这个级别:
我希望这有助于
我在这里找到解决方案: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 :
This "mapping_types" must be placed at this level :
I hope this helps
I found the solution here : https://github.com/doctrine/DoctrineBundle/issues/327
无论我走到哪里,我都会看到
extension_dir
的路径应该从ext
更改为绝对路径。这对我有用。然而,当尝试为我同事的电脑构建服务器时,我不得不将值设置为ext
,而不是输入绝对路径。如果您确实输入了绝对路径,但仍然找不到扩展名,请考虑尝试使用绝对路径和
ext
。Everywhere I go I read that the path of
extension_dir
should be changed fromext
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 toext
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
.检查 phpinfo() 中的 extension_dir 中的正确路径。
Check correct path in extension_dir in you phpinfo().
有同样的问题,因为我忘记进入我的虚拟机。如果我像这样进入本地目录:
就会出现该错误。但它可以在我的虚拟机上运行
Had the same issue, because I forgot to go into my virtual machine. If I go to my local directory like this:
that error will appear. But it works on my virtual machine
您需要有一个名为 pdo_mysql 的模块。在 phpinfo() 中寻找以下内容,
You need to have a module called pdo_mysql. Looking for following in phpinfo(),
代码中的 dsn 显示您正在尝试连接 mysql 驱动程序。您的错误消息表明该驱动程序不可用。
检查您的服务器上是否安装了 mysql 扩展。
在 Ubuntu/Debian 中,您可以通过以下方式检查软件包:
如果没有,请安装 php5-mysql 软件包。
在 Ubuntu/Debian 中,您可以使用:
sudo apt-get install php5-mysql
sudo apt-get install php7.0-mysql
最后,让它工作,您需要重新启动您的网络服务器:
sudo /etc/init.d/apache2 restart
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:
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
更新:新版本应使用
php-sqlite3
包而不是php5-sqlite
。因此,如果您使用的是最新的 ubuntu 版本,请使用这个:问题的原始答案在这里:
如果您的 phpinfo() 没有显示 pdo_sqlite 行(在我的情况下,在我的 Ubuntu 服务器上),您只需要运行上面的行然后你就可以开始了。
Update: newer versions should use
php-sqlite3
package instead ofphp5-sqlite
. So use this, if you are using a recent ubuntu version:Original answer to question is here:
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.
对于具有 PHP 7.0 的较新版本的 Ubuntu,您可以获取
php-mysql
软件包:然后重新启动服务器:
For newer versions of Ubuntu that have PHP 7.0 you can get the
php-mysql
package:Then restart your server: