Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redis.exceptions.ConnectionError: Error -2 connecting to localhost:6379. Name or service not known

I have this error when I run my code in server, my env is debian, and Python2.7.3

Traceback (most recent call last):
  File "fetcher.py", line 4, in <module>
    import mirad.fetcher_tasks as tasks
  File "/home/mirad/backend/mirad/fetcher_tasks.py", line 75, in <module>
    redis_keys = r.keys('*')
  File "/home/mirad/backend/venv/local/lib/python2.7/site-packages/redis/client.py", line 863, in keys
    return self.execute_command('KEYS', pattern)
  File "/home/mirad/backend/venv/local/lib/python2.7/site-packages/redis/client.py", line 534, in execute_command
    connection.send_command(*args)
  File "/home/mirad/backend/venv/local/lib/python2.7/site-packages/redis/connection.py", line 532, in send_command
    self.send_packed_command(self.pack_command(*args))
  File "/home/mirad/backend/venv/local/lib/python2.7/site-packages/redis/connection.py", line 508, in send_packed_command
    self.connect()
  File "/home/mirad/backend/venv/local/lib/python2.7/site-packages/redis/connection.py", line 412, in connect
    raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error -2 connecting to localhost:6379. Name or service not known.

when I run redis-cli it works correctly without any error:

$ redis-cli 
127.0.0.1:6379> 
like image 934
Vahid Kharazi Avatar asked Aug 07 '14 06:08

Vahid Kharazi


People also ask

Could not connect to Redis at name or service not known?

Run docker inspect redis and examine the ports output, it will tell you what port it is accessible on as well as the IP. Note, however, that this will only be connectable over that IP from that host. To access it from off of the host you will need to use the port from the above command and the host's IP address.

How do I start Redis locally?

To start Redis client, open the terminal and type the command redis-cli. This will connect to your local server and now you can run any command. In the above example, we connect to Redis server running on the local machine and execute a command PING, that checks whether the server is running or not.

Can't connect to Redis?

Firewall restriction is another common reason that can trigger the “could not connect to Redis connection refused”. By default Redis server listen to the TCP port 6379. If another application is using the port or if the firewall restrictions blocks the port, it can trigger the connection refused error.


1 Answers

It seems that you are trying connect redis with server that is unidentified by your current Debian environment. From Traceback, I see you are trying to connect using host name as localhost ,

r_server=redis.Redis(host="localhost",port=6379)

But , your system is unable to understand "localhost" , make entry in hosts file i.e saying 127.0.0.1 is localhost. add below code in /etc/hosts

127.0.0.1 localhost

otherwise connect redis using below command ;

r_server=redis.Redis(host="127.0.0.1",port=6379) 
 
like image 187
Vidya Sagar Avatar answered Sep 20 '22 11:09

Vidya Sagar