繁体   English   中英

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

[英]Error connecting to Redis on 127.0.0.1:6379(ECONNREFUSED) on rails application

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

我的环境:windows10、vagrant、Ruby -v 2.2.3、rails -v 4.2.5、mysql2 -v 0.3.18...

但是我收到以下错误。

在 127.0.0.1:6379 上连接到 Redis 时出错(ECONNREFUSED)

这是rails应用程序上的错误图片...

我猜不是 app/posts_controller.rb 中的代码而是我的其他设置文件中的代码有问题。

这就是为什么当我在 ruing redis-server之后使用redis-cli在 tarminal 上运行zadd hoge 1 1zrange hoge 0 -1等时,我的输出没有任何问题。

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

gemfile.lock

首先我运行 bundle install 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)

配置/环境/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

应用程序/控制器/posts_controller.rb

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

ids = REDIS.zrevrange "排名", 0, -1 @ranking_posts = Post.where(id: ids)

REDIS.zincrby "排名", 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("/")

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

Rails 不会启动 redis 服务器。 您在那里提供的配置只是设置 redis 客户端与 redis 服务器通信。 您必须安装并启动 redis 以便它可以连接到某些东西。

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

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

这就是为什么当我在 ruing redis-server 后使用 redis-cli 在 tarminal 上运行 zadd hoge 1 1 和 zrange hoge 0 -1 等时,我的输出没有任何问题。

但是在运行 rails 应用程序时,您的 redis-server 似乎没有运行,这就是为什么它说连接到 localhost:6379 时出错

尝试在新选项卡中与 rails server 并行运行下面提到的命令

redis-server 

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

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

确保您在系统上安装了 redis-server:-

 sudo apt install redis-server

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM