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

时间:2022-09-15 18:10:04

Sometimes (about every 20 request) I get this error. But the next (next second), the same request, is fine. I dont know why it failed the first one. Sometimes i can get another error :

有时(大约每20个请求)我就会犯这个错误。但是下一个(下一秒),同样的请求,是可以的。我不知道为什么第一次失败了。有时我还会犯另一个错误:

No supported encrypter found. The cipher and / or key length are invalid.

不支持加密。密码和/或密钥长度无效。

My .env database parameters are fine.

我的.env数据库参数没有问题。

I have generated a key using php artisan key:generate

我使用php artisan键生成了一个键:generate

This key is in my .env file under a APP_KEY key

这个键在我的.env文件中APP_KEY键下

My config/app.php has a key 'key' => env('APP_KEY'), 'cipher' => 'AES-256-CBC'

我的配置/应用程序。php有一个key' key' => env('APP_KEY'), 'cipher' => 'AES-256-CBC'

does anyone have ANY idea how this can happen?

有人知道这是怎么发生的吗?

1 个解决方案

#1


0  

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

在过去的几天里我遇到了同样的问题,我想我解决了这个问题:

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.

env中的设置并不总是出于某种原因而使用,偶尔Laravel只会在config/app中使用默认设置。php和配置/ database.php。

config/app.php:

配置/ app.php:

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

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

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

将“SomeRandomString”更改为从您的.env中生成的密钥。

config/database.php

配置/ 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,
    ],

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.

从.env中将localhost、数据库、用户名、密码更改为您的实际设置。这个例子适用于MySQL,如果您使用另一个数据库,请更改这些变量。

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

可能有更好的解决方案(更安全?),但这正是迄今为止避免出现错误的原因。

#1


0  

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

在过去的几天里我遇到了同样的问题,我想我解决了这个问题:

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.

env中的设置并不总是出于某种原因而使用,偶尔Laravel只会在config/app中使用默认设置。php和配置/ database.php。

config/app.php:

配置/ app.php:

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

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

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

将“SomeRandomString”更改为从您的.env中生成的密钥。

config/database.php

配置/ 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,
    ],

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.

从.env中将localhost、数据库、用户名、密码更改为您的实际设置。这个例子适用于MySQL,如果您使用另一个数据库,请更改这些变量。

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

可能有更好的解决方案(更安全?),但这正是迄今为止避免出现错误的原因。