Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot connect to mongodb using machine ip

Tags:

mongodb

Installed Mongo using homebrew. If I type mongo on shell, it gets connected to test. But when I type the ip address of the local machine instead of 127.0.0.1

mongo --host 192.168.1.100 --verbose

It gives me error message

MongoDB shell version: 2.4.6
Fri Aug 23 15:18:27.552 versionArrayTest passed
connecting to: 192.168.1.100:27017/test
Fri Aug 23 15:18:27.579 creating new connection to:192.168.1.100:27017 
Fri Aug 23 15:18:27.579 BackgroundJob starting: ConnectBG
Fri Aug 23 15:18:27.580 Error: couldn't connect to server 192.168.1.100:27017 at src/mongo/shell/mongo.js:147
Fri Aug 23 15:18:27.580 User Assertion: 12513:connect failed

Have tried modifying the mongo.conf by commenting bind_ip or by changing the ip address from 127.0.0.1 to 0.0.0.0 but no luck. This should be simple but have no clue now. Using mac.

Thanks

Update: As requested. This works after I have made the changes as you suggested.

ifconfig output

lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
   options=3<RXCSUM,TXCSUM>
   inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
   inet 127.0.0.1 netmask 0xff000000 
   inet6 ::1 prefixlen 128 
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
   options=b<RXCSUM,TXCSUM,VLAN_HWTAGGING>
   ether XX:XX:XX:
   media: autoselect (none)
   status: inactive
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
   ether XX:XX:XX:XX:01 
   inet6 XXXX:XXXX:XXXX: %en1 prefixlen 64 scopeid 0x5 
   inet 192.168.1.100 netmask 0xffffff00 broadcast 192.168.1.255
   media: autoselect
   status: active
 p2p0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 2304
   ether XX:XX:XX:XX:XX
   media: autoselect
   status: inactive
 fw0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 4078
   lladdr XX:XX:XX:XX
   media: autoselect <full-duplex>
   status: inactive

Output when executing command mongo --host 192.168.1.100 --verbose

MongoDB shell version: 2.4.5
Fri Aug 23 16:42:09.806 versionArrayTest passed
connecting to: 192.168.1.100:27017/test
Fri Aug 23 16:42:09.837 creating new connection to:192.168.1.100:27017
Fri Aug 23 16:42:09.837 BackgroundJob starting: ConnectBG
Fri Aug 23 16:42:10.129 connected connection!
Server has startup warnings: 
Fri Aug 23 16:41:59.025 [initandlisten] 
Fri Aug 23 16:41:59.025 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000

File mongod.conf

# Store data in /usr/local/var/mongodb instead of the default /data/db
dbpath = /usr/local/var/mongodb

# Append logs to /usr/local/var/log/mongodb/mongo.log
logpath = /usr/local/var/log/mongodb/mongo.log
logappend = true

# Only accept local connections
bind_ip = 0.0.0.0`
like image 375
java_dude Avatar asked Aug 23 '13 22:08

java_dude


People also ask

How does MongoDB connect to IP address?

Enable MongoDB Auth In the same config file, go to the network interfaces section and change the bindIp from 127.0. 0.1 to 0.0. 0.0 which means allow connections from all ip addresses. Now save and exit the config file and restart mongodb server.

Why is my MongoDB not connecting?

These are some of the solutions: Ensure that your MongoDB instance is running: Compass must connect to a running MongoDB instance. Also check you have installed MongoDB and have a running mongod process. You should also check that the port where MongoDB is running matches the port you provide in the compass connect.

How do I find my MongoDB IP address?

To find the public IP address for any node in your cluster, use the nslookup tool from the command line. The IP address is shown in the Address portion of the output.

Which TCP IP port does MongoDB use?

Port. The default port number for mongod and mongos instances is 27017.


1 Answers

I just tested this on my Mac with Homebrew, works fine if you change the bind address. I suspect you probably just didn't get the config for bind correct?

Just so we have all the information, can you paste the output of ifconfig please?

By default, MongoDB should listen on all interfaces, you shouldn't need to change the configuration, however, the Homebrew setup seems to override this (/usr/local/etc/mongod.conf):

# Only accept local connections
bind_ip = 127.0.0.1

Please kill MongoDB and run this (note the -v):

$ mongod --bind_ip 0.0.0.0 -v
warning: bind_ip of 0.0.0.0 is unnecessary; listens on all ips by default
all output going to: /usr/local/var/log/mongodb/mongo.log

Just paste your output for that please?

And then just try:

$ mongo --host 192.168.43.2 --verbose
MongoDB shell version: 2.4.6
Sat Aug 24 09:07:14.556 versionArrayTest passed
connecting to: 192.168.43.2:27017/test
Sat Aug 24 09:07:14.657 creating new connection to:192.168.43.2:27017
Sat Aug 24 09:07:14.657 BackgroundJob starting: ConnectBG
Sat Aug 24 09:07:14.657 connected connection!
Server has startup warnings: 
Sat Aug 24 09:06:44.360 [initandlisten] 
Sat Aug 24 09:06:44.360 [initandlisten] ** WARNING: soft rlimits too low. Number of files     is 256, should be at least 1000
> 

Obviously replace it with your IP address. Let us know how that goes.

like image 71
victorhooi Avatar answered Sep 28 '22 04:09

victorhooi