Want to use Redis on Heroku (Redis :: CannotConnectError (Error connecting to Redis at 127.0.0.1:6379 (ECONNREFUSED)))

I want to use Redis on Heroku, but I got this Redis::CannotConnectError (Error connecting to Redis on 127.0.0.1:6379 (ECONNREFUSED)) .

I checked them, but they didn’t help me. Redis connection with 127.0.0.1:6379 failed - connect ECONNREFUSED , deploy redis for heroku unable to connect , How to get Redis to run on Heroku? .

I am using Ruby 2.0.0p247 and Rails4. I use cougar.

I use RedisToGo (nano), and in /config/initializers/redis.rb I write this.

 uri = URI.parse(ENV["REDISTOGO_URL"] || "redis://localhost:6379/" ) $redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password) 

I checked "REDISTOGO_URL" - this is the same URL that I can see when $ heroku config .

I restarted Redis in Heroku GUI but it does not work.

Please tell me what I need. Thank you for your help.

+6
source share
2 answers

You are trying to connect to your computer (note 127.0.0.1 == localhost). I assume that is not the Redis server you are looking for :)

It appears that ENV["REDISTOGO_URL"] set incorrectly.


Also, just pay attention to Redis To Go , if you haven’t already done so, which most people use as a Redis server in combination with Heroku.

+4
source

Check which ENV variable should contain redis configuration

For example, if you intend to use Redis for Sidekiq, you need to map ENV ["REDISTOGO_URL"] to ENV ["REDIS_PROVIDER"] because Sidekiq expects to receive a redis configuration from ENV ["REDIS_PROVIDER"]

You can do it this way

 heroku config:set REDIS_PROVIDER=REDISTOGO_URL 

Also you do not need URI.parse , Redis will do it instead of you if necessary.

 $redis = Redis.new(url: ENV['REDISTOGO_URL' || "redis://localhost:6379/"]) 

I suggest using gem figaro to set ENV variables for your local development. This way you delete the OR statement

 $redis = Redis.new(url: ENV['REDISTOGO_URL']) 

and set an alternate value in config / application.yml

 REDISTOGO_URL: redis://127.0.0.1:6379 
+8
source

Source: https://habr.com/ru/post/955437/


All Articles