如何修复Laravel迁移错误'PDOException and PDO :: __ construct'

时间:2019-04-12 11:33:08

标签: php laravel laravel-5 pdo

我正在尝试执行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中创建

3 个答案:

答案 0 :(得分:0)

如果您使用的是宅基地,请确保mysql在您的计算机上运行mysql端口可能是33060 ..

阐明mysql是否正在运行,请在Mac或Linux上打开终端,或在Windows上打开命令行,然后编写 mysql -u root 如果出现错误,则表示mysql未运行。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

根据以下内容更改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