Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 连接到流浪机器上的Redis_Ruby On Rails_Redis_Vagrant - Fatal编程技术网

Ruby on rails 连接到流浪机器上的Redis

Ruby on rails 连接到流浪机器上的Redis,ruby-on-rails,redis,vagrant,Ruby On Rails,Redis,Vagrant,我试图解决从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

我试图解决从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_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
我试着按照计划做,但没有取得任何进展。这可能是我忽略的东西。有人能给我指出正确的方向,我还能尝试什么吗

编辑:完整错误消息:

$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)

我明白了,我需要使用我的虚拟机适配器(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"
$redis=redis.new(:host=>'192.168.56.1',:port=>6379)
=> #
$redis.set('a','b')
=>“好的”
$redis.get('a')
=>“b”

您好,您是如何为您的虚拟机提供ip地址的?@MareksNo我相信192.168.56.1是VirtualBox的默认地址。我认为有办法改变这一点,但我不是一个网络专家。您可以在ServerFault上搜索该IP地址以获得进一步的信息。嗯,好吧,我的服务器没有运行,因为在layers config中,我的蠢蛋决定将vagrants 10.0.0.2作为主机IP
$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"