ruby-on-rails 连接到Redis 127.0.0.1:6379(错误号::ECONNREFUSED)时发生错误

ac1kyiln  于 2022-11-19  发布在  Ruby
关注(0)|答案(8)|浏览(221)

我尝试运行sidekiq worker和Rails。当我尝试docker-compose up worker时,我得到以下错误:

worker_1 | Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:332:in `rescue in establish_connection'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:318:in `establish_connection'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:94:in `block in connect'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:280:in `with_reconnect'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:93:in `connect'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:351:in `ensure_connected'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:208:in `block in process'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:293:in `logging'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:207:in `process'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:113:in `call'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis.rb:211:in `block in info'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis.rb:57:in `block in synchronize'
worker_1 | /usr/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis.rb:57:in `synchronize'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis.rb:210:in `info'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/cli.rb:71:in `block in run'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq.rb:84:in `block in redis'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/connection_pool-2.2.0/lib/connection_pool.rb:64:in `block (2 levels) in with'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/connection_pool-2.2.0/lib/connection_pool.rb:63:in `handle_interrupt'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/connection_pool-2.2.0/lib/connection_pool.rb:63:in `block in with'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/connection_pool-2.2.0/lib/connection_pool.rb:60:in `handle_interrupt'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/connection_pool-2.2.0/lib/connection_pool.rb:60:in `with'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq.rb:81:in `redis'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/cli.rb:68:in `run'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/bin/sidekiq:13:in `<top (required)>'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/bin/sidekiq:23:in `load'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/bin/sidekiq:23:in `<main>'
nyvur_worker_1 exited with code 1

下面是我的docker-compose文件:

web: &app_base
  build: .
  ports:
    - "80:80"
  volumes:
    - .:/Nyvur
  command: /usr/bin/start_server.sh
  links:
    - postgres
    - mongo
    - redis
  environment: &app_environment
    SIDEKIQ_CONCURRENCY: 50
    SIDEKIQ_TIMEOUT: 10
    ENABLE_DEBUG_SERVER: true
    RACK_ENV: production
    RAILS_ENV: production

worker:
  build: .
  volumes:
    - .:/Nyvur
  ports: []
  links:
    - postgres
    - mongo
    - redis
  command: bundle exec sidekiq -c 50

postgres:
  image: postgres:9.1
  ports:
    - "5432:5432"
  environment:
    LC_ALL: C.UTF-8
    POSTGRES_DB: Nyvur_production
    POSTGRES_USER: postgres
    POSTGRES_PASSWORD: 3x1mpl3

mongo:
  image: mongo:3.0.7
  ports:
    - "27017:27017"

redis:
  image: redis
  ports:
    - "6379:6379"

我的Dockerfile

FROM phusion/passenger-customizable
MAINTAINER VodkaMD <support@nyvur.com>
ENV RACK_ENV="production" RAILS_ENV="production"
SECRET_KEY_BASE="e09afa8b753cb175bcef7eb5f737accd02a4c16d9b6e5d475943605abd4277cdf47c488812d21d9c7117efd489d876f34be52f7ef7e88b21759a079339b198ce"
ENV HOME /root
CMD ["/sbin/my_init"]

RUN /pd_build/utilities.sh
RUN /pd_build/ruby2.2.sh
RUN /pd_build/python.sh
RUN /pd_build/nodejs.sh
RUN /pd_build/redis.sh
RUN /pd_build/memcached.sh
RUN apt-get update && apt-get install -y vim nano dialog net-tools build-essential wget libpq-dev git
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# RUN mkdir /etc/nginx/ssl
# RUN openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt
RUN rm -f /etc/service/nginx/down
RUN rm -f /etc/service/redis/down
RUN rm -f /etc/service/sshd/down
RUN rm -f /etc/service/memcached/down
WORKDIR /tmp
ADD Gemfile /tmp/
ADD Gemfile.lock /tmp/
RUN mkdir /home/app/Nyvur
ADD . /home/app/Nyvur
RUN chown -R app:app /home/app/Nyvur
WORKDIR /home/app/Nyvur
RUN bundle install --deployment
RUN bundle exec rake assets:precompile
RUN rm /etc/nginx/sites-enabled/default
COPY config/nginx/nginx_nyvur.conf /etc/nginx/sites-enabled/nginx_nyvur.conf
ADD config/nginx/postgres-env.conf /etc/nginx/main.d/postgres-env.conf
ADD config/nginx/rails-env.conf /etc/nginx/main.d/rails-env.conf
ADD config/nginx/start_server.sh /usr/bin/start_server.sh
RUN chmod +x /usr/bin/start_server.sh
RUN mkdir -p /home/app/Nyvur/tmp/pids
RUN mkdir -p /home/app/Nyvur/tmp/sockets
RUN mkdir -p /home/app/Nyvur/log
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
EXPOSE 80 443 9292

我已经尝试了不同的配置,我已经检查了其他版本,但问题仍然存在,到目前为止,Sidekiq运行良好以外的Docker。

58wvjzkj

58wvjzkj1#

检查您的redis服务器是否正在运行,在终端中使用以下命令启动redis:

redis-server
bksxznpy

bksxznpy2#

Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)

您的应用尝试连接到运行它的容器的localhost接口,但是redis正在不同的容器中运行。
修改你的应用配置,使用redis容器的链接名(在你的例子中是redis)作为连接的主机名。

2wnc66cl

2wnc66cl3#

在macOS上(使用Homebrew),我可以通过运行以下命令修复此问题:

brew services start redis

如果您还没有安装redis,您需要先运行以下命令:

brew install redis
ymzxtsji

ymzxtsji4#

我已经使用以下代码段解决了此错误

sudo apt install redis-server

然后通过运行redis服务器使用-

redis-server
bpzcxfmw

bpzcxfmw5#

Redis快速回顾

有一个类似的问题,在我的情况下,我有一个脚本,启动redis和其他初始设置,我忘了运行它。它通常与CaS(Tomcat)中断,但这一次它打破了由于redis,我几乎忘记了这是由脚本设置。
无论如何,为了补充其他答案,检查是否存在问题的快速方法是运行

redis-cli

它会让你知道redis是否在工作,从而帮助你记住是否需要运行它。
如果Redi没有运行,你会得到一个redis控制台:

redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>

如果它正在运行,您将得到一个redis控制台(在交互模式下),请参见documentation

redis-cli
127.0.0.1:6379>

例如你可以说:PING,它将返回PONG

redis-cli
127.0.0.1:6379> PING
PONG

要运行Redis服务器,请执行以下操作:

redis-server
vaqhlq81

vaqhlq816#

使用***redis-server***命令启动您的redis服务器。

cs7cruho

cs7cruho7#

如果您正在使用heroku并遇到此类问题,则需要在环境中指定REDIS_PROVIDER_URL

u5rb5r59

u5rb5r598#

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

sudo apt install redis-server

相关问题