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

匿名 (未验证) 提交于 2019-12-03 00:52:01

问题:

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>  

回答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="localhost",port=6379)  


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!