简体   繁体   English

Yii2 异常 'yii\\db\\Exception' 带有消息“找不到驱动程序”

[英]Yii2 exception 'yii\db\Exception' with message 'could not find driver'

I have an advanced yii2 template.我有一个高级 yii2 模板。 I'm trying to create a console command.我正在尝试创建一个控制台命令。 I've created a controller class and action incide console/controllers folder:我创建了一个控制器类和操作包含控制台/控制器文件夹:

namespace console\controllers;

use yii\console\Controller;

class WorkModelController extends Controller
{
    public function actionValidate(){}
}

My action should connect with mysql database, select some data and do something with it.我的操作应该与 mysql 数据库连接,选择一些数据并用它做一些事情。 When I running the command: yii work-model/validate I get this error:当我运行命令时:yii work-model/validate 我收到这个错误:

C:\\OSPanel\\domains\\localhost>yii work-model/validate Exception 'yii\\db\\Exception' with message 'could not find driver' C:\\OSPanel\\domains\\localhost>yii work-model/validate Exception 'yii\\db\\Exception' 带有消息“找不到驱动程序”

in C:\\OSPanel\\domains\\localhost\\vendor\\yiisoft\\yii2\\db\\Connection.php:56在 C:\\OSPanel\\domains\\localhost\\vendor\\yiisoft\\yii2\\db\\Connection.php:56

My console/config/main.php and main-local.php files contain next db-config:我的 console/config/main.php 和 main-local.php 文件包含下一个 db-config:

'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=dbname',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],
];

When I trying connect to the database from another part of app all works fine, but when I trying connect from console I get this error.当我尝试从应用程序的另一部分连接到数据库时一切正常,但是当我尝试从控制台连接时出现此错误。

Please, help me slove this error.请帮我解决这个错误。

try to run php -me from the cli and make sure pdo_mysql is there.. if not, then u need to enable it from your php.ini config.尝试从 cli 运行php -me并确保 pdo_mysql 在那里..如果没有,那么你需要从你的 php.ini 配置中启用它。

Sometime, when u use server stack such as lamp/xampp, u probably missed out from resync your environment path to use the same version of php.ini of your server stack.有时,当您使用诸如 lamp/xampp 之类的服务器堆栈时,您可能会错过重新同步您的环境路径以使用相同版本的服务器堆栈的php.ini to do this, u may simply check/compare php.ini path from both phpinfo() from the browser and php -i from the cli为此,您可以简单地检查/比较来自浏览器的 phpinfo() 和来自 cli 的 php -i 的 php.ini 路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM