当前位置: 首页 > 知识库问答 >
问题:

Laravel网站未连接(数据库连接失败)

夏星阑
2023-03-14

我是拉威尔的新手,目前正在拉威尔的一个项目中工作。

当我在我的文件中使用'php工匠服务'来检查它是否工作/localhost,我得到了这个错误:

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

在应用中。php第1014行

应用时-

在ScriptMint中中止时('399','数据库连接失败!')。php第31行

在ScriptMint-

在管道上-

at XSP保护-

在管道上-

在TransformsRequest-

在管道上-

在ValidatePostSiz-

在管道上-

在检查形式维护模式下-

在管道上-

委托代理-

在管道上-

在管道上-

在内核-

在内核-

在服务器中的require_once('C:\Users\User\Desktop\Project\website\public\index.php')。php第21行

共有2个答案

许展鹏
2023-03-14

您必须在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}"
白高超
2023-03-14

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

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

同时检查文件数据库中的config文件夹。php,如果所有内容都适合您选择的连接。下面是我的数据库中的一个示例。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'),
        ],

    ],

];
 类似资料:
  • 问题内容: 我正在Laravel 5(.1)中创建一个应用程序,在该应用程序中需要连接到不同的数据库。唯一的问题是,它不知道必须连接到哪个数据库,因此无法在config中使用database.php。控制器负责使用动态给定的连接详细信息进行连接。 如何建立与数据库的新连接,包括使用DB类?(或者这可能) 提前致谢! 问题答案: 最简单的解决方案是在运行时设置数据库配置。Laravel可能希望从文件

  • 我使用Laravel 5和我的数据库连接有问题: 这是我的database.php文件: 这是我的. env文件: DB_HOST=localhost DB_数据库=我的_场景 DB_USERNAME=root DB_PASSWORD=null 然而,当我运行“php artisan migrate”时,我得到了一个错误 [PDOException]SQLSTATE[HY000][1049]未知数

  • 连接数据库 在能够对MongDB进行操作之前,需要使用BuguFramework创建一个数据库连接,代码如下: BuguConnection conn = BuguFramework.getInstance().createConnection(); conn.connect("192.168.0.100", 27017, "mydb", "username", "password"); 也可以

  • 一、全局配置定义 return array( 'DB_TYPE' => 'mysql', 'DB_HOST' => '127.0.0.1', 'DB_NAME' => 'thinkcmf', 'DB_USER' => 'root', 'DB_PWD' => 'root', 'DB_PORT' => '3306', 'DB_PREFIX' =>

  • ThinkPHP内置了抽象数据库访问层,把不同的数据库操作封装起来,我们只需要使用公共的Db类进行操作,而无需针对不同的数据库写不同的代码和底层实现,Db类会自动调用相应的数据库驱动来处理。数据库抽象访问层基于PDO方式,目前内置包含了Mysql、SqlServer、PgSQL、Sqlite等数据库的支持。 如果应用需要使用数据库,必须配置数据库连接信息,数据库的配置文件有多种定义方式。 配置文件

  • 我试图连接到我的mongo数据库在我的远程服务器: 但我有一个错误: MongoDB外壳版本:2.6.10连接到:xxx。xxx。xxx。xx:27017/测试2016-11-07T05:18:39.140 0000警告:无法连接到xxx。xxx。xxx。xx:27017,原因:错误号:111连接被拒绝2016-11-07T05:18:39.142 0000错误:无法连接到服务器xxx。xxx。x