I want to use Redis on Heroku but I got this error Redis::CannotConnectError (Error connecting to Redis on 127.0.0.1:6379 (ECONNREFUSED))
.
I checked these , but didn't get helped. Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED, deploying redis to heroku unable to connect, How to get Redis to start on Heroku? .
I use Ruby 2.0.0p247 and Rails4. I'm using puma.
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" is the same URL that I can see when $ heroku config
.
I did Redis restart on Heroku GUI, but it doesn't work.
Please tell me anything I need to do. Thank you for your help.
Check which ENV variable should contain redis configuration
For example if you are going to use Redis for Sidekiq you need to map ENV["REDISTOGO_URL"] to ENV["REDIS_PROVIDER"] because Sidekiq is expecting to get redis configuration from ENV["REDIS_PROVIDER"]
You can do it this way
heroku config:set REDIS_PROVIDER=REDISTOGO_URL
Also you don't need to URI.parse, Redis will do it instead of you in case it is needed.
$redis = Redis.new(url: ENV['REDISTOGO_URL' || "redis://localhost:6379/"])
I suggest to use the gem figaro to set ENV variables for your local development. Thus you remove OR statement
$redis = Redis.new(url: ENV['REDISTOGO_URL'])
and set alternative value in your config/application.yml
REDISTOGO_URL: redis://127.0.0.1:6379
You're trying to connect to your own computer (note 127.0.0.1 == localhost). I'm guessing that's not the Redis server you're looking for :)
Looks like ENV["REDISTOGO_URL"]
isn't properly set.
Also, just a side note, look up Redis To Go if you haven't already, which is what most people use as a Redis server in conjunction with Heroku.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With