53

My Laravel 5 has run OK until the database was configured, then found this error:

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

Doing some research it seems that I configured MySQL access too late, so I should restart the server in order to get the correct environment variables. Well, I'm using Dreamhost's shared server and I just can't do that.

How should I fix this issue?

Thanks

godsaway
  • 601
  • 1
  • 6
  • 9
  • You don't have to restart webserver in order to make Laravel reread it's .env file. Laravel does it during every run. – Maxim Lanin Sep 06 '15 at 11:57
  • @max.lanin so then if that's not the problem, if the credentials are correct, what is the problem? I'm running `php artisan migrate` so that I apply the migration – godsaway Sep 06 '15 at 14:12

8 Answers8

110

If you have run php artisan config:cache on your server, then your Laravel app could cache outdated config settings that you've put in the .env file.

Run php artisan config:clear to fix that.

Harry
  • 2,429
  • 4
  • 21
  • 26
  • 15
    Running `php artisan config:cache` actually *clears the cache* and *rebuilds it*, so I'd recommend that instead of just `config:clear` if you want to keep using the cache. **BUT** TIL that if you use the `env()` function outside of the cache files, you'll get `NULL` values when your config is cached. So make sure to ONLY use `env()` inside the files in your cache directory – Chris Apr 23 '18 at 20:25
37

I know this is old, but for local dev, this is what got things back to a production .env file:

rm bootstrap/cache/config.php

then

php artisan config:cache
php artisan config:clear
php artisan cache:clear
Janiis
  • 1,478
  • 1
  • 13
  • 19
  • 1
    The docs specifically state "You should typically run the php artisan config:cache command as part of your production deployment routine. The command should not be run during local development" – twigg Jul 05 '19 at 13:48
20

It's possible that your configuration variables are cached. Verify your config/app.php as well as your .env file then try

php artisan cache:clear

on the command line.

AurelienT
  • 351
  • 1
  • 4
  • 1
    Thanks but I already tried that. Anyway, if `.env` variables override `config/app.php` variables why should I check this file? – godsaway Sep 06 '15 at 14:14
  • They don't. The `config/app.php` file use them with the `env()` function. All config files do, really. So I'd check the `config/database.php` to see if the calls to `env()` use the correct names. – AurelienT Sep 06 '15 at 15:18
  • Let's see, if I have the app working locally with apache I should only tweak some values in the `.env` file in the server, right? And if nothing is changed inside the `config/`files everything should be working I guess. There's something I'm missing damn – godsaway Sep 06 '15 at 17:53
16

To be clear there are 4 types of caches you can clear depending upon your case.

php artisan cache:clear

You can run the above statement in your console when you wish to clear the application cache. What it does is that this statement clears all caches inside storage\framework\cache.

php artisan route:cache

This clears your route cache. So if you have added a new route or have changed a route controller or action you can use this one to reload the same.

php artisan config:cache 

This will clear the caching of the env file and reload it

php artisan view:clear 

This will clear the compiled view files of your application.

For Shared Hosting

Most of the shared hosting providers don't provide SSH access to the systems. In such a case you will need to create a route and call the following line as below:

Route::get('/clear-cache', function() {
    Artisan::call('cache:clear');
    return "All cache cleared";
});
Dibyendu Mitra Roy
  • 1,604
  • 22
  • 20
6

A short solution:

use Dotenv;

with(new Dotenv(app()->environmentPath(), app()->environmentFile()))->overload();
with(new LoadConfiguration())->bootstrap(app());

In my case I needed to re-establish database connection after altering .env programmatically, but it didn't work , If you get into this trouble try this

app('db')->purge($connection->getName()); 

after reloading .env , that's because Laravel App could have accessed the default connection before and the \Illuminate\Database\DatabaseManager needs to re-read config parameters.

MatteoOreficeIT
  • 190
  • 2
  • 5
3

In case anybody stumbles upon this question who cannot reload their webserver (long running console command like a queue runner) or needs to reload their .env file mid-request, i found a way to properly reload .env variables in laravel 5.

use Dotenv;
use InvalidArgumentException;

try {
    Dotenv::makeMutable();
    Dotenv::load(app()->environmentPath(), app()->environmentFile());
    Dotenv::makeImmutable();
} catch (InvalidArgumentException $e) {
    //
}
Question Mark
  • 3,557
  • 1
  • 25
  • 30
1

This is the only question I could find relating to reloading .env and thus config($key) values on an existing app instance, but many of these answers should probably live on a different question.

I got the following to work for a Laravel 6.x application, while trying to make an artisan command use my cypress environment variables.

For context $this->laravel === app().

    /**
     * Make the application use the cypress environment variables
     *
     * @return void
     */
    protected function enableCypressEnv()
    {
        // Get all of the original values from config. We need to do this because
        // rebuilding config will skip packages.
        $old = app(Repository::class)->all();

        // Change the applications env file
        $this->laravel->loadEnvironmentFrom('.env.cypress');

        // Reload the app's environment variables 
        Dotenv::create(
            $this->laravel->environmentPath(),
            $this->laravel->environmentFile(),
            Env::getFactory()
        )->overload();

        // Force config to be rebuitl with new env
        (new LoadConfiguration())->bootstrap($this->laravel);
        
        // Get all of the new values from buidling with new env
        $new = app(Repository::class)->all();

        // Merge the new values over the old distinctly
        $merged = array_merge_recursive_distinct($old, $new);

        // Get the applications configuration instance
        $config = config();

        // Overwrite all of the values in the container in accordance with the merged values
        foreach ($merged as $key => $value) {
            $config->set([$key => $value]);
        }
    }

Shoutout to the answers above for pointing me in the right direction, but I will include my answer here as none of them worked perfectly for me.

Kurt Friars
  • 3,625
  • 2
  • 16
  • 29
  • If you are wondering why one might want to do this, I have created the ability for cypress to access config file values by exporting them to a json file. Obviously this file will need to be built with the correct .env values configured. Since I will often be running this locally, I don't want to have to constantly rename my .env values, instead I can just let the command do it for me. – Kurt Friars Nov 02 '21 at 23:09
-11

In config/database.php I changed the default DB connection from mysql to sqlite. I deleted the .env file (actually renamed it) and created the sqlite file with touch storage/database.sqlite. The migration worked with sqlite.

Then I switched back the config/database.php default DB connection to mysql and recovered the .env file. The migration worked with mysql.

It doesn't make sense I guess. Maybe was something serverside.

godsaway
  • 601
  • 1
  • 6
  • 9
  • 10
    Even though it solved your problem, you shouldn't mark a "hack" as an answer - it encourages bad practices – zedling May 11 '18 at 12:29