SOLVED: Could not connect to MongoDB on the provided host and port
 


While trying to connect to a newly installed MongoDB server from Mongo DB complass client application, you may see this error. Here is a solution for this issue.

There may be a few reasons that could have caused this issue.

  1. The mongodb service is not running
  2. Some network issue
  3. Some firewall issue

Let us see each of these issue and resolve them one by one.

The mongodb service is not running

First of all make sure that the mongodb service is running. If you are using windows, goto services and then scroll down and check that the mongodb service is running. If you are on a linux machine, type 'service mongod status' and check that the service is active and running.

Some network issue

If you are sure that the mongodb service is running then make sure there is no network issue. Usually the default mongodb service listens on port 27017 and IP: 127.0.0.1. This works great as long as you are connecting from localhost and there are no funny network settings. In some cases you may have to open the config file and make mongodb listen to both 127.0.0.1 as well as your private IP of your machine. If you are using Windows, you can check your private IP using 'ipconfig' command and in linux using 'ifconfig' command. Once you know the IP of your machine, edit your config file and add this ip as follows:
In windows it is usually this path. C:\Program Files\MongoDB\Server\4.0\bin\mongod.cfg and in linux it is /etc/mongod.conf. Edit this line bind_ip: 127.0.0.1 as below bind_ip: 127.0.0.1, xx.xx.xx.xx Where xx.xx.xx.xx is your private IP.
Save the file and restart the service.

Some firewall issue

Even after you do steps 1 and 2 above, if you still cannot connect then probably the port 27017 is not open or blocked by firewall. In windows, you may edit the windows firewall and open this port. If you are using AWS or some other cloud hosting, then you may need to configure the 'security group' or equivalent firewall setting and open this port for external access.

After these 3 steps, you should be able to access mongodb from anywhere

 
 
 
 
 
COPYRIGHT © 2017 java-samples.com. ALL RIGHTS RESERVED.