0

I am using the latest version of Pymongo 4.3.3 and my connection string looks like below -

mongodb://userName:password@server:10255/?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&socketTimeoutMS=360000&connectTimeoutMS=360000&appName=myAppName

I use this connection string to connect to the Azure Cosmos DB for MongoDB using the below code -

from pymongo import MongoClient
client = MongoClient(connString)

I intermittently see the error

InvalidURI: Invalid URI scheme: URI must begin with 'mongodb://' or 'mongodb+srv://'

I have tried import dns but not sure how do I use it to resolve this error. My connection string is correct and when I run it from my local it runs successfully. This intermittently fails when run from Azure Function App.

My requirements.txt -

azure-functions
pymongo
pyodbc
dnspython

Can someone please help? Thank you!

Sanket Kelkar

Sanket Kelkar
  • 129
  • 2
  • 9
  • 1
    Most likely variable `connString` does not hold the connection string you show. Try `print(connString)` to verify. – Wernfried Domscheit Mar 31 '23 at 07:00
  • @Wernfried Domscheit Here's the print output- ```mongodb://userName:password@server:10255/?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&socketTimeoutMS=360000&connectTimeoutMS=360000&appName=myAppName``` I had mistakenly put ```appName=@myAppName@``` in my question (now edited). it is actually ```appName=myAppName``` – Sanket Kelkar Mar 31 '23 at 12:09
  • Does your password (or username) has any special characters? Like `: / ? # [ ] @` – Wernfried Domscheit Mar 31 '23 at 12:16
  • Username no, the password contains a couple of ```=```s. – Sanket Kelkar Mar 31 '23 at 12:22
  • Can you check this? Is it helpful to use ```pymongo[tls,srv]``` instead of ```pymongo``` https://stackoverflow.com/a/53644085/11163817 I have already tried this and it ran a couple of times successfully, but can I consider it a permanent solution? – Sanket Kelkar Mar 31 '23 at 12:24
  • Hi @Wernfried Domscheit, can you please help if you know of any workaround? Thanks! – Sanket Kelkar Apr 04 '23 at 04:21
  • According documentation `=` should be no problem. However, try to [encode](https://docs.python.org/3/library/urllib.parse.html#url-quoting) the password and username. – Wernfried Domscheit Apr 04 '23 at 06:50

0 Answers0