异常 'PDOException',消息为 '找不到驱动程序' SQLSRV Laravel Artisan CLI

5
我在 Windows Server 2008 上运行 php 5.5,并使用 Laravel 4.0 框架。通过 PDO 连接 SQL SRV 数据库,当通过 http 执行时没有问题,可以连接数据并更新。但是,当从 Artisan(Laravel 的 CLI 工具)调用命令时,日志中出现以下错误:
log.ERROR: exception 'PDOException' with message 'could not find driver' in C:\inetpub\MBSWeb\postsrch\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:47
Stack trace:
#0 C:\inetpub\MBSWeb\postsrch\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php(47): PDO->__construct('sqlsrv:Server=H...', 'xxxxxx', 'xxxxxx', Array)
#1 C:\inetpub\MBSWeb\postsrch\vendor\laravel\framework\src\Illuminate\Database\Connectors\SqlServerConnector.php(29): Illuminate\Database\Connectors\Connector->createConnection('sqlsrv:Server=H...', Array, Array)
#2 C:\inetpub\MBSWeb\postsrch\vendor\laravel\framework\src\Illuminate\Database\Connectors\ConnectionFactory.php(41): Illuminate\Database\Connectors\SqlServerConnector->connect(Array)
#3 C:\inetpub\MBSWeb\postsrch\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php(128): Illuminate\Database\Connectors\ConnectionFactory->make(Array, 'sqlsrv')
#4 C:\inetpub\MBSWeb\postsrch\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php(64): Illuminate\Database\DatabaseManager->makeConnection('sqlsrv')
#5 C:\inetpub\MBSWeb\postsrch\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php(242): Illuminate\Database\DatabaseManager->connection()
#6 C:\inetpub\MBSWeb\postsrch\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php(206): Illuminate\Database\DatabaseManager->__call('table', Array)
#7 C:\inetpub\MBSWeb\postsrch\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php(206): Illuminate\Database\DatabaseManager->table('bb_job_details')
#8 C:\inetpub\MBSWeb\postsrch\app\models\Job.php(214): Illuminate\Support\Facades\Facade::__callStatic('table', Array)
#9 C:\inetpub\MBSWeb\postsrch\app\models\Job.php(214): Illuminate\Support\Facades\DB::table('bb_job_details')
#10 C:\inetpub\MBSWeb\postsrch\app\models\Cron.php(18): Job::getJobDetailsByStatus()
#11 C:\inetpub\MBSWeb\postsrch\app\controllers\ApplicantController.php(14): Cron::ChangePostingJobStatus()
#12 C:\inetpub\MBSWeb\postsrch\app\commands\GetApplicantsCommand.php(45): ApplicantController->index()
#13 C:\inetpub\MBSWeb\postsrch\vendor\laravel\framework\src\Illuminate\Console\Command.php(108): GetApplicantsCommand->fire()
#14 C:\inetpub\MBSWeb\postsrch\vendor\symfony\console\Symfony\Component\Console\Command\Command.php(244): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#15 C:\inetpub\MBSWeb\postsrch\vendor\laravel\framework\src\Illuminate\Console\Command.php(96): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#16 C:\inetpub\MBSWeb\postsrch\vendor\symfony\console\Symfony\Component\Console\Application.php(897): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#17 C:\inetpub\MBSWeb\postsrch\vendor\symfony\console\Symfony\Component\Console\Application.php(191): Symfony\Component\Console\Application->doRunCommand(Object(GetApplicantsCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#18 C:\inetpub\MBSWeb\postsrch\vendor\symfony\console\Symfony\Component\Console\Application.php(121): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#19 C:\inetpub\MBSWeb\postsrch\artisan(59): Symfony\Component\Console\Application->run()
#20 {main} [] []
[2014-03-23 22:53:13] log.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to undefined function Symfony\Component\Console\mb_detect_encoding()' in C:\inetpub\MBSWeb\postsrch\vendor\symfony\console\Symfony\Component\Console\Application.php:721
Stack trace:
#0 [internal function]: Illuminate\Exception\Handler->handleShutdown()
#1 {main} [] []

PDO SQLSRV库已安装,如下图所示的phpinfo(),我已经说过,当通过http使用应用程序时,可以毫无问题地连接;
请注意,我正在使用完全相同的代码,一个是从Laravel控制器调用的,另一个是从Laravel命令调用的。为什么通过http可以正常工作,但在CLI中却不行呢?
2个回答

9

PHP CLI和PHP CGI使用不同的php.ini文件。尝试php -i | grep pdo查看您的CLI是否安装了pdo,查看php -i | grep "Loaded Configuration File"


1
谢谢,你指引了我正确的方向。我不得不使用Windows命令php -i | find /i "Configuration File"。结果发现CLI正在使用另一个版本的php。我更改了PATH变量,现在一切都很好。 - Samuel Hawksby-Robinson
1
也可以使用 php --ini 命令(跨平台可用)来获取 CLI 的 .ini 文件位置。 - Leith
在运行MAMP的Mac上,有没有通过终端安装mssql的方法? - Michel Ayres
@MichelAyres,你应该创建一个顶级问题,而不是在评论中提问。这就是使SO最好的方式。 - Victory

2
上面的答案是正确的,但是为了澄清一下:
  1. 打开控制台并执行:

    php -i | grep "Loaded Configuration File"

  2. 这将给出您的php-cli.ini所在的文件夹目录(Laravel命令使用与HTTP请求使用的不同的php.ini)

  3. 转到该文件,并确保它具有以下内容:

    extension=php_pdo_mysql.dll

  4. 重新启动服务器并重试。这对我有用,希望能帮到你。


只是为了更加明确一点,重新启动服务器也意味着重新启动php-fpm,如果您正在使用它的话。 - Michael

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接