PHP 7.0 上的 Laravel 5.4:PDO 异常 - 找不到驱动程序 (MySQL)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42909397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:43:40  来源:igfitidea点击:

Laravel 5.4 on PHP 7.0: PDO Exception - Could not find driver (MySQL)

phpmysqllaravelpdolaravel-5

提问by Maramal

I have a Laravel 5.4 project on my Ubuntu 14.04 (VPS with Plesk 12.5.30). After creating the database and setted up the .envfile with required information I ran php artisan migrateand exceptions were thrown:

我的 Ubuntu 14.04(带有 Plesk 12.5.30 的 VPS)上有一个 Laravel 5.4 项目。创建数据库并使用所需信息设置.env文件后,我运行php artisan migrate并抛出异常:

[Illuminate\Database\QueryException]
could not find driver (SQL: select * from information_schema.tables where table_schema = pmaramaldb and table_name = migrations)

[PDOException]
could not find driver

[Illuminate\Database\QueryException]
找不到驱动程序(SQL: select * from information_schema.tables where table_schema = pmaramaldb and table_name = migrations)

[PDOException]
找不到驱动程序

My .envfile has:

我的.env文件有:

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=pmaramaldb

DB_USERNAME=user

DB_PASSWORD=password

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=pmaraldb

DB_USERNAME=用户

DB_PASSWORD=密码

I've followed most of the guides to solve this error and there wasn't any positive results:

我已经按照大多数指南来解决这个错误,但没有任何积极的结果:

Enabling Extension Solution:

启用扩展解决方案:

PDO is enabled

PDO 已启用

The php.inifile is being generated automatically, I guess this is made by Plesk. At the start of the file it says:

php.ini中被自动生成的文件,我想这是做的Plesk。在文件的开头它说:

; ATTENTION!
;
; DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY,
; SO ALL YOUR CHANGES WILL BE LOST THE NEXT TIME THE FILE IS GENERATED.

; 注意力!
;
; 不要修改这个文件,因为它是自动生成的,
;因此,下次生成文件时,您所做的所有更改都将丢失。

So I've tried it anyway. After that I've restarted Apache and then tried again... Didn't work.

所以无论如何我都试过了。之后,我重新启动了 Apache,然后再试了一次……没有用。

Installing php7.0-mysql Solution:

安装php7.0-mysql 解决方法:

I've also tried to install MySQL in case is not:

我还尝试安装 MySQL,以防万一:

user@server:/var/www/vhosts$ sudo apt-get install php7.0-mysql

Leyendo lista de paquetes... Hecho

Creando árbol de dependencias

Leyendo la información de estado... Hecho

php7.0-mysql is already the newest version.

0 actualizados, 0 se instalarán, 0 para eliminar y 67 no actualizados.

用户@服务器:/var/www/vhosts$ sudo apt-get install php7.0-mysql

Leyendo lista de paquetes... Hecho

Creando árbol de Dependencias

Leyendo la información de estado... Hecho

php7.0-mysql 已经是最新版本了。

0 现实化,0 自我安装,0 对消除 y 67 没有现实化。

** SQL Lite Install Solution:**

** SQL Lite 安装解决方案:**

I've also tried as question linksays: sudo apt-get install php5-sqliteand the result was:

我也试过问题链接说:sudo apt-get install php5-sqlite结果是:

user@server:/var/www/vhosts/system/maramal.io/etc$ sudo apt-get install php5-sqlite

Leyendo lista de paquetes... Hecho

Creando árbol de dependencias

Leyendo la información de estado... Hecho

php5-sqlite is already the newest version.

0 actualizados, 0 se instalarán, 0 para eliminar y 67 no actualizados.

用户@服务器:/var/www/vhosts/system/maramal.io/etc$ sudo apt-get install php5-sqlite

Leyendo lista de paquetes... Hecho

Creando árbol de Dependencias

Leyendo la información de estado... Hecho

php5-sqlite 已经是最新版本了。

0 现实化,0 自我安装,0 对消除 y 67 没有现实化。

Composer Dump-autoload Solution:

Composer 转储自动加载解决方案:

It didn't work either. I've tried composer dump-autoloadand it didn't work.

它也没有用。我试过了composer dump-autoload,没有用。

After ran the command php -i:

运行命令后php -i

PDO

PDO support => enabled

PDO drivers =>

...

PHP Version => 5.6.30-1+deb.sury.org~trusty+1

PDO

PDO 支持 => 启用

PDO 驱动程序 =>

...

PHP 版本 => 5.6.30-1+deb.sury.org~trusty+1

Well, I am not sure if this has anything to do with the error, but the version shown by running php -vis:

好吧,我不确定这是否与错误有关,但是运行显示的版本php -v是:

user@server: ~/path$ php -v

PHP 5.6.30-1+deb.sury.org~trusty+1 (cli)

Copyright (c) 1997-2016 The PHP Group

Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

用户@服务器:~/path$ php -v

PHP 5.6.30-1+deb.sury.org~trusty+1 (cli)

版权所有 (c) 1997-2016 PHP Group

