小编典典

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

redis

在服务器中运行代码,env为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>

阅读 1284

收藏
2020-06-20

共1个答案

小编典典

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

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

但是,您的系统无法理解“ localhost”,在主机文件中输入内容,即说127.0.0.1是localhost。在/ etc / hosts中添加以下代码

127.0.0.1 localhost

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

r_server=redis.Redis(host="localhost",port=6379)
2020-06-20