gpt4 book ai didi

ruby-on-rails - 在 Vagrant 机器上连接到 Redis

转载 作者:IT王子 更新时间:2023-10-29 06:11:20 25 4
gpt4 key购买 nike

我试图解决从 Rails 服务器连接到 Redis 服务器时出现的这个错误,两者都在 Vagrant VM 中运行:

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

我认为,我可以通过以下方式验证 Redis 服务器是否正在运行:

ps aux | grep redis
redis 839 0.1 0.1 35140 1840 ? Ssl 00:21 0:00 /usr/local/bin/redis-server *:6379
vagrant 1220 0.0 0.0 11676 952 pts/0 R+ 00:22 0:00 grep --color=auto redis

sudo netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 839/redis-server *:
tcp 0 0 0.0.0.0:57102 0.0.0.0:* LISTEN 778/rpc.statd
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 507/rpcbind
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 725/sshd
tcp6 0 0 :::6379 :::* LISTEN 839/redis-server *:
tcp6 0 0 :::111 :::* LISTEN 507/rpcbind
tcp6 0 0 :::22 :::* LISTEN 725/sshd
tcp6 0 0 :::49667 :::* LISTEN 778/rpc.statd

我的 redis.conf 中有这个:

# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379

# If you want you can bind a single interface, if the bind option is not
# specified all the interfaces will listen for incoming connections.
#
bind 127.0.0.1

和 Vagrantfile:

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "ubuntu-12.04-amd64"

# THE URL FROM WHERE THE 'CONFIG.VM.BOX' BOX WILL BE FETCHED IF IT
# DOESN'T ALREADY EXIST ON THE USER'S SYSTEM.
config.vm.box_url = "http://files.vagrantup.com/precise64.box"

config.vm.network "forwarded_port", guest: 6379, host: 6379, auto_correct: true

config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--memory", "1024"]
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
end

config.vm.provision "shell", path: "init.sh"
end

我尝试关注 canonical entry但我没有取得任何进展。这可能是我忽略的显而易见的事情。有人可以为我指出正确的方向吗?我还可以尝试什么?

编辑:完整错误信息:

$redis = Redis.new(:host => '127.0.0.1', :port => 6379)
=> #<Redis client v3.2.2 for redis://127.0.0.1:6379/0>
$redis.set('a', 'b')
Redis::CannotConnectError: Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)

编辑:注释掉绑定(bind) 127.0.0.1 并能够从 redis 框 ping 但 Rails 仍然没有运气:

redis-cli -h 127.0.0.1 ping
PONG

最佳答案

我明白了,我需要使用我的 VM (VirtualBox) 适配器的 IP 地址才能让它在我的 Windows 机器上运行。这有效:

$redis = Redis.new(:host => '192.168.56.1', :port => 6379)
=> #<Redis client v3.2.2 for redis://192.168.56.1:6379/0>
$redis.set('a', 'b')
=> "OK"
$redis.get('a')
=> "b"

关于ruby-on-rails - 在 Vagrant 机器上连接到 Redis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36611889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com