0

I am unable to connect to my MongoDB instance from my Java client. The MongoDb instance is installed on a Ubuntu VM session inside VirtualBox. Mongo works fine when I run commands from the Mongo shell within Ubuntu - but I cant connect to it from my Windows 7 Intellij session.

My Java client code is as follows,

package com.mycom.mongodb.demo;

import com.mongodb.MongoClient;

public class App
{
    public static void main( String[] args )
    {

        MongoClient mongoClient = new MongoClient("192.168.171.68", 27017);

        mongoClient.getDatabaseNames().forEach(System.out::println);

    }
}

But I get the error message shown below.

enter image description here

I am able to Ping the IP address of my Ubuntu box but I am not able to telnet to the IP:Port that MongoDb is running on.

enter image description here

this is my mongo.cfg file

enter image description here

MongoDb is running on LocalHost on the Ubuntu machine - so maybe this is an area that I need to configure ? Any help would be appreciated.

This is my command-line to start mongod

sudo /opt/mongodb/mongodb-linux-x86_64-ubuntu1604-3.6.3/bin/mongod --config /var/opt/mongodb2/mongo.cfg

UPDATE: I am able to get it working if I include the "--bind_ip_all" parameter i.e.

sudo /opt/mongodb/mongodb-linux-x86_64-ubuntu1604-3.6.3/bin/mongod --bind_ip_all --config /var/opt/mongodb2/mongo.cfg

which I guess is ok for a small demo program but not as a permanent solution

robbie70
  • 1,515
  • 4
  • 21
  • 30
  • What's your question? MongoDB [listens on 127.0.0.1 by default](https://docs.mongodb.com/manual/administration/security-checklist/#limit-network-exposure), if you want to allow external connections you'll have to configure that. – CodeCaster Mar 16 '18 at 09:55
  • thanks. Thats my question. How do I configure MongoDB to listen to an external connection ? If I do "ipconfig" in my CMD session in Windows, will that give me an IP address that I then need to use to configure in MongoDB and if yes - then how do I configure it ? – robbie70 Mar 16 '18 at 09:58
  • 1
    https://stackoverflow.com/questions/30884021/mongodb-bind-ip-wont-work-unless-set-to-0-0-0-0 – CodeCaster Mar 16 '18 at 09:59

1 Answers1

0

I found the answer using the link provided by @CodeCaster - thank you.

I added the "bindIp" entry to the "mongo.cfg" file as shown below. This is the ip address from my Ubuntu VM which I found by using the "ifconfig" from a Terminal window.

robbie70@robbie70-vm-ubuntu64:~$ more /var/opt/mongodb2/mongo.cfg
net:
   port: 27017
   bindIp: 192.168.171.68
systemLog:
   destination: file
   path: "/var/opt/mongodb2/logs/mongodb2.log"
   logAppend: true
   quiet: false
storage:
   dbPath: "/var/opt/mongodb2/data"

now when I start MongoDb I dont specify "--bind_ip_all" in the command line,

sudo /opt/mongodb/mongodb-linux-x86_64-ubuntu1604-3.6.3/bin/mongod --config /var/opt/mongodb2/mongo.cfg

and the test demo Java Client program runs ok now, enter image description here

robbie70
  • 1,515
  • 4
  • 21
  • 30