Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect to mongodb running on a remote machine

I have mongodb running on a remote server. I can ssh to the remote server and connect to mongodb from the shell on the remote machine. However i have to connect to that mongodb instance from my python script. However, im unable to connect to mongodb directly from the shell on my local machine running linux using the command:

mongo <remote_ip>:27017

or through pymongo using

connection = pymongo.Connection("<remote_ip>", 27017)

I get the below error when using pymongo:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/pymongo-1.11-py2.6-linux-i686.egg/pymongo/connection.py", line 370, in __init__
    self.__find_master()
  File "/usr/local/lib/python2.6/dist-packages/pymongo-1.11-py2.6-linux-i686.egg/pymongo/connection.py", line 605, in __find_master
    raise AutoReconnect("could not find master/primary")
AutoReconnect: could not find master/primary

What could be causing this problem ?. Does it mean mongo is running on a port other than 27017 and if so how can i find out which port it is running on ?

Please Help Thank You

like image 584
Jim Avatar asked Jan 18 '26 20:01

Jim


1 Answers

You can use netstat -a -p on the machine running mongodb to see what port it's attached to. (netstat -a lists all connections and -p provides the name of the program owning the connection.) Also make sure the remote computer is allowing external connections on that port (the connections aren't being blocked by a firewall) and that mongodb is accepting remote connections.

like image 57
agf Avatar answered Jan 21 '26 08:01

agf