【问题标题】:How to fix migrate Laravel error 'PDOException and PDO::__construct'如何修复迁移 Laravel 错误“PDOException 和 PDO::__construct”
【发布时间】:2019-09-03 03:58:25
【问题描述】:

我正在尝试执行php artisan migrate 命令在phpmyadmin 中创建一个表

但它的显示错误

“找不到驱动程序”

我已经制作了php artisan serve,它成功了

 'mysql' => [
                'driver' => 'mysql',
                'host' => env('DB_HOST', '127.0.0.1'),
                'port' => env('DB_PORT', '3306'),
                'database' => env('DB_DATABASE', 'testdb'),
                'username' => env('DB_USERNAME', 'root'),
                'password' => env('DB_PASSWORD', 'xxxx'),
                'unix_socket' => env('DB_SOCKET', ''),
                'charset' => 'utf8mb4',
                'collation' => 'utf8mb4_unicode_ci',
                'prefix' => '',
                'prefix_indexes' => true,
                'strict' => true,
                'engine' => null,
                'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

 DB_CONNECTION=mysql
 DB_HOST=127.0.0.1
 DB_PORT=3306
 DB_DATABASE=testdb
 DB_USERNAME=root
 DB_PASSWORD=xxxx

这是命令行中显示的错误:

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

  at C:\AppServ\www\laravel2\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   PDOException::("could not find driver")
      C:\AppServ\www\laravel2\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70

  2   PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=testdb", "root", "xxxx", [])
      C:\AppServ\www\laravel2\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70

预计会显示“表创建成功”,并且也在phpmyadmin中创建

【问题讨论】:

  • 尝试打 composer dump-autoload, php artisan config:clear

标签: php laravel laravel-5 pdo


【解决方案1】:

config/database.php 文件根据此更改:

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

在这里查看:Link

打开.env 文件并进行编辑。只需设置正确的数据库凭据

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=            // Your Database Name
DB_USERNAME=           // Your Database Username
DB_PASSWORD=          // Your Database Password, If no password is set then just clear it

.env编辑后,必须清除缓存:php artisan config:cache

【讨论】:

    【解决方案2】:

    您缺少 PHP 的 php-mysql 驱动程序,您可以通过执行php -i | grep mysql 来检查它 如果您缺少它,则必须根据您的系统类型安装它,例如,如果您在 ubuntu 上使用 php7.2 系统,您应该这样做: sudo apt-get install php7.2-mysql

    【讨论】:

      【解决方案3】:

      如果你使用 homestead mysql 端口,请确保你的机器上正在运行 mysql 端口可能是 33060 ..

      澄清mysql是否正在运行在mac或linux上打开终端或在windows上打开命令行并写入 mysql -u root 如果你得到的错误意味着 mysql 没有运行。

      【讨论】:

        猜你喜欢
        • 2020-11-07
        • 1970-01-01
        • 2015-05-20
        • 2019-02-19
        • 2021-09-23
        • 1970-01-01
        • 2016-04-18
        • 2019-10-04
        • 1970-01-01
        相关资源
        最近更新 更多