在Rails应用程序上使用127.0.0.1:6379(ECONNREFUSED)连接到Redis时出错

时间:2018-09-17 19:03:04

标签: ruby-on-rails ruby redis

通过使用Redis,我尝试在Ruby on Rails上创建排名系统。

我的环境:Windows10,无业游民,Ruby -v 2.2.3,rails -v 4.2.5,mysql2 -v 0.3.18 ...

但是我遇到以下错误。

在127.0.0.1:6379(ECONNREFUSED)上连接到Redis的错误

this is error pic on rails application...

我想不是出在app / posts_controller.rb中的代码而是我的其他设置文件出了点​​问题。

这就是为什么当我在zadd hoge 1 1上使用zrange hoge 0 -1在tarminal上运行redis-cliredis-server等时,我的输出毫无问题。

与我的rails应用程序相关的以下文件:

gemfile.lock

首先,我运行捆绑安装gem 'redis'gem 'redis-rails'

GEM
  remote: https://rubygems.org/
  specs:
    actionmailer (4.2.5)
      actionpack (= 4.2.5)
      actionview (= 4.2.5)
      activejob (= 4.2.5)
      mail (~> 2.5, >= 2.5.4)
      rails-dom-testing (~> 1.0, >= 1.0.5)
    actionpack (4.2.5)
      actionview (= 4.2.5)
      activesupport (= 4.2.5)
      rack (~> 1.6)
      rack-test (~> 0.6.2)
      rails-dom-testing (~> 1.0, >= 1.0.5)
      rails-html-sanitizer (~> 1.0, >= 1.0.2)
    actionview (4.2.5)
      activesupport (= 4.2.5)
      builder (~> 3.1)
      erubis (~> 2.7.0)
      rails-dom-testing (~> 1.0, >= 1.0.5)
      rails-html-sanitizer (~> 1.0, >= 1.0.2)
    activejob (4.2.5)
      activesupport (= 4.2.5)
      globalid (>= 0.3.0)
    activemodel (4.2.5)
      activesupport (= 4.2.5)
      builder (~> 3.1)
    activerecord (4.2.5)
      activemodel (= 4.2.5)
      activesupport (= 4.2.5)
      arel (~> 6.0)
    activesupport (4.2.5)
      i18n (~> 0.7)
      json (~> 1.7, >= 1.7.7)
      minitest (~> 5.1)
      thread_safe (~> 0.3, >= 0.3.4)
      tzinfo (~> 1.1)
    arel (6.0.4)
    bcrypt (3.1.12)
    binding_of_caller (0.8.0)
      debug_inspector (>= 0.0.1)
    builder (3.2.3)
    byebug (10.0.2)
    coderay (1.1.2)
    coffee-rails (4.1.1)
      coffee-script (>= 2.2.0)
      railties (>= 4.0.0, < 5.1.x)
    coffee-script (2.4.1)
      coffee-script-source
      execjs
    coffee-script-source (1.12.2)
    concurrent-ruby (1.0.5)
    crass (1.0.4)
    debug_inspector (0.0.3)
    devise (4.5.0)
      bcrypt (~> 3.0)
      orm_adapter (~> 0.1)
      railties (>= 4.1.0, < 6.0)
      responders
      warden (~> 1.2.3)
    erubis (2.7.0)
    execjs (2.7.0)
    globalid (0.4.1)
      activesupport (>= 4.2.0)
    hike (1.2.3)
    i18n (0.9.5)
      concurrent-ruby (~> 1.0)
    jbuilder (2.7.0)
      activesupport (>= 4.2.0)
      multi_json (>= 1.2)
    jquery-rails (4.3.3)
      rails-dom-testing (>= 1, < 3)
      railties (>= 4.2.0)
      thor (>= 0.14, < 2.0)
    json (1.8.6)
    loofah (2.2.2)
      crass (~> 1.0.2)
      nokogiri (>= 1.5.9)
    mail (2.7.0)
      mini_mime (>= 0.1.1)
    method_source (0.9.0)
    mini_mime (1.0.1)
    mini_portile2 (2.3.0)
    minitest (5.11.3)
    multi_json (1.13.1)
    mysql2 (0.3.18)
    nokogiri (1.8.4)
      mini_portile2 (~> 2.3.0)
    orm_adapter (0.5.0)
    pry (0.11.3)
      coderay (~> 1.1.0)
      method_source (~> 0.9.0)
    pry-rails (0.3.6)
      pry (>= 0.10.4)
    rack (1.6.10)
    rack-test (0.6.3)
      rack (>= 1.0)
    rails (4.2.5)
      actionmailer (= 4.2.5)
      actionpack (= 4.2.5)
      actionview (= 4.2.5)
      activejob (= 4.2.5)
      activemodel (= 4.2.5)
      activerecord (= 4.2.5)
      activesupport (= 4.2.5)
      bundler (>= 1.3.0, < 2.0)
      railties (= 4.2.5)
      sprockets-rails
    rails-deprecated_sanitizer (1.0.3)
      activesupport (>= 4.2.0.alpha)
    rails-dom-testing (1.0.9)
      activesupport (>= 4.2.0, < 5.0)
      nokogiri (~> 1.6)
      rails-deprecated_sanitizer (>= 1.0.1)
    rails-html-sanitizer (1.0.4)
      loofah (~> 2.2, >= 2.2.2)
    railties (4.2.5)
      actionpack (= 4.2.5)
      activesupport (= 4.2.5)
      rake (>= 0.8.7)
      thor (>= 0.18.1, < 2.0)
    rake (12.3.1)
    rdoc (4.3.0)
    redis (3.2.0)
    redis-actionpack (5.0.2)
      actionpack (>= 4.0, < 6)
      redis-rack (>= 1, < 3)
      redis-store (>= 1.1.0, < 2)
    redis-activesupport (5.0.7)
      activesupport (>= 3, < 6)
      redis-store (>= 1.3, < 2)
    redis-rack (2.0.4)
      rack (>= 1.5, < 3)
      redis-store (>= 1.2, < 2)
    redis-rails (5.0.2)
      redis-actionpack (>= 5.0, < 6)
      redis-activesupport (>= 5.0, < 6)
      redis-store (>= 1.2, < 2)
    redis-store (1.5.0)
      redis (>= 2.2, < 5)
    responders (2.4.0)
      actionpack (>= 4.2.0, < 5.3)
      railties (>= 4.2.0, < 5.3)
    sass (3.2.19)
    sass-rails (4.0.5)
      railties (>= 4.0.0, < 5.0)
      sass (~> 3.2.2)
      sprockets (~> 2.8, < 3.0)
      sprockets-rails (~> 2.0)
    sdoc (0.4.2)
      json (~> 1.7, >= 1.7.7)
      rdoc (~> 4.0)
    spring (2.0.2)
      activesupport (>= 4.2)
    sprockets (2.11.0)
      hike (~> 1.2)
      multi_json (~> 1.0)
      rack (~> 1.0)
      tilt (~> 1.1, != 1.3.0)
    sprockets-rails (2.3.3)
      actionpack (>= 3.0)
      activesupport (>= 3.0)
      sprockets (>= 2.8, < 4.0)
    thor (0.20.0)
    thread_safe (0.3.6)
    tilt (1.4.1)
    turbolinks (5.2.0)
      turbolinks-source (~> 5.2)
    turbolinks-source (5.2.0)
    tzinfo (1.2.5)
      thread_safe (~> 0.1)
    uglifier (4.1.19)
      execjs (>= 0.3.0, < 3)
    warden (1.2.7)
      rack (>= 1.0)
    web-console (2.3.0)
      activemodel (>= 4.0)
      binding_of_caller (>= 0.7.2)
      railties (>= 4.0)
      sprockets-rails (>= 2.0, < 4.0)