Zend Engine v2.6.0,版权所有 (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev,版权 (c) 1999-2016,由 Zend Technologies

采纳答案by apokryfos

There's a conflict between the PHP that is used by Apache and the PHP that is linked to the command line. (It happens more often that it should to be honest).

Apache 使用的 PHP 与链接到命令行的 PHP 之间存在冲突。(更经常发生的是,它应该是诚实的)。

What is typically done is:

通常的做法是:

which php

This tells you which php will be expecuted when running in the command line. e.g. /usr/bin/php

这会告诉您在命令行中运行时将执行哪个 php。例如/usr/bin/php

mv /usr/bin/php /usr/bin/php.old

Then link or copy the correct PHP version to an executable path:

然后将正确的 PHP 版本链接或复制到可执行路径:

ln -s /path/to/php/bin/php /usr/bin/php

or the following should also work.

或者以下也应该有效。

cp /path/to/php/bin/php /usr/bin/php

Also suggested if you want to be able to manually install mods:

如果您希望能够手动安装 mod,还建议:

ln -s /path/to/php/bin/phpize /usr/bin/phpize
ln -s /path/to/php/bin/php-config /usr/bin/php-config

This way your CLI will match your webserver.

这样您的 CLI 将匹配您的网络服务器。

Update:

更新:

If as noted in this answerif you are using Ubuntu with multiple alternative installations of PHP you can do:

如果如本答案所述,如果您将 Ubuntu 与多个 PHP 替代安装一起使用,您可以执行以下操作:

sudo update-alternatives --set php /usr/bin/php<version>
sudo update-alternatives --set phar /usr/bin/phar<version>
sudo update-alternatives --set phar.phar /usr/bin/phar.phar<version> 
sudo update-alternatives --set phpize /usr/bin/phpize<version> 
sudo update-alternatives --set php-config /usr/bin/php-config<version>

回答by ?smail Atkurt

As an alternative solution be sure you have installed php7-mysql

作为替代解决方案,请确保您已安装 php7-mysql

try to run command below

尝试在下面运行命令

sudo apt install php7.0-mysql

回答by Devin Norgarb

Same problem, ubuntu 17.10, Laravel 5.5.

同样的问题,ubuntu 17.10,Laravel 5.5。

$ sudo apt install php7.1-pdo php7.1-mysql

$ sudo apt install php7.1-pdo php7.1-mysql

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

A nice quick fix, hope this helps

一个不错的快速修复,希望这会有所帮助

回答by doyevaristo

I had the same scenario you have. I did resolve mine the following:

我有和你一样的场景。我确实解决了以下问题:

1) on your terminal, type

1)在你的终端上,输入

php --ini

php --ini

this will output your current php.ini configuration file path.

这将输出您当前的 php.ini 配置文件路径。

2) edit php.ini using vim or your preferred editor.

2) 使用 vim 或您喜欢的编辑器编辑 php.ini。

vim /etc/php.ini

vim /etc/php.ini

note that /etc/php.ini is your path file for your php.ini. it may be different on your machine.

请注意,/etc/php.ini 是 php.ini 的路径文件。您的机器上可能会有所不同。

3) find ;extension=php_pdo_mysql.dlland uncomment it by removing semicolon (;)

3) 找到;extension=php_pdo_mysql.dll并通过删除分号 (;)

4) save your php.ini change/s

4) 保存您的 php.ini 更改/s

**5) restart the web server to apply the changes

**5) 重新启动 Web 服务器以应用更改

sudo systemctl restart apache2

须藤 systemctl 重启 apache2

**6) Now run your command.

**6) 现在运行您的命令。

php artisan migrate

php工匠迁移

hope this helps for you.

希望这对你有帮助。

回答by Jignesh Joisar

in linux do like that

在 linux 中这样做

first check your php version like this :

首先像这样检查你的php版本:

php -v

after you get version number for example i get 7.1then install like that

例如,在您获得版本号后,我获得7.1然后像这样安装

sudo apt-get install  php7.1-sqlite     //for laravel testing with sqlite
sudo apt-get install  php-mysql         //for default mysql
sudo apt-get install  php7.1-mysql      //for version based mysql 
sudo apt-get install  php7.1-common     //for other necessary package for php

and need to restart apache2

并且需要重启apache2

sudo service apache2 restart

回答by Farhad Hossen

You can try these following command

您可以尝试以下命令

php artisan cache:clear
php artisan config:clear  

Then restart your apache server. After then reconfigure your .envfile

然后重启你的apache服务器。然后重新配置您的.env文件

回答by abhishek subedi

Your database driver is missing. To solve the probelem

您的数据库驱动程序丢失。解决问题

First install the driver

首先安装驱动

For ubuntu: For mysql database.

对于 ubuntu:对于 mysql 数据库。

sudo apt-get install php5.6-mysql/php7.2-mysql

You also can search for other database systems.

您还可以搜索其他数据库系统。

You also can search for the driver:

您也可以搜索驱动程序:

sudo apt-cache search drivername

回答by Lotes Molapo

Ok I once experienced this error... I don't know why but it looks like in php 7.3 there is problem in relation to drivers or something there what I did was switch back to php 7.2 and everything worked fine...

好的,我曾经遇到过这个错误......我不知道为什么,但看起来在 php 7.3 中存在与驱动程序相关的问题或我所做的就是切换回 php 7.2 并且一切正常......

回答by Fatih TüZEN

clear artisan cache

清除工匠缓存

php artisan cache:clear

and

php artisan migrate:refresh

that's it

就是这样

回答by anoja madusanka

sudo apt install php7.2-pdo php7.2-mysql