且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel 5错误SQLSTATE [HY000] [1045]用户'forge'@'localhost'的访问被拒绝(使用密码:NO)

更新时间:2021-08-04 21:22:50

在过去的几天中,我遇到了完全相同的问题,我想我已经解决了:

I had this exact same problem for the past few days and I think I solved it:

.env中的设置由于某些原因并非总是使用,偶尔Laravel只会使用config/app.php和config/database.php中的默认设置.

The settings in .env are not always used for some reason or other and occasionally Laravel will just use the default settings in config/app.php and config/database.php.

config/app.php:

config/app.php:

'key' => env('APP_KEY', 'SomeRandomString'),

'cipher' => 'AES-256-CBC',

将'SomeRandomString'更改为从您的.env生成的密钥

Change the 'SomeRandomString' to the generated key from your .env

config/database.php

config/database.php

'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => env('DB_DATABASE', 'forge'),
        'username'  => env('DB_USERNAME', 'forge'),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

从.env将localhost,数据库,用户名,密码更改为您的实际设置.如果使用其他数据库,则此示例适用于MySQL,请改为更改这些变量.

Change localhost, database, username, password to your actual settings from the .env. This example is for MySQL if you use another database, change those variables instead.

可能有更好的解决方案(更安全吗?),但是到目前为止,这仍使错误不会出现.

There might be a better solution (more secure?) but this is what so far kept the error from showing up.