11

当我在服务器中运行代码时出现此错误,我的环境是 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> 
4

2 回答 2

14

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

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

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

127.0.0.1 localhost

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

r_server=redis.Redis(host="127.0.0.1",port=6379) 
 
于 2014-10-27T15:36:48.690 回答
0

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

于 2020-11-09T09:50:39.163 回答