Having a bit of trouble connecting to a remote MongoDb instance from a .NET app.
I've created a very simple console application which is basically a smoke test and brings back a single item. If I run it on the server on which the MongoDb service is running, it talks to MongoDb with no problem. And the ConnectionString is simply mongodb://localhost:27017
.
However, I want to run it in my dev environment on my dev machine. I have tried several different connection strings (listed at the end of this post), and I keep getting a timeout error:
A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "Automatic", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "192.168.1.51:27017" }", EndPoint: "192.168.1.51:27017", State: "Disconnected", Type: "Unknown" }] }.
I'm using v2.5.1 of mongocsharpdriver
I have created an inboudd rule for the 27017 port on the server and even turned the firewall off completely.
The cfg file for my Mongo server is very basic:
systemLog:
destination: file
path: E:\MongData\mongod.log
storage:
dbPath: E:\MongData\db
directoryPerDB: false
net:
port: 27017
security:
authorization: 'enabled'
I don't have any replications. Just the single instance.
As you can tell, I'm a Mongo noob. What's going wrong?
Cheers
Connection Strings:
<add name="MConnectionString" connectionString="mongodb://daveAdmin:firetruck1@192.168.1.51:27017" />
<add name="MRealEstateConnectionString" connectionString="mongodb://daveAdmin:firetruck1@BIGDOG:27017" />
<add name="MRealEstateConnectionString" connectionString="mongodb://BIGDOG:27017" />
<add name="RealEstateConnectionString" connectionString="mongodb://192.168.1.51:27017" />
I also attempted to connect using the CLI after downloading the Mongo binaries (zip file) with no luck:
W:\mongodb\bin>mongo --host BIGDOG:27017 MongoDB shell version v3.7.5-85-gb48579fcba connecting to: mongodb://BIGDOG:27017/ 2018-04-22T11:05:36.098+0930 E QUERY [js] Error: couldn't connect to server BIGDOG:27017, connection attempt failed: NetworkTimeout: Socket operation timed out : connect@src/mongo/shell/mongo.js:251:13 @(connect):1:6 exception: connect failed
W:\mongodb\bin>mongo --host 192.168.1.51:27017 MongoDB shell version v3.7.5-85-gb48579fcba connecting to: mongodb://192.168.1.51:27017/ 2018-04-22T11:05:56.508+0930 E QUERY [js] Error: couldn't connect to server 192.168.1.51:27017, connection attempt failed: NetworkTimeout: Socket operation timed out : connect@src/mongo/shell/mongo.js:251:13 @(connect):1:6 exception: connect failed
W:\mongodb\bin>mongo --username daveAdmin --password firetruck1 --host 192.168.1.51:27017 MongoDB shell version v3.7.5-85-gb48579fcba connecting to: mongodb://192.168.1.51:27017/ 2018-04-22T11:07:49.671+0930 E QUERY [js] Error: couldn't connect to server 192.168.1.51:27017, connection attempt failed: NetworkTimeout: Socket operation timed out : connect@src/mongo/shell/mongo.js:251:13 @(connect):1:6 exception: connect failed
W:\mongodb\bin>mongo --username daveAdmin --password firetruck1 --host BIGDOG:27017 MongoDB shell version v3.7.5-85-gb48579fcba connecting to: mongodb://BIGDOG:27017/ 2018-04-22T11:08:54.305+0930 E QUERY [js] Error: couldn't connect to server BIGDOG:27017, connection attempt failed: NetworkTimeout: Socket operation timed out : connect@src/mongo/shell/mongo.js:251:13 @(connect):1:6 exception: connect failed
I can ping the server and access its network shares from my dev desktop.
How do I connect remotely? This is just a simple local network - a couple of computers linked via a switch.
How do I connect remotely? This is just a simple local network
Starting in MongoDB 3.6, MongoDB binaries, mongod
and mongos
, bind to localhost
by default.
When bound only to the localhost, these MongoDB 3.6 binaries can only accept connections from clients that are running on the same machine. Remote clients cannot connect to the binaries bound only to localhost.
Based on the configuration file you provided, it seems that you're still binding only to the localhost interface (default). This explains why you're able to connect when your application/mongo shell was executed from the same MongoDB server host.
In your example, you can try run mongod with the following :
mongod --bind_ip localhost,192.168.1.51
Also see net.bindIp for the configuration parameter format option.
You may also be interested in : MongoDB Security Hardening