php artisan 迁移错误找不到驱动程序

Posted

技术标签:

【中文标题】php artisan 迁移错误找不到驱动程序【英文标题】:php artisan migrate error could not find driver 【发布时间】:2018-08-15 21:51:57 【问题描述】:

我在尝试运行 php artisan migrate 时遇到了 Illuminate\Database\QueryException : could not find driver 错误。

我环顾四周,但找不到解决此异常的方法。

我在 Windows 计算机上运行 Laravel 5.6 和 PHP 7.2 和 mysql 5.7。

这里是 .env 文件:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=larabiz
DB_USERNAME=root 
DB_PASSWORD=root

还有详细的错误日志:

php artisan migrate -v

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

  at C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Database\Connection.php: 664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) 
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         
    668|

  Exception trace:

  1   Doctrine\DBAL\Driver\PDOException::("could not find driver")
      C:\Users\Cyril\Projects\larabiz\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php : 47

  2   PDOException::("could not find driver")
      C:\Users\Cyril\Projects\larabiz\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php : 43

  3   PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=larabiz", "root", "root", [])
      C:\Users\Cyril\Projects\larabiz\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php : 43

  4   Doctrine\DBAL\Driver\PDOConnection::__construct("mysql:host=127.0.0.1;port=3306;dbname=larabiz", "root", "root", [])
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php : 65

  5   Illuminate\Database\Connectors\Connector::createPdoConnection("mysql:host=127.0.0.1;port=3306;dbname=larabiz", "root", "root", [])
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php : 44

  6   Illuminate\Database\Connectors\Connector::createConnection("mysql:host=127.0.0.1;port=3306;dbname=larabiz", [])
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Database\Connectors\MySqlConnector.php : 24

  7   Illuminate\Database\Connectors\MySqlConnector::connect()
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Database\Connectors\ConnectionFactory.php : 183

  8   Illuminate\Database\Connectors\ConnectionFactory::Illuminate\Database\Connectors\closure()
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Database\Connection.php : 915

  9   call_user_func(Object(Closure))
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Database\Connection.php : 915

  10  Illuminate\Database\Connection::getPdo()
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Database\Connection.php : 940

  11  Illuminate\Database\Connection::getReadPdo()
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Database\Connection.php : 399

  12  Illuminate\Database\Connection::getPdoForSelect()
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Database\Connection.php : 325

  13  Illuminate\Database\Connection::Illuminate\Database\closure("select * from information_schema.tables where table_schema = ? and table_name = ?")
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Database\Connection.php : 657

  14  Illuminate\Database\Connection::runQueryCallback("select * from information_schema.tables where table_schema = ? and table_name = ?", Object(Closure))
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Database\Connection.php : 624

  15  Illuminate\Database\Connection::run("select * from information_schema.tables where table_schema = ? and table_name = ?", Object(Closure))
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Database\Connection.php : 333

  16  Illuminate\Database\Connection::select("select * from information_schema.tables where table_schema = ? and table_name = ?")
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Database\Schema\MySqlBuilder.php : 18

  17  Illuminate\Database\Schema\MySqlBuilder::hasTable("migrations")
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Database\Migrations\DatabaseMigrationRepository.php : 169

  18  Illuminate\Database\Migrations\DatabaseMigrationRepository::repositoryExists()
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php : 545

  19  Illuminate\Database\Migrations\Migrator::repositoryExists()
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Database\Console\Migrations\MigrateCommand.php : 97

  20  Illuminate\Database\Console\Migrations\MigrateCommand::prepareDatabase()
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Database\Console\Migrations\MigrateCommand.php : 63

  21  Illuminate\Database\Console\Migrations\MigrateCommand::handle()
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php : 29

  22  call_user_func_array([])
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php : 29

  23  Illuminate\Container\BoundMethod::Illuminate\Container\closure()
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php : 87

  24  Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Object(Closure))
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php : 31

  25  Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), [])
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Container\Container.php : 564

  26  Illuminate\Container\Container::call()
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Console\Command.php : 183

  27  Illuminate\Console\Command::execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
      C:\Users\Cyril\Projects\larabiz\vendor\symfony\console\Command\Command.php : 252

  28  Symfony\Component\Console\Command\Command::run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Console\Command.php : 170

  29  Illuminate\Console\Command::run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      C:\Users\Cyril\Projects\larabiz\vendor\symfony\console\Application.php : 865

  30  Symfony\Component\Console\Application::doRunCommand(Object(Illuminate\Database\Console\Migrations\MigrateCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      C:\Users\Cyril\Projects\larabiz\vendor\symfony\console\Application.php : 241

  31  Symfony\Component\Console\Application::doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      C:\Users\Cyril\Projects\larabiz\vendor\symfony\console\Application.php : 143

  32  Symfony\Component\Console\Application::run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Console\Application.php : 88

  33  Illuminate\Console\Application::run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      C:\Users\Cyril\Projects\larabiz\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php : 121

  34  Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      C:\Users\Cyril\Projects\larabiz\artisan : 37

感谢您的帮助!

【问题讨论】:

Laravel: PDOException: could not find driver的可能重复 我支持@Alex 你从哪里运行命令? 【参考方案1】:

我在 Ubuntu 16.04 中遇到了类似的问题,对我有帮助的是我为 php 7.2 安装了 php-mysql。如果您有 php 7.2,我建议您运行以下命令,或者根据您的 PHP 版本安装 php mysql

apt-get install php7.2-mysql

systemctl restart apache2

【讨论】:

以上是关于php artisan 迁移错误找不到驱动程序的主要内容,如果未能解决你的问题,请参考以下文章

Laravel:PHP Artisan Migrate 抛出 PDO 异常:找不到驱动程序(Postgres)

php artisan migrate throwing [PDO Exception] 找不到驱动程序 - 使用 Laravel

php artisan migrate:[PDOException] 找不到驱动程序

php artisan migrate-laravel 的问题

PostgreSQL 迁移失败并出现错误消息“找不到驱动程序”

php: 加载 oracle 驱动程序给出错误“无法加载动态库 - 找不到指定的过程。”