MongoDB – PyMongo error while connecting to a remote server: [Errno 111] Connection refused

If you are trying to connect a remote server (e.g. Linux Ubuntu) via PyMongo, it is probable that you get an error as below:

  • pymongo.errors.ConnectionFailure: [Errno 111] Connection refused

In order to resolve this issue, you need to enable external interfaces on the remote server by commenting out the line with “bind_ip = 127.0.0.1” in MongoDB configuration file (/etc/mongodb.conf). Here are the steps you can take to achieve this:

  • Edit the MongoDB configuration file
    • sudo nano /etc/mongodb.conf
  • Find the line with “bind_ip = 127.0.0.1”
  • Add a “#” character in front of the line to comment it out
  • Save changes (Ctrl+o) to file and exit the editor (Ctrl+x)
  • Restart the MongoDB service
    • sudo service mongodb restart

By following the above steps, my issue is resolved. However, there are cases having the same error while connecting to the local server, this is because server is shut down uncleanly and need to be repaired.

This entry was posted in Data Analysis, Linux and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *