redis.exceptions.ConnectionError:错误-2连接到localhost:6379。名称或服务未知

时间:2014-08-07 06:51:14

标签: python redis database-connection redis-py

我在服务器中运行代码时遇到此错误,我的环境是debian,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.

当我运行redis-cli时,它正常工作,没有任何错误:

$ redis-cli 
127.0.0.1:6379> 

2 个答案:

答案 0 :(得分:8)

您似乎正在尝试使用当前Debian环境无法识别的服务器连接redis。 从Traceback,我看到你正在尝试使用主机名连接为localhost,

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

但是,您的系统无法理解&#34; localhost&#34; ,在hosts文件中输入,即127.0.0.1是localhost。在/ etc / hosts中添加以下代码

127.0.0.1 localhost

否则使用以下命令连接redis;

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

答案 1 :(得分:0)

我遇到了类似的问题,后来意识到我还没有通过以下命令在终端中打开Redis:$ redis-server。 也许是这样。