【问题标题】:Laravel Illuminate\Database\QueryException (No such file or directory) when using the command "php artisan migrate"使用命令“php artisan migrate”时,Laravel Illuminate\Database\QueryException(没有这样的文件或目录)
【发布时间】:2020-08-16 07:23:19
【问题描述】:

我在 VPS 上安装了 Laravel。我通过 SSH 用我的计算机连接到它。

“php artisan migrate”命令返回此错误:

   Illuminate\Database\QueryException 

  SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
    667|         // If an exception occurs when attempting to run a query, we'll format the error
    668|         // message to include the bindings with SQL, which will make this exception a
    669|         // lot more helpful to the developer instead of just the database's errors.
    670|         catch (Exception $e) {
  > 671|             throw new QueryException(
    672|                 $query, $this->prepareBindings($bindings), $e
    673|             );
    674|         }
    675| 

我的 .env 文件如下所示:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:secret
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

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

BROADCAST_DRIVER=redis
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

LARAVEL_ECHO_PORT=6001

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

我的数据库文件如下所示:

<?php

use Illuminate\Support\Str;

return [

    /*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish
    | to use as your default connection for all database work. Of course
    | you may use many connections at once using the Database library.
    |
    */

    'default' => env('DB_CONNECTION', 'mysql'),

    /*
    |--------------------------------------------------------------------------
    | Database Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the database connections setup for your application.
    | Of course, examples of configuring each database platform that is
    | supported by Laravel is shown below to make development simple.
    |
    |
    | All database work in Laravel is done through the PHP PDO facilities
    | so make sure you have the driver for your particular database of
    | choice installed on your machine before you begin development.
    |
    */

    'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'url' => env('DATABASE_URL'),
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
            'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
        ],

        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            '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'),
            ]) : [],
        ],

        'pgsql' => [
            'driver' => 'pgsql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],

        'sqlsrv' => [
            'driver' => 'sqlsrv',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
        ],

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            'queue' => '{default}',
            'retry_after' => 90,
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | Migration Repository Table
    |--------------------------------------------------------------------------
    |
    | This table keeps track of all the migrations that have already run for
    | your application. Using this information, we can determine which of
    | the migrations on disk haven't actually been run in the database.
    |
    */

    'migrations' => 'migrations',

    /*
    |--------------------------------------------------------------------------
    | Redis Databases
    |--------------------------------------------------------------------------
    |
    | Redis is an open source, fast, and advanced key-value store that also
    | provides a richer body of commands than a typical key-value system
    | such as APC or Memcached. Laravel makes it easy to dig right in.
    |
    */

    'redis' => [

        'client' => env('REDIS_CLIENT', 'phpredis'),

        'options' => [
            'cluster' => env('REDIS_CLUSTER', 'redis'),
            'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
        ],

        'default' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),
            'database' => env('REDIS_DB', '0'),
        ],

        'cache' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),
            'database' => env('REDIS_CACHE_DB', '1'),
        ],

    ],

];

这两个都有意义吗?我应该使用本地主机吗? Redis 和 Laravel 都在同一台服务器上。我已经尝试过帮助人们的常用方法,比如命令: php artisan config:cache php artisan cache:clear

【问题讨论】:

  • 你会生成 App Key 吗? php工匠密钥:生成
  • 是的,我只是在这篇文章中将其替换为“秘密”。
  • Laravel 到目前为止一直很糟糕。我很惊讶它应该有如此广泛的用途,但到目前为止,它对我来说只是个问题。这不寻常吗?还有比 Laravel 更好的选择吗?

标签: laravel


【解决方案1】:

还有很多事情要检查:

  1. 如果 mysql 容器已使用正确的数据集启动。我的 .sql 脚本(在卷映射下)是空的。如果您不保存脚本,VSCode 会将其保留为空。

  2. 您也可以使用:docker-compose exec container-name 登录到容器中,以检查 init 数据库文件是否存在。

  3. 之后,尝试建立与 mysql 的连接,例如使用命令行:mysql -u username -D database -p。检查数据是否存在。如果返回任何内容,请尝试上面的查询。

  4. 在解决这一切之后。要检查的另一件事是 .env 文件中的变量 DB_HOST。我的是:

  • DB_HOST=本地主机
  • DB_HOST=127.0.0.1

然后改为:

  • DB_HOST=db #db 是 mysql 镜像的容器名
  1. 最后但同样重要的是,以下 docker-compose 命令非常有用(应用程序是您的容器名称或应用程序名称):

    • docker-compose up -d
    • docker-compose exec 应用作曲家安装
    • docker-compose exec app php artisan key:generate
    • docker-compose exec app php artisan config:clear
    • docker-compose exec app php artisan migrate:install

希望这会对处于相同情况的人有所帮助!

【讨论】:

    【解决方案2】:

    运行 php artisan config:clear 而不是 config:cachecache:clear

    然后php artisan migrate:install

    【讨论】:

    • 我做到了,现在它给了我错误“SQLSTATE[HY000] [2002] Connection denied (SQL: create table migrations (id int unsigned not null auto_increment primary key, migration varchar(255) not null, batch int not null) 默认字符集 utf8mb4 collat​​e 'utf8mb4_unicode_ci'"
    • 是时候放弃 Laravel 并尝试更可靠的东西了吗?我不断遇到错误和其他类似的挂断。
    • @ChewieTheChorkie 请问您的问题有什么解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 2020-01-20
    • 2020-04-19
    • 2021-12-11
    • 2021-12-28
    • 2017-09-26
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多