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

后端 未结 12 1050
感动是毒
感动是毒 2020-12-28 12:33

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. Conn         


        
相关标签:
12条回答
  • 2020-12-28 13:14

    if anyone comes here trying to get django_rq to work after encountering either 99 or 111 errors try the following:

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

    this requires you to name the redis container like this in your docker-compose.yml

    services:
      app:
        build:
          context: .
        ports:
          - "8000:8000"
        volumes:
          - ./app:/app
        command: >
          sh -c   "python manage.py makemigrations && 
                  python manage.py migrate && 
                  python manage.py runserver 0.0.0.0:8000"
        depends_on:
          - redis
      redis:
        image: redis:6-alpine
        ports:
          - "6379:6379"
    
    0 讨论(0)
  • 2020-12-28 13:16

    This could happen when whatever application that is calling/connecting to redis, the environment variable it consumed in order to specify a connection hasn't been properly set - REDISCLOUD_URL or REDISTOGO_URL etc. This could most easily be that redis was started after the app or redis restarted and cycled its connection IP and/or access. So, upon deploying, insure redis is started prior to the downstream app(s)

    Insure redis is up and running and a simple reboot on the app could fix the issue OR, as other answers have indicated, refresh the app in the appropriate manner to re-fresh & re-consume the environment variable.

    0 讨论(0)
  • 2020-12-28 13:20

    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.

    0 讨论(0)
  • 2020-12-28 13:23

    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)
    
    0 讨论(0)
  • 2020-12-28 13:27

    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

    0 讨论(0)
  • 2020-12-28 13:28

    Right now Heroku automatically sets the environment variable REDIS_URL to URL + port.

    A convenient way to work with redis on heroku is to use a connection pool:

    settings.py

    import redis
    REDIS_DEFAULT_CONNECTION_POOL = redis.ConnectionPool.from_url(os.getenv('REDIS_URL', 'redis://localhost:6379/'))
    

    whererver.py

    from redis import Redis
    from myProject.settings import REDIS_DEFAULT_CONNECTION_POOL
    
    redis = Redis(connection_pool=REDIS_DEFAULT_CONNECTION_POOL)
    print(redis.keys())  # works
    
    0 讨论(0)
提交回复
热议问题