【问题标题】:exception 'PDOException' with message 'could not find driver' SQLSRV Laravel Artisan CLI带有消息“找不到驱动程序”SQLSRV Laravel Artisan CLI 的异常“PDOException”
【发布时间】:2014-05-01 04:33:48
【问题描述】:

我在 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 使用应用程序时我可以毫无问题地连接;

为什么 PDO 连接通过 http 可以正常工作,但在 CLI 中却不行? 请注意我使用的是完全相同的代码集,一个是从 Laravel 控制器调用的,另一个是从 Laravel 命令调用的。

【问题讨论】:

    标签: php sql-server laravel command-line-interface


    【解决方案1】:

    上面的答案是正确的,但只是为了澄清:

    1. 打开你的控制台并执行:

      php -i | grep "加载的配置文件"

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

    3. 转到那个文件,并确保它有这个:

      extension=php_pdo_mysql.dll

    4. 重新启动服务器并重试。这对我有用,希望对您有所帮助。

    【讨论】:

    • 澄清一点,重启服务器也意味着重启php-fpm,如果你正在使用的话。
    【解决方案2】:

    php cli 和 php cgi 使用不同的 php.ini 文件。试试php -i | grep pdo看看是否为你的cli安装了pdo,见php -i | grep "Loaded Configuration File"

    【讨论】:

    • 谢谢,您为我指明了正确的方向。我不得不使用 Windows 命令 php -i |查找 /i "配置文件" 。原来 CLI 使用的是另一个版本的 php。我更改了 PATH 变量,现在一切正常。
    • 也可以只使用 php --ini(跨平台工作)来获取 CLI .ini 文件位置。
    • 有没有办法在运行 MAMP 的 mac 上通过终端安装 mssql?
    • @MichelAyres 您应该创建一个顶级问题,而不是在 cmets 中提问。这就是使 SO 工作得最好的原因。
    猜你喜欢
    • 2015-03-12
    • 2014-01-23
    • 2016-07-30
    • 2017-02-13
    • 1970-01-01
    • 2012-04-22
    • 2015-01-03
    • 2020-01-21
    • 1970-01-01
    相关资源
    最近更新 更多