【问题标题】:database connection in laravellaravel 中的数据库连接
【发布时间】:2018-09-09 15:15:49
【问题描述】:

我不知道为什么 laravel 没有连接到我的数据库,但我认为我的配置是正确的。 我得到的错误是“SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)” 这是我的 .env

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

还有我的database.php:

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

     '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' => '',
        'strict' => true,
        'engine' => null,
      ],

我错过了什么吗?

【问题讨论】:

  • 这里的信息真的不够多。您已经注意到连接尝试是使用用户名“homestead”进行的,但您共享的信息并没有暗示为什么会出现这种情况。您确定您提供的是正确的文件吗?
  • 您的任何配置文件或.env 文件(仍然)是否包含用户名“homestead”?
  • 如果您使用php artisan serve 运行您的应用程序并编辑您的.env 文件,您需要重新启动服务器:CTRL + C 并再次运行php artisan serve

标签: php laravel


【解决方案1】:

尝试替换

    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',

    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',

然后从您的项目根目录执行:

php artisan cache:clear

告诉我你得到了什么。

【讨论】:

    【解决方案2】:

    用下面的代码替换 database.php 的内容。看来mysql键必须在connections键里面。

    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',
                'database' => env('DB_DATABASE', database_path('database.sqlite')),
                'prefix' => '',
            ],
    
            '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' => '',
                'strict' => true,
                'engine' => null,
            ],
    
            'pgsql' => [
                'driver' => 'pgsql',
                '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' => '',
                'schema' => 'public',
                'sslmode' => 'prefer',
            ],
    
            'sqlsrv' => [
                'driver' => 'sqlsrv',
                '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' => '',
            ],
    
        ],
    
        /*
        |--------------------------------------------------------------------------
        | 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 set of commands than a typical key-value systems
        | such as APC or Memcached. Laravel makes it easy to dig right in.
        |
        */
    
        'redis' => [
    
            'client' => 'predis',
    
            'default' => [
                'host' => env('REDIS_HOST', '127.0.0.1'),
                'password' => env('REDIS_PASSWORD', null),
                'port' => env('REDIS_PORT', 6379),
                'database' => 0,
            ],
    
        ],
    
    ];
    

    【讨论】:

    • 我得到了同样的错误,尽管我改变了它,laravel 仍然考虑默认用户名'homestead@localhhost'
    • @ⵍⵢⴻⵙ 我已将这部分从答案中移至此处:“请发送您收到的异常。我们无法理解您的代码有什么问题只看 .env”
    • 您需要清除缓存。在你的 laravel 项目根目录中使用这个命令:php artisan config:cache
    猜你喜欢
    • 2016-04-15
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 2021-10-14
    相关资源
    最近更新 更多