3

EDIT : Thanks to @PedroFaria99, clear config cache solved the problem, but if anybody want to bring an explanation about the randomness aspect feel free.

I have an issue with my laravel 5.5 local installation (production environnement isn't impacted). Here Laravel is used as an API and serves a client-sided VueJS application.

Sometimes (randomly), my laravel is returning 500 error to my client. It can happens on various routes, never the same one, after 1 to 10 successives HTTP request or not and when I check the storage

[2018-03-09 13:44:08] production.ERROR: PDOException: SQLSTATE[HY000] [1045] Access Denied for user: 'forge'@'@localhost' (password: NO) in [...] Illuminate\Database\Connectors\Connector.php:119

However, my .env file is settup, and my database.php is using env() with default parameters are "forge" and "localhost". So I tried to change this parameter to "test", and the next 500 errors was same but with "test" instead of "forge".

I'm very confused, since this error doesn't happen systematicly.

.env file

APP_ENV=local
APP_DEBUG=true
DB_HOST=localhost
DB_DATABASE=mydatabase
DB_USERNAME=root
DB_PASSWORD=

database.php

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

...    
'connections' => [
        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'test'),
            'username'  => env('DB_USERNAME', 'test'),
            'password'  => env('DB_PASSWORD', '')

...

5
  • 1
    try php artisan key:generate to see if it fixes, fi not you may want to clear the config cache Commented Mar 9, 2018 at 14:24
  • Didn't thought about it, since it was randomness. Clear config cache seems to worked. Thanks ! But any idea about the fact it was pretty random ? Commented Mar 9, 2018 at 14:28
  • not really, it seems the server just freaks out sometimes, not sure Commented Mar 9, 2018 at 14:35
  • I belive you need to have it cached since it pretty much updates the file ./project/bootstrap/chache/config.php which seems to hold the cached values. I belive it works as a backup, in case laravel doesn't completly load the .env file before loading the the rest of the autoloaded files. Commented Mar 9, 2018 at 14:47
  • 1
    I have the same issue, I ended up putting the same configuration on both files and the problem (obviously) disappeared Commented Mar 9, 2018 at 15:59

2 Answers 2

1

Try

php artisan cache:clear
php artisan config:cache

It happens because laravel saves env values in its cache and if your config is not cached it will take the configuaration that are saved in the database.php file. You can not directly access env values on runtime.

Sign up to request clarification or add additional context in comments.

Comments

0

I hate to revive a dead thread but after some soul searching I found the issue might just be php's lack of thread safety : https://github.com/laravel/framework/issues/28571 and the great writeup here : https://mattallan.me/posts/how-php-environment-variables-actually-work/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.