PLATFORMS
  ruby

DEPENDENCIES
  byebug
  coffee-rails (~> 4.1.0)
  devise
  jbuilder (~> 2.0)
  jquery-rails
  mysql2 (= 0.3.18)
  pry-rails
  rails (= 4.2.5)
  redis (= 3.2.0)
  redis-rails
  sass-rails (~> 4.0.2)
  sdoc (~> 0.4.0)
  spring
  sprockets (= 2.11.0)
  turbolinks
  uglifier (>= 1.3.0)
  web-console (~> 2.0)

BUNDLED WITH
   1.10.6

配置/初始化/redis.rb

第二,我创建了这个文件。

require 'redis'

uri = URI.parse(ENV["REDIS"])
REDIS = Redis.new(host: uri.host, port: uri.port)

config / environments / development.rb

第三,我在最下面的行中添加了ENV["REDIS"] = "localhost:6379"

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = false

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  # Asset digests allow you to set far-future HTTP expiration dates on all assets,
  # yet still be able to expire them through the digest params.
  config.assets.digest = true

  # Adds additional error checking when serving assets at runtime.
  # Checks for improperly declared sprockets dependencies.
  # Raises helpful error messages.
  config.assets.raise_runtime_errors = true

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true

  ENV["REDIS"] = "localhost:6379"
end

app / controller / posts_controller.rb

最后,我在 def索引 def show 中描述了新代码。

ids = REDIS.zrevrange“ ranking”,0,-1 @ranking_posts = Post.where(id:ids)

REDIS.zincr通过“排名”,1,“#{@ post.id}”

顺便说一句,如果我没有描述上面的两个描述, 当然,我的rails应用程序可以工作。

class PostsController < ApplicationController

  def index
    @posts = Post.all.order(id: :desc)


    ids = REDIS.zrevrange "ranking", 0, -1
    #the order of posts ranking
    @ranking_posts = Post.where(id: ids)

  end


  def show
    @post = Post.find_by(id: params[:id])

    # update ranking
    REDIS.zincrby "ranking", 1, "#{@post.id}"
  end



  def new
  end

  def create
    Post.create(name: post_params[:name], text: post_params[:text], user_id: current_user.id)

    redirect_to("/")
  end

  def destroy
    post = Post.find(params[:id])
    if post.user_id == current_user.id
      post.destroy
    end
    redirect_to("/")

请告诉我如何解决此错误...(; ^;)

2 个答案:

答案 0 :(得分:1)

Rails不会启动Redis服务器。您在此处提供的配置仅设置redis客户端以与redis服务器通信。您必须安装并启动redis才能使其连接。

https://redislabs.com/ebook/appendix-a/a-3-installing-on-windows/a-3-2-installing-redis-on-window/

答案 1 :(得分:1)

根据文章中提到的描述,您可以在运行redis-server之后在终端上运行redis命令。

  

这就是为什么当我运行zadd hoge 1 1和zrange hoge 0 -1等时   通过在使用redis-server后使用redis-cli进行tarminal,我得到了结果   没问题。

但是在运行Rails应用程序时,似乎您的Redis服务器未运行,这就是为什么在连接到localhost:6379时说错误

尝试在与Rails服务器并行的新选项卡中运行下面提到的命令

redis-server 

如果服务器已经在运行,则使用下面提到的命令将其关闭,然后重新启动

redis-cli    #shutdown #to stop the server
redis-server #to start the server again