【问题标题】:Laravel website not getting connected (Database connection failed)Laravel 网站未连接(数据库连接失败)
【发布时间】:2020-11-26 14:59:00
【问题描述】:

我是 Laravel 新手,目前正在从事 Laravel 项目。

当我在我的文件中使用 'php artisan serve' 来检查它是否在工作 /localhost 时,我收到了这个错误:

(1/1) HttpException 数据库连接失败!

在 Application.php 第 1014 行

在 Application->abort('399', '数据库连接失败!', array())in helpers.php 第 46 行

at abort('399', 'Database connection failed!') in ScriptMint.php 行 31

在 ScriptMint->handle(object(Request), object(Closure)) in Pipeline.php 第 171 行

在 Pipeline->Illuminate\Pipeline{closure}(object(Request))in XSSProtection.php 第 33 行

在 XSSProtection->handle(object(Request), object(Closure))in Pipeline.php 第 171 行

在 Pipeline->Illuminate\Pipeline{closure}(object(Request))in TransformsRequest.php 第 21 行

在 TransformsRequest->handle(object(Request), object(Closure))in Pipeline.php 第 171 行

在 Pipeline->Illuminate\Pipeline{closure}(object(Request))in ValidatePostSize.php 第 27 行

在 ValidatePostSize->handle(object(Request), object(Closure))in Pipeline.php 第 171 行

在 Pipeline->Illuminate\Pipeline{closure}(object(Request))in CheckForMaintenanceMode.php 第 62 行

在 CheckForMaintenanceMode->handle(object(Request), object(Closure))in Pipeline.php 第 171 行

在 Pipeline->Illuminate\Pipeline{closure}(object(Request))in TrustProxies.php 第 57 行

在 TrustProxies->handle(object(Request), object(Closure))in Pipeline.php 第 171 行

在 Pipeline->Illuminate\Pipeline{closure}(object(Request))in Pipeline.php 第 105 行

在Pipeline->then(object(Closure))in Kernel.php line 151

在 Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php 行 116

在 Kernel->handle(object(Request)) in index.php 第 55 行

在 require_once('C:\Users\User\Desktop\Project\website\public\index.php')in server.php 第 21 行

【问题讨论】:

  • 查看您的“.​​env”文件,凭据是否针对本地数据库正确设置?
  • ...数据库服务器是否启动并运行?

标签: php laravel


【解决方案1】:

检查项目根目录中的.env 文件。 它应该有这样的条目:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=<yourdatabase>
DB_USERNAME=<yourdatabaseuser>
DB_PASSWORD=<yourdatabaseuserpassword>

如果一切都适合您选择的连接,请检查文件database.php 中的config 文件夹。这是我的database.php 的一个例子。我没有从最初的项目设置中改变任何东西:

<?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,
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | 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'),
        ],

    ],

];

【讨论】:

  • 谁能告诉我如何进行本地主机和检查,因为我只是想检查它的外观,当它在 XAMPP 上进行本地化时。你能告诉我要进行哪些更改吗?我应该在哪里进行更改
【解决方案2】:

您必须在 laravel 安装的根文件夹中创建 .env 文件。

将此代码添加到您的 .env 文件并将 DB_DATABASE、DB_USERNAME 和 DB_PASSWORD 更改为您的数据库凭据。

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:cqFZ1xZXorL2iw/2m6CDH676fyLT+CdmOOBNUIo7tSs=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

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

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

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

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}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    • 2017-06-12
    • 2023-03-02
    • 2020-05-05
    • 2011-12-05
    相关资源
    最近更新 更多