Invalid URI scheme: mongodb+srv

Hi,

I am trying to connect my python application. Every time I am getting Invalid URI scheme: mongodb+srv error while trying to connect with mongo Atlas.

Please help me where i m doing wrong.

Hello @Alok_verma

From The Fine Manual:
https://docs.atlas.mongodb.com/driver-connection/

Does your look like this ?

import pymongo
import dns # required for connecting with SRV

client = pymongo.MongoClient("mongodb+srv://kay:myRealPassword@cluster0.mongodb.net/test?w=majority")
db = client.test

Please provide the following information so that we can troubleshoot the issue:

  • Full error traceback
  • PyMongo and dnspython version installed
  • MongoDB URI you are using with the username/password removed.
1 Like

Hi Prashant,

please find details below

Error: Invalid URI scheme: mongodb+srv
python flask code which i m trying along with URI

CONNECTION_STR=“mongodb+srv://username:passsword@cluster0.mongodb.net/API?retryWrites=true&w=majority”

    def get_connection(self):
        try:
            client = MongoClient(MongoDbConnection.CONNECTION_STR)
            db = client.API
            return db
        except Exception as e:
            print(e)

Hi @Alok_verma and welcome to the forums!

Unfortunately I’m unable to reproduce the error that you’re encountering. Are you able to reproduce the error consistently ? Are you able to connect from the same machine to the Atlas cluster using mongo shell ?

Could you also provide the following information :

  • The version of MongoDB Python driver that you’re using
  • The full stack trace of the error

Regards,
Wan.

Hi wan,

here is my error and the version of pymongo
pymongo==3.11.3


InvalidURI                                Traceback (most recent call last)
<ipython-input-86-5c9178705b3c> in <module>()
      1 from pymongo import MongoClient
----> 2 client = MongoClient("mongodb+srv://XXX:XXX@cluster0.gxys0.mongodb.net/aiq?retryWrites=true&w=majority")

/Users/shamim/workspaceroot/2020/openrules_dev/lib/python2.7/site-packages/pymongo/mongo_client.pyc in __init__(self, host, port, document_class, tz_aware, connect, **kwargs)
    398           .. seealso:: :doc:`/examples/server_selection`
    399 
--> 400           | **Authentication:**
    401 
    402           - `username`: A string.

InvalidURI: Invalid URI scheme: mongodb+srv

request your help, also want to understand how do we connect through pymongo using certificate provided by atlas

You might be missing a package. Last python project I was using dnspython==1.15.0 with Anaconda Since pymongo was 3.7.0, you might need to use a more recent dnspython.

1 Like