在Vagrant机器上连接到Redis

JJ32

从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

我尝试遵循规范的条目,但没有取得任何进展。我可能忽略了一些明显的事情。有人可以为我指出其他可以尝试的正确方向吗?

编辑:完整的错误消息:

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

编辑:注释掉绑定127.0.0.1,并能够从Redis盒中ping起来,但是仍然无法从Rails运气:

redis-cli -h 127.0.0.1 ping
PONG
JJ32

弄清楚了,我需要为我的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"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从Vagrant机器连接到Redis Docker容器

无法连接到AWS上的Redis节点

无法连接到Heroku上的Redis

vagrant ssh 无法连接到 Windows 10 上的 vm

如何通过ssh将ansible连接到两台Vagrant机器(Virtualbox Hypervisor)中?

无法使用HeidiSQL连接到Vagrant:“无法连接到'localhost'上的MySQL服务器”

自动连接到远程机器

无法连接到在VirtualBox上运行的Redis服务器

Flask-Session Redis 连接到 AWS 上的错误主机?

SignalR无法连接到SSL上的Azure Redis

在 Heroku 上连接到 Redis 的 Rails 5 错误

Vagrant 多机-机器之间的连接

从本地机器上的 Mongo Compass 连接到 Mongo Docker 容器?

将本地机器上的 SSIS 连接到远程服务器 oracle 数据库

如何从本地机器连接到专用网络上的数据库?

无法在 localhost 8091 的 Windows 机器上连接到 Couchbase 服务器

如何将VPN上的机器连接到AWS PHZ DNS?

我可以从Eclipse / IDE调试器连接到远程机器上的jsadebugd进程吗?

从本地机器将 Python 连接到 AWS EC2 上的 kafka

只能从同一台机器上的Spark连接到Mesos

从互联网上的任何地方连接到部署在本地机器上的网站

Telnet 连接到远程机器并在其上查找 Firefox 版本

从Linux机器上的nodejs使用Active Directory连接到Sql Server

配置hadoop-client连接到其他机器/服务器上的hadoop

远程机器节点无法使用Ruby连接到Selenium Webdriver上的集线器

如何将本地机器上运行的 Postgres 数据库连接到 docker 容器

使用 IP 连接到在本地机器上运行的服务器失败

Windows 机器上的 Apache NiFi 无法连接到同一主机上的 MySQL 数据库

从 python3 - pyodbc (linux) 连接到远程机器上的 firebird 2.5 (windows) ODBC DSN