Error 111 connecting to localhost:6379. Connection

2020-02-20 07:00发布

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?

8条回答
仙女界的扛把子
2楼-- · 2020-02-20 07:29

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楼-- · 2020-02-20 07:32

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.

查看更多
Root(大扎)
4楼-- · 2020-02-20 07:34

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 enter image description here

  • After enter image description here

查看更多
你好瞎i
5楼-- · 2020-02-20 07:34

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)
查看更多
▲ chillily
6楼-- · 2020-02-20 07:40

I also landed here with the following problem.

Trying again in 2.00 seconds...

[2019-06-10 07:25:48,432: ERROR/MainProcess] consumer: Cannot connect to redis://localhost:6379//: Error 111 connecting to localhost:6379. Connection refused..
Trying again in 4.00 seconds...

[2019-06-10 07:25:52,439: ERROR/MainProcess] consumer: Cannot connect to redis://localhost:6379//: Error 111 connecting to localhost:6379. Connection refused..
Trying again in 6.00 seconds...

[2019-06-10 07:25:58,447: ERROR/MainProcess] consumer: Cannot connect to redis://localhost:6379//: Error 111 connecting to localhost:6379. Connection refused..
Trying again in 8.00 seconds...

I realized the problem was the ufw which was denying the connections. Therefore, I allowed connections at this port using the following command.

sudo ufw alloww 6379

查看更多
Luminary・发光体
7楼-- · 2020-02-20 07:47

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!

查看更多
登录 后发表回答