Laravel 5 error SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: NO)

后端 未结 3 547
走了就别回头了
走了就别回头了 2021-01-11 17:57

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 anot

相关标签:
3条回答
  • 2021-01-11 18:43

    I just experienced this after merging in changes from git without updating the .env file.

    Basically, the person changed the code so that the application required these keys in the env file:

    DB_SOME_DATABASE=something
    DB_SOME_USERNAME=something
    DB_SOME_PASSWORD=something
    

    But I still had the old credentials in there, so it "looked correct", but Laravel was throwing this error that made it look like the env file wasn't being used.

    This would indicate that, if you see this, check very closely if anything is spelled wrong in your .env file that would cause Laravel to attempt to use the default values (ie: look in the folder config/database.php where you see 'forge').

    It may not be a typo. Someone may have added a database connection and maybe you haven't updated your .env file yet.

    0 讨论(0)
  • 2021-01-11 18:44

    Just cache your config using

    php artisan config:cache
    

    Don't forget to do this every time after setting your .env file.

    0 讨论(0)
  • 2021-01-11 18:51

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

    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:

    'key' => env('APP_KEY', 'SomeRandomString'),
    
    'cipher' => 'AES-256-CBC',
    

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

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

    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.

    0 讨论(0)
提交回复
热议问题