Solution to 'getaddrinfo failed' error in Laravel.

Author
By Darío Rivera
Posted On in Laravel

The getaddrinfo failed error is a propagated PHP error that basically indicates that the host name could not be resolved when connecting to some service. It usually happens when the database, queues or cache host cannot be resolved. The first thing you should notice is in which service this error has occurred, below is an example of this error in the connection with redis.

RedisException 

  php_network_getaddresses: getaddrinfo failed: Name or service not known

  at vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:135
    131|         if (version_compare(phpversion('redis'), '5.3.0', '>=')) {
    132|             $parameters[] = Arr::get($config, 'context', []);
    133|         }
    134| 
  > 135|         $client->{($persistent ? 'pconnect' : 'connect')}(...$parameters);
    136|     }
    137| 
    138|     /**
    139|      * Create a new redis cluster instance.

      +26 vendor frames 
  27  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

In essence, it means that PHP could not initiate a connection with Redis due to the host name. Therefore, you must verify the data of your Redis connection.

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

Something very common that happens with Laravel, is that if you placed your cache driver with redis, that is, like this

CACHE_DRIVER=redis

And this process failed due to the previously seen error, even so in the application's bootstrap, the values of the configuration files were already cached, so if you try to change your cache driver or the same Redis configuration, it will be ignored leaving us in a loop of error since we cannot clear this configuration with cache:clear.

The solution to this problem consists of deleting the cache of the application's bootstrap, in summary, deleting the following file.

rm boostrap/cache/config.php

Acerca de Darío Rivera

Author

Application Architect at Elentra Corp . Quality developer and passionate learner with 10+ years of experience in web technologies. Creator of EasyHttp , an standard way to consume HTTP Clients.

LinkedIn Twitter Instagram

Sólo aquellos que han alcanzado el éxito saben que siempre estuvo a un paso del momento en que pensaron renunciar.