Python 连接到 localhost:6379 时出现错误 111。拒绝连接。姜戈赫鲁库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36088409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error 111 connecting to localhost:6379. Connection refused. Django Heroku
提问by John Waller
I am able to run redis locally and everything works.
我能够在本地运行 redis,一切正常。
However when I deploy to heroku I get this error:
但是,当我部署到 heroku 时,出现此错误:
Error 111 connecting to localhost:6379. Connection refused.
I have set up a Procfile with...
我已经设置了一个 Procfile ......
web: gunicorn odb.wsgi --log-file -
worker: python worker.py
I have a worker.py file...
我有一个 worker.py 文件...
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.
REDISTOGO_URL 变量出现在 heroku 配置中。
Redis to go is an installed add-on for my app.
Redis to go 是我的应用程序的已安装附加组件。
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?
REDISTOGO_URL 是否必须在 settings.py 中定义?为什么在 worker.py 中甚至没有定义的情况下,heroku 会尝试连接到本地主机?
采纳答案by John Waller
Turns out I needed to set up things like this for it to work on Heroku.
原来我需要设置这样的东西才能在 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)
回答by shrishinde
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.
可能与您的问题没有直接关系,但我遇到了同样的错误,结果我的系统上没有安装 redis-server 包。
Problem was resolved with,
问题解决了,
Ubuntu:sudo apt-get install redis-server
Ubuntu:sudo apt-get install redis-server
Cent OS:sudo yum install redis
分操作系统:sudo yum install redis
回答by Deft-pawN
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
解决办法是sudo apt-get install redis-server
。不要忘记启动您的服务sudo service redis-server start
,您可以使用该命令sudo service redis-server {start|stop|restart|force-reload|status}
作为参考
回答by Mr Singh
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", }, }
也许您的环境中没有安装 radis 服务器
sudo apt-get install redis-server
我需要在 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", }, }
回答by ruhanbidart
If you are using django_rq, a configuration like this will work for you:
如果您使用的是 django_rq,这样的配置将适用于您:
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!
它将在您的本地环境和 Heroku 上运行!
回答by Josh Laird
Error 111 is thrown when the application is unable to contact Redis. I had the same problem following the Heroku Django Channelstutorial. The settings.pyfile should read:
当应用程序无法联系 Redis 时,会抛出错误 111。在Heroku Django Channels教程之后,我遇到了同样的问题。在settings.py文件应改为:
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
.
REDISCLOUD_URL
而不是REDIS_URL
.
Ensure Redis is installed on the Heroku server.
确保 Redis 安装在 Heroku 服务器上。
回答by cinch
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.
我意识到问题是 ufw 拒绝连接。因此,我使用以下命令允许在此端口进行连接。
sudo ufw alloww 6379
sudo ufw alloww 6379
回答by WaveRider
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)
当任何正在调用/连接到 redis 的应用程序,它为指定连接而消耗的环境变量没有被正确设置时,可能会发生这种情况 -REDISCLOUD_URL
或者REDISTOGO_URL
等等。这很可能是在应用程序或 redis 重新启动后启动了 redis并循环其连接 IP 和/或访问。因此,在部署时,确保在下游应用程序之前启动 redis
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.
确保 redis 已启动并正在运行,应用程序上的简单重启可以解决问题,或者,正如其他答案所指出的那样,以适当的方式刷新应用程序以重新刷新和重新使用环境变量。
回答by David Schumann
Right now Heroku automatically sets the environment variable REDIS_URL to URL + port.
现在 Heroku 自动将环境变量 REDIS_URL 设置为 URL + 端口。
A convenient way to work with redis on heroku is to use a connection pool:
在 heroku 上使用 redis 的一种便捷方法是使用连接池:
settings.py
设置.py
import redis
REDIS_DEFAULT_CONNECTION_POOL = redis.ConnectionPool.from_url(os.getenv('REDIS_URL', 'redis://localhost:6379/'))
whererver.py
哪里的.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
回答by Maik Ro
if anyone comes here trying to get django_rq to work after encountering either 99 or 111 errors try the following:
如果有人在遇到 99 或 111 错误后试图让 django_rq 工作,请尝试以下操作:
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
这要求您在 docker-compose.yml 中像这样命名 redis 容器
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"