Vagrant MongoDB配置连接被拒绝

时间:2017-02-08 15:45:12

标签: mongodb ubuntu vagrant vagrant-provision

我遇到mongo配置问题。

出于某种原因,当我第一次进行配置时,它会给我一个错误:

==> METEOR_Dev_Box_test: ADDING MONGO INITIATE
==> METEOR_Dev_Box_test: MongoDB shell version v3.4.2
==> METEOR_Dev_Box_test: connecting to: 127.0.0.1:27017
==> METEOR_Dev_Box_test: W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
==> METEOR_Dev_Box_test: E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :

但如果我vagrant ssh

并运行:

mongo 127.0.0.1:27017 --eval "db=db.getSiblingDB('local');rs.initiate()"

效果很好。

这是我的规定的相关部分:

sudo service mongod stop
sudo sed -i '/#replication:/a replication:\n \ \ replSetName: rs0' /etc/mongod.conf
sudo sed -i '/#replication:/d' /etc/mongod.conf

sudo service mongod restart

mongo 127.0.0.1:27017 --eval "db=db.getSiblingDB('local');rs.initiate()"

Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

require 'yaml'
settings = YAML.load_file('./vagrant_setup.yml')

VAGRANTFILE_API_VERSION = '2'

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    settings.each do |settings|
        config.vm.define settings['name'] do |machine|
            machine.vm.box = settings['box']
            machine.vm.network "private_network", ip: settings['ip']
            machine.vm.hostname = settings['hostname']
            machine.vm.provision :shell, path: './vagrant_bootstrap.sh'
            machine.vm.provision :shell, path: './vagrant_meteor.sh', privileged: false
            machine.vm.network 'forwarded_port', guest: settings['port_1'], host: settings['port_1']
            machine.vm.network 'forwarded_port', guest: settings['port_2'], host: settings['port_2']
            machine.vm.network 'forwarded_port', guest: settings['port_3'], host: settings['port_3']
            machine.vm.provider :virtualbox do |vb|
                vb.name = settings['name']
                vb.memory = settings['memory']
                vb.cpus = settings['cpus']
                vb.customize ["modifyvm", :id, "--usb", "on"]
                vb.customize ["usbfilter", "add", "0", "--target", :id, "--name", "android", "--vendorid", "0x18d1"]
                vb.customize ["usbfilter", "add", "0", "--target", :id, "--name", "androidSamsung", "--vendorid", "0x04e8"]
                vb.customize ["usbfilter", "add", "0", "--target", :id, "--name", "androidLG", "--vendorid", "0x1004"]
            end
        end
    end
end

感觉在提供时,他将127.0.0.1关联为无法访问。

提前谢谢你。 :)

1 个答案:

答案 0 :(得分:3)

@FrédéricHenri谢谢你。你是对的! mongodb需要一段时间才能完全初始化。

我使用sleep 5s等待它继续。

How do I pause my shell script for 1 second before continuing?

所以我的代码现在看起来像这样:

sudo service mongod stop
sudo sed -i '/#replication:/a replication:\n \ \ replSetName: rs0' /etc/mongod.conf
sudo sed -i '/#replication:/d' /etc/mongod.conf

sudo service mongod start

sleep 5s

mongo 127.0.0.1:27017 --eval "db=db.getSiblingDB('local');rs.initiate()"

完美无瑕地工作......围绕此> 2天。<