Connecting to MongoDB from remote client machine

Configuring a MongoDB server on your server is an easy task. When you are connecting to instance of MongoDB server from same local machine, networking related issues do not come into play. It is on local machine, there is no question of any port blocking or IP blocking etc. But as soon as you try to access your instance of MongoDB server from another machine, then you are bound to run into connection problems if you have not modified configuration file for your MongoDB server installation.

Let's look at following sample code that I wrote to check connectivity.

using Byte.LogIngestor.Console;
using MongoDB.Driver;

try
{
    var client = new MongoClient("mongodb://mydbuser:mycomplexPassword@myRemoteServer/?authSource=ByteMonitor");
    var database = client.GetDatabase("ByteMonitor");
    var byteLogCollection = database.GetCollection("ByteLog");
    await byteLogCollection.InsertOneAsync(new ByteLog
    {
        Category = "Test", Component = "Test", Context = "1", Environment = "Local", LogLevel = LogLevel.Information,
        Source = "Local", ShortMessage = "Test Mongo", ReportedAtTimeUtc = DateTime.UtcNow,
        ReportedFromLocation = "127.0.0.1", Details = "Mongo collection check!"
    });
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

As soon as I ran this code, I ran into following exception.

A timeout occurred after 30000ms selecting a server using CompositeServerSelector
{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 }, 
OperationsCountServerSelector }. Client view of cluster state is { ClusterId : "1", Type : "Unknown", State : "Disconnected", 
Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/myRemoteServer:27017" }", 
EndPoint: "Unspecified/myRemoteServer:27017", ReasonChanged: "Heartbeat", State: "Disconnected", 
ServerVersion: , TopologyVersion: , Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: 
An exception occurred while opening a connection to the server.
 ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (10060): 
A connection attempt failed because the connected party did not properly respond after a period of time, 
or established connection failed because connected host has failed to respond. 192.168.86.50:27017

From this exception you can see that remote MongoDB server did not allow the connection. There is a reason for it. When you install local instance of MongoDB server, in the mongod.cfg, there is an entry that configures what type of network access MongoDB server will allow. You will find that following entries by default.

net:
  port: 27017
  bindIp: 127.0.0.1

This entry translates to that MongoDB server will listen on Port 27017 from IP address 127.0.0.1. This is a security precaution at the time of the installation. The installation restricts the access to local machine only. You will have to first secure the installation of your MongoDB server before changing the network related settings.

After you have secured the access and ready to allow remote machines to allow to connect to your server, you can change the settings to as below.

net:
  port: 27017
  bindIpAll: true

Use of "bindIpApp" is a shortcut to allow or deny global access. If you want to restrict access to certain remove machines, then use bindIp setting to provide list of allowed hosts and IP addresses.

After you make the required configuration changes in your MongoDB installation, you shall be able to connect to your server without any problems.

Search

Social

Weather

17.5 °C / 63.5 °F

weather conditions Clouds

Monthly Posts

Blog Tags