Mongo DB: Failed to connect on Laravel Homestead

Solution:

I managed to fix my problem after hours of searching so I will post the fix. Because I didn’t find anything that could help me I started to check the Homestead scripts in order to understand how Mongo is installed and in homestead.rb I found this line:

# Install MongoDB If Necessary
if settings.has_key?('mongodb') && settings['mongodb']
  config.vm.provision 'shell' do |s|
    s.name = 'Installing MongoDb'
    s.path = script_dir + '/install-mongo.sh'
  end
end

So I searched were “install-mongo.sh” is called and I found this condition:

if [ -f /home/vagrant/.mongo ]
then
    echo "MongoDB already installed."
    exit 0
fi

So Mongo DB is not installed every time only if the “/home/vagrant/.mongo” file doesn’t exist. At this point I realized that maybe Mongo failed to be installed but this file was written.

So the solution was to destroy the Vagrant box and recreate it from scratch:

vagrant destroy
vagrant up --provision