Error 111 connecting to localhost:6379. Connection refused. Django Heroku

匿名 (未验证) 提交于 2019-12-03 03:05:02

问题:

I am able to run redis locally and everything works.

However when I deploy to heroku I get this error:

Error 111 connecting to localhost:6379. Connection refused.  

I have set up a Procfile with...

web: gunicorn odb.wsgi --log-file - worker: python worker.py 

I have a worker.py file...

import os import urlparse from redis import Redis from rq import Worker, Queue, Connection  listen = ['high', 'default', 'low']  redis_url = os.getenv('REDISTOGO_URL') if not redis_url:     raise RuntimeError('Set up Redis To Go first.')  urlparse.uses_netloc.append('redis') url = urlparse.urlparse(redis_url) conn = Redis(host=url.hostname, port=url.port, db=0, password=url.password)  if __name__ == '__main__': with Connection(conn):     worker = Worker(map(Queue, listen))     worker.work() 

A REDISTOGO_URL variable appears in the heroku config.

Redis to go is an installed add-on for my app.

Does REDISTOGO_URL have to be defined in settings.py? Why is heroku trying to connect to the local host when it is not even defined in worker.py?

回答1:

May be not directly related to your question but I was facing same error and it turn out that on my system redis-server package was not installed.

Problem was resolved with sudo apt-get install redis-server



回答2:

The solution is sudo apt-get install redis-server. Don't forget to start your service by sudo service redis-server start and you can use the command sudo service redis-server {start|stop|restart|force-reload|status} for reference



回答3:

Turns out I needed to set up things like this for it to work on Heroku.

redis_url = os.getenv('REDISTOGO_URL')  urlparse.uses_netloc.append('redis') url = urlparse.urlparse(redis_url) conn = Redis(host=url.hostname, port=url.port, db=0, password=url.password) 


回答4:

If you are using django_rq, a configuration like this will work for you:

RQ_QUEUES = {     'default': {          'HOST': 'localhost',          'PORT': '6379',          'URL': os.getenv('REDISTOGO_URL', 'redis://localhost:6379'),  # If you're          'DB': 0,          'DEFAULT_TIMEOUT': 480,      } } 

It will make that work on your local environment and also on Heroku!



回答5:

Error 111 is thrown when the application is unable to contact Redis. I had the same problem following the Heroku Django Channels tutorial. The settings.py file should read:

CHANNEL_LAYERS = {     "default": {         "BACKEND": "asgi_redis.RedisChannelLayer",         "CONFIG": {             "hosts": [os.environ.get('REDISCLOUD_URL', 'redis://localhost:6379')],         },         "ROUTING": "chat.routing.channel_routing",     }, } 

REDISCLOUD_URL instead of REDIS_URL.

Ensure Redis is installed on the Heroku server.



回答6:

I was facing same the error

  • Maybe radis server was not installed in your environment

    sudo apt-get install redis-server

  • I needed to set up things like this in settings.py

    redis_host = os.environ.get('REDIS_HOST', 'localhost')     # Channel layer definitions # http://channels.readthedocs.org/en/latest/deploying.html#setting-up-a-channel-backend CHANNEL_LAYERS = {     "default": {         # This example app uses the Redis channel layer implementation asgi_redis         "BACKEND": "asgi_redis.RedisChannelLayer",         "CONFIG": {             "hosts": [(redis_host, 6379)],         },         "ROUTING": "multichat.routing.channel_routing",     }, } 
  • Before

  • After



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