exception 'PDOException' with message 'could not find driver' SQLSRV Laravel Artisan CLI

29,769

Solution 1

php cli and php cgi use different php.ini files. Try php -i | grep pdo to see if pdo is installed for your cli, see php -i | grep "Loaded Configuration File"

Solution 2

The answer above is correct, but just to clarify:

  1. Open your console and execute:

    php -i | grep "Loaded Configuration File"

  2. That will give you the folder directory where you php-cli.ini is (Laravel Commands use a different php.ini than the one used by HTTP requests)

  3. Go to that file, and make sure it has this:

    extension=php_pdo_mysql.dll

  4. Restart your server and try again. That worked for me, hope it helps.

Share:
29,769
Samuel Hawksby-Robinson
Author by

Samuel Hawksby-Robinson

I'm an impassioned developer of things, particularly if they are Internet of Things (IoT) things or Ethereum blockchain things, bonus points if they are both things. My background is in developing web applications, on which I've built my business Samyoul Ltd. I massively enjoy participating in hackathons and projects oriented around IoT, blockchain and/or data analysis. I develop in a professional capacity, but also just for fun. Currently Ethereum blockchain applications and peripheral systems are becoming an ever increasingly large part of what I work on and also what I tinker with in my spare time (when there is any).

Updated on October 15, 2020

Comments

  • Samuel Hawksby-Robinson
    Samuel Hawksby-Robinson over 3 years

    I am running php 5.5 on a Windows Server 2008. I am using the Laravel 4.0 framework for the application. I am connecting to a SQL SRV database via PDO, when executed via http there is no issue, connections are made data is called and updated without issue.

    However when calling a command from Artisan (Laravel's CLI tool) I get the following error in the log:

    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} [] []
    

    The PDO SQLSRV lib is installed, see the below screenshot of the phpinfo(), and as I've already said I can connection without issue when using the application via http;

    My php info.

    Why is the PDO connection working fine via http but not in CLI? Please note I'm using the exact same set of code, one is called from a Laravel controller, the other from a Laravel command.

  • Samuel Hawksby-Robinson
    Samuel Hawksby-Robinson over 10 years
    Thank you, you pointed me in the right direction. I had to use the Windows command php -i | find /i "Configuration File" . It turned out the CLI was using another version of php. I changed the PATH variable and now it all works good.
  • Leith
    Leith almost 10 years
    Could also just use php --ini (which works cross-platform) to get the CLI .ini file location.
  • Michael
    Michael about 9 years
    Just to clarify a bit more, restarting the server also means restarting php-fpm, if that's what you're using.
  • Michel Ayres
    Michel Ayres over 8 years
    Is there a way to install mssql by terminal on a mac running MAMP?
  • Victory
    Victory over 8 years
    @MichelAyres you should create a top level question, instead of asking in the comments. That's what makes SO work the best.