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

20,166

Solution 1

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) 
 

Solution 2

I faced a similar problem and later on realized that I have not opened redis in the terminal by command: $ redis-server. Maybe it is the case.

Share:
20,166
Vahid Kharazi
Author by

Vahid Kharazi

Updated on January 25, 2021

Comments

  • Vahid Kharazi
    Vahid Kharazi over 3 years

    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>