0

I'm trying to import data from MongoDB to Azure Machine learning with a python script. I use the following script:

import pymongo as pymongo
import pandas as pd

def azureml_main(dataframe1 = None, dataframe2 = None):
    client = pymongo.MongoClient("SERVER:USERNAME:PASSWORD")
    db = client['DATABASE']
    coll = db['COLLECTION']
    cursor = coll.find().limit(10)
    df = pd.DataFrame(list(cursor))
    return df,

This gives me the following error:

Error 0085: The following error occurred during script evaluation, please view the output log for more information:
---------- Start of error message from Python interpreter ----------
Caught exception while executing function: Traceback (most recent call last):
  File "C:\server\invokepy.py", line 199, in batch
    odfs = mod.azureml_main(*idfs)
  File "C:\temp\416f67ae321a4f7b9a2d5eda63aa127c.py", line 23, in azureml_main
    df = pd.DataFrame(list(cursor))
  File "C:\pyhome\lib\site-packages\pymongo\cursor.py", line 977, in next
    if len(self.__data) or self._refresh():
  File "C:\pyhome\lib\site-packages\pymongo\cursor.py", line 902, in _refresh
    self.__read_preference))
  File "C:\pyhome\lib\site-packages\pymongo\cursor.py", line 813, in __send_message
    **kwargs)
  File "C:\pyhome\lib\site-packages\pymongo\mongo_client.py", line 728, in _send_message_with_response
    server = topology.select_server(selector)
  File "C:\pyhome\lib\site-packages\pymongo\topology.py", line 121, in select_server
    address))
  File "C:\pyhome\lib\site-packages\pymongo\topology.py", line 97, in select_servers
    self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: SERVERNAME:XXXXX:[WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond,SERVERNAME:XXXXX: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond,SERVERNAME:XXXXX: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Process returned with non-zero exit code 1

Is this caused by not whitelisting any IP adresses? I can't find any information on what kind of IP comes out of the Azure ML. Is there a workaround to this issue?

Robert
  • 41
  • 2
  • 6

1 Answers1

0

That error is nothing to do with any IP whitelisting; it's related to not being able to connect to your mongo database. Check your connection string, and that your server is running. The connection string should look something like

mongodb://username:password@server:27017/yourdatabase?authSource=admin

First check it works from your chosen command prompt / shell using

mongo mongodb://username:password@server:27017/yourdatabase?authSource=admin

then change your python connection to:

client = pymongo.MongoClient("<working connection string>")
Belly Buster
  • 8,224
  • 2
  • 7
  • 20
  • I've tried to use the same script in visual studio code and i'm able to get the data without any problems. I've even tried it with a connection string ment for a older python, this works fine too. But when I run it in Azure Machine learning studio i'm still stuck on the timeout – Robert Sep 27 '19 at 09:11
  • OK I understand your comment on whitelisting a bit better now. Maybe something is blocking 27017 as it isn't a "standard" port; you may need to enable something somewhere to allow that access; I don't know anything about Azure ML so can't help in that regard I'm afraid. Hvae you tested your mongodb is accessible from the outside world? – Belly Buster Sep 27 '19 at 10:15
  • Not sure if you found the answer, but maybe this can explain the problem: https://stackoverflow.com/questions/54502828/import-mongodb-data-to-azure-ml-studio-from-python-script – gneusch Nov 04 '20 at 21:49