2

I am having an issue with mongo provisioning.

For some reason when I do provision the first time, it will give me an error:

==> 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 :

but if I do vagrant ssh

and run:

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

It works perfectly.

this is the relevant part of my provision:

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

It feels that while in provision, he is associating 127.0.0.1 as not reachable.

Thank you in advance. :)

Rip3rs
  • 1,284
  • 12
  • 21
  • how is your provisioning configured, can you add your Vagrantfile – Frederic Henri Feb 08 '17 at 15:55
  • Hi mate, added Vagrantfile to the question. also, I removed 2 lines on the provision that I only noticed were not meant to be there. – Rip3rs Feb 08 '17 at 16:02
  • 2
    maybe mongo needs some time to start and the execution right after the start of the command does not work - add a wait of 5/10 sec just to make sure mongo is up and running – Frederic Henri Feb 08 '17 at 16:37
  • I did `sudo service mongod status && netstat -tnl` to check what was going on and it is true, there in no ip or port available. – Rip3rs Feb 08 '17 at 17:56

1 Answers1

3

@Frédéric Henri thank you. You were correct! it takes sometime for mongodb to fully initialise.

I used the sleep 5s to wait until it would continue.

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

so my code looks like this now:

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()"

And works flawlessly... 2 days around this >.<

Community
  • 1
  • 1
Rip3rs
  • 1,284
  • 12
  • 21
  • Had exactly the same issue and cost me a couple of hours. Anyone knows how to exactly know when the mongod server is ready? I would like to implement it if its not complicated. – muratgozel Sep 03 '18 at 18:27