0

Probably a duplicate here but was not able to find the correct answer hence asking the question.

So, I have a mongodb setup on my local machine say M1where the mongo service is running and db has some data.

Now, I want to connect to this mongodb using a mongo client on some other machine say M2 but not able to.

Following all the answers, I tried adding the M2 address in mongodb.conf file but no luck.

net:
   bindIp: [m2.some.valid.ip,127.0.0.1]

I even tried giving 0.0.0.0 but still no luck. With localhost, am able to connect from my own machine but if I mention M1 address and try locally with that address I get

Cannot connect to Mongo DB at M1 address, network is unreachable.

AM badly stuck for long here. Have just started with mongodb so pardon for any newbie question.

All my mongo services are up and running and am able to access the data using terminal or any mongo client at localhost.

This is the log when i run command mongod on terminal:

2018-09-11T11:14:13.977+0530 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2018-09-11T11:14:13.993+0530 I CONTROL  [initandlisten] MongoDB starting : pid=11307 port=27017 dbpath=/data/db 64-bit host=DELC02S5E2CG8WN.sea.corp.expecn.com
2018-09-11T11:14:13.993+0530 I CONTROL  [initandlisten] db version v4.0.2
2018-09-11T11:14:13.993+0530 I CONTROL  [initandlisten] git version: fc1573ba18aee42f97a3bb13b67af7d837826b47
2018-09-11T11:14:13.993+0530 I CONTROL  [initandlisten] allocator: system
2018-09-11T11:14:13.993+0530 I CONTROL  [initandlisten] modules: none
2018-09-11T11:14:13.993+0530 I CONTROL  [initandlisten] build environment:
2018-09-11T11:14:13.993+0530 I CONTROL  [initandlisten]     distarch: x86_64
2018-09-11T11:14:13.993+0530 I CONTROL  [initandlisten]     target_arch: x86_64
2018-09-11T11:14:13.993+0530 I CONTROL  [initandlisten] options: {}
2018-09-11T11:14:13.993+0530 I STORAGE  [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2018-09-11T11:14:13.993+0530 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=7680M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2018-09-11T11:14:14.698+0530 I STORAGE  [initandlisten] WiredTiger message [1536644654:698414][11307:0x7fff8fb43380], txn-recover: Main recovery loop: starting at 2/7040
2018-09-11T11:14:14.792+0530 I STORAGE  [initandlisten] WiredTiger message [1536644654:792304][11307:0x7fff8fb43380], txn-recover: Recovering log 2 through 3
2018-09-11T11:14:14.856+0530 I STORAGE  [initandlisten] WiredTiger message [1536644654:856010][11307:0x7fff8fb43380], txn-recover: Recovering log 3 through 3
2018-09-11T11:14:14.904+0530 I STORAGE  [initandlisten] WiredTiger message [1536644654:904754][11307:0x7fff8fb43380], txn-recover: Set global recovery timestamp: 0
2018-09-11T11:14:15.037+0530 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] 
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] 
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-09-11T11:14:15.103+0530 I CONTROL  [initandlisten] 
2018-09-11T11:14:15.157+0530 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2018-09-11T11:14:15.161+0530 I NETWORK  [initandlisten] waiting for connections on port 27017

mongodb version is db version v4.0.2 and am using mac.

Also, when I bind ip of my M2 machine, and do a mongo --host m2.address , i get this:

mongo --host m2.address
MongoDB shell version v4.0.2
connecting to: mongodb://m2.address:27017/
2018-09-11T11:31:07.186+0530 E QUERY    [js] Error: couldn't connect to server m2.address:27017, connection attempt failed: SocketException: Error connecting to m2.address:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:257:13
@(connect):1:6
exception: connect failed

UPDATE

These are the steps I perform before trying to connect:

  1. Start mongodb service: brew services start mongodb
  2. Start mongod by running command mongod
  3. Using Robo3T to create a connection by specifying 0.0.0.0 as connection address. Tried specific address as well.
  4. All giving "Network Unreachable" error.

Let me know if am missing any parameters on start or if am doing anything wrong.

roger_that
  • 9,493
  • 18
  • 66
  • 102

1 Answers1

0

Following all the answers, I tried adding the M2 address in mongodb.conf file but no luck.

This is wrong. net.bindIp contains list of M1 interfaces that server listens on, so for M2 to connect to M1, the net.bindIp should have M1 ip address.

Also, when I bind ip of my M2 machine, and do a mongo --host m2.address , i get this:

This is wrong. The --host parameter should have ip address of the server - M1 in your case.

Using Robo3T to create a connection by specifying 0.0.0.0

This makes no sense. 0.0.0.0 is not a valid ip address at all. It can be used as a wildcard value for net.bindIp in mongodb.conf on M1 to make the server listening on all available interfaces. Robo3T is a client, and you need to give it server's ip address to connect to it.

On M1 (server):

  • set net.bindIp: 0.0.0.0
  • restart mongod

On M2 (client):

  • connect to the server: mongo --host m1.address

Troubleshooting:

  1. Confirm mongodb is up and running on M1 - run command sudo lsof -i | grep mongod. It should print at least 1 process with something like TCP *:27017 (LISTEN).
  2. If p1 works, confirm the network is ok and no firewalls block the connection - on M2 execute telnet m1_ip 27017 It should give you something like Escape character is '^]'.
Community
  • 1
  • 1
Alex Blex
  • 34,704
  • 7
  • 48
  • 75
  • Did everything you've mentioned. `mongo --host m1.address` on terminal giving Operation Time out and Robo3T is giving `Unreachable Network m1.address` – roger_that Sep 11 '18 at 12:32
  • I have update the answer with most common scenarios - server didn't start or a firewall blocks connection. Please check if it is not the case. – Alex Blex Sep 11 '18 at 15:32
  • Also, I noticed `2018-09-11T11:14:13.993+0530 I CONTROL [initandlisten] options: {}` - could you confirm it is different after you changed `mongodb.conf`. It might be just the case that the config file you are modifying is not being used by the db. In this case it listens to local interface only and is unreachable from other computers. – Alex Blex Sep 11 '18 at 15:38