doableware / djongo

Django and MongoDB database connector
https://www.djongomapper.com
GNU Affero General Public License v3.0
1.88k stars 355 forks source link

Having trouble connecting to MongoDB Atlas #132

Closed mohamed-elsabagh closed 6 years ago

mohamed-elsabagh commented 6 years ago

I am trying to connect my django application to my remote Mongodb replica set running on Atlas, how can this be done in the configuration in the settings.py how can I fill the configuration for the following dns for example

mongo "mongodb+srv://dnsServerName/test" --username myusername

I have tried several configuration but it gives me timeout error although my ip address is white listed and I can connect to the DB from my machine

nesdis commented 6 years ago

Sorry, I have no experience with Atlas and probably this is not the right place to raise this ticket. Djongo is for connecting with a MongoDB instance with Django.

kurisu-na commented 6 years ago

Hi, I recently used Djongo with MongoDB Atlas in PythonAnywhere and found the solution. I don't know if this is a good practice or not; I just followed the error trail.

  1. Do pip install dnspython.

  2. Open this file:

    yourvirtualenvfolder/lib/python3.6/site-packages/pymongo/mongo_client.py

  3. Search for this part of code:

    class MongoClient(common.BaseObject):
    HOST = "localhost"
    PORT = 27017
  4. Substitute "localhost" with your SRV address, for example:

    class MongoClient(common.BaseObject):
    HOST = "mongodb+srv://dnsServerName/test"
    PORT = 27017

Save the file, and voila, you're connected to MongoDB Atlas. You don't need to change anything else in settings.py except for djongo database settings.

Hope this helps :)

ghost commented 5 years ago

Sorry, I have no experience with Atlas and probably this is not the right place to raise this ticket. Djongo is for connecting with a MongoDB instance with Django.

This is about how to connect to MongoDB to a remote server. MongoAtlas is just a service provider for mongo DB. I can't find anywhere about how to connect to DB using connection URI.

Do we have provision to connect to DB using URI? If not, having that would be great.

StayFoolisj commented 5 years ago

Hi, I recently used Djongo with MongoDB Atlas in PythonAnywhere and found the solution. I don't know if this is a good practice or not; I just followed the error trail.

  1. Do pip install dnspython.
  2. Open this file:

yourvirtualenvfolder/lib/python3.6/site-packages/pymongo/mongo_client.py

  1. Search for this part of code:
class MongoClient(common.BaseObject):
    HOST = "localhost"
    PORT = 27017
  1. Substitute "localhost" with your SRV address, for example:
class MongoClient(common.BaseObject):
    HOST = "mongodb+srv://dnsServerName/test"
    PORT = 27017

Save the file, and voila, you're connected to MongoDB Atlas. You don't need to change anything else in settings.py except for djongo database settings.

Hope this helps :)

I only needed to run this to make it work: pip3 install dnspython

ajs-88 commented 5 years ago

pip install djongo pip install dnspython

in settings.py

DATABASES = { 'default': { 'ENGINE': 'djongo', 'NAME': 'dbname', 'HOST': 'mongodb+srv://username:password@cluster0-4oblc.gcp.mongodb.net/test?ssl=true&ssl_cert_reqs=CERT_NONE&retryWrites=true', } }

./manage.py makemigrations ./manage.py migrate

agiokas-dbg commented 4 years ago

Why is this closed? It is still a bug with djongo 1.3.0 I am having the exact same issue. I can even connect to the dockerized mongo container by executing an interactive bash from the app container, and pointing it to the dockerized mongo URI, but django fails to connect returning an error of No mongodb hosts found

Let me ellaborate:

  1. I've added a print(host) in venv/lib/python3.6/site-packages/pymongo/mongo_client.py after changing the host to the mongo URI
  2. it still prints localhsot
  3. in venv/lib/python3.6/site-packages/djongo/base.py line 176 I've also added a print(name) and the method get_new_connection never seems to be called.
  4. under webapp/webapp/settings.py I have in HOST' : 'mongodb://segnetmongo:27017' which always times-out and returns a No mongodb hosts found
  5. however, if I run from an interactive container shell mongo mongodb://segnetmongo:27017/ I get a connection just fine.

I've tried to follow the rabbit down the hole to see why get_new_connection is not called, and I know that pymongo (assuming that's the connection handler for djongo) expects a host parameter, else it defaults to localhost (which it does). What I don't understand is why is the MongoClient constructor isn't not receiving the HOST parameter from the settings.py file.

omkarshinde254 commented 4 years ago

Hi, I recently used Djongo with MongoDB Atlas in PythonAnywhere and found the solution. I don't know if this is a good practice or not; I just followed the error trail.

  1. Do pip install dnspython.
  2. Open this file:

yourvirtualenvfolder/lib/python3.6/site-packages/pymongo/mongo_client.py

  1. Search for this part of code:
class MongoClient(common.BaseObject):
    HOST = "localhost"
    PORT = 27017
  1. Substitute "localhost" with your SRV address, for example:
class MongoClient(common.BaseObject):
    HOST = "mongodb+srv://dnsServerName/test"
    PORT = 27017

Save the file, and voila, you're connected to MongoDB Atlas. You don't need to change anything else in settings.py except for djongo database settings.

Hope this helps :)

This works in my case when using a shared Cluster on Mongo Atlas. Thanks.

irpl commented 4 years ago

Hi, I recently used Djongo with MongoDB Atlas in PythonAnywhere and found the solution. I don't know if this is a good practice or not; I just followed the error trail.

  1. Do pip install dnspython.
  2. Open this file:

yourvirtualenvfolder/lib/python3.6/site-packages/pymongo/mongo_client.py

  1. Search for this part of code:
class MongoClient(common.BaseObject):
    HOST = "localhost"
    PORT = 27017
  1. Substitute "localhost" with your SRV address, for example:
class MongoClient(common.BaseObject):
    HOST = "mongodb+srv://dnsServerName/test"
    PORT = 27017

Save the file, and voila, you're connected to MongoDB Atlas. You don't need to change anything else in settings.py except for djongo database settings. Hope this helps :)

This works in my case when using a shared Cluster on Mongo Atlas. Thanks.

You say that you've got it working with a shared cluster in mongo atlas. I've got my app set up that way too. I'm on version 1.3.2 of djongo. Did you get an error like this?

Applying contenttypes.0001_initial...This version of djongo does not support "schema validation using CONSTRAINT" fully. Visit https://www.patreon.com/nesdis
Traceback (most recent call last):
  File "/home/user/proc/.venv/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 842, in parse
    return handler(self, statement)
  File "/home/user/proc/.venv/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 907, in _insert
    query = InsertQuery(self, self.db, self.connection_properties, sm, self._params)
  File "/home/user/proc/.venv/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 339, in __init__
    super().__init__(*args)
  File "/home/user/proc/.venv/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 61, in __init__
    self.parse()
  File "/home/user/proc/.venv/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 409, in parse
    self._fill_values(statement)
  File "/home/user/proc/.venv/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 367, in _fill_values
    raise SQLDecodeError
djongo.exceptions.SQLDecodeError
nagarjunr commented 4 years ago

Is there any proper fix available for this?

jie8357IOII commented 4 years ago

python3.6

Hi @irpl I fix the same issue when I install sqlparse==0.2.4, give a try.

Ad1tyaV commented 4 years ago

@irpl this will fail when you deploy your application to hosting platforms like heroku or Azure, during build itself, it will override the changes in mongo_client.py

a-kriya commented 4 years ago

@agiokas-dbg @gustavofleury @noelpuru Pretty sure you're encountering this documentation issue: https://github.com/nesdis/djongo/issues/410#issuecomment-646885842

@jie8357IOII Thanks for that hint! I was receiving DatabaseErrors when running migrate, but ignored that little red line warning about djongo requiring sqlparse===0.2.4 during pip install. It was installing v0.3.1 for some reason, but downgrading it made the migrate command work.

jimitrupani commented 4 years ago

Hi, I recently used Djongo with MongoDB Atlas in PythonAnywhere and found the solution. I don't know if this is a good practice or not; I just followed the error trail.

  1. Do pip install dnspython.
  2. Open this file:

yourvirtualenvfolder/lib/python3.6/site-packages/pymongo/mongo_client.py

  1. Search for this part of code:
class MongoClient(common.BaseObject):
    HOST = "localhost"
    PORT = 27017
  1. Substitute "localhost" with your SRV address, for example:
class MongoClient(common.BaseObject):
    HOST = "mongodb+srv://dnsServerName/test"
    PORT = 27017

Save the file, and voila, you're connected to MongoDB Atlas. You don't need to change anything else in settings.py except for djongo database settings. Hope this helps :)

This works in my case when using a shared Cluster on Mongo Atlas. Thanks.

this one solved my problem and for my settings.py

DATABASES = { 'default': { 'ENGINE': 'djongo', 'NAME': 'ebookstore', 'HOST': ', 'USER': '', 'PASSWORD': '', } }

PrabuKannan-DEV commented 4 years ago

sqlparse==0.2.4

This solved the issue, just installed sqlparse==0.2.4 into my virtual env. Thanks brother!

matisotee commented 4 years ago

Hi everyone, I solved the problem with the following steps: settings.py: DATABASES = { 'default': { 'ENGINE': 'djongo', 'NAME': 'db_name', 'CLIENT': { 'host': 'mongodb+srv://your_db_user>:<your_db_password>@cluster0.zhs7q.mongodb.net/<db_name?retryWrites=true&w=majority', 'username': 'your_db_user', 'password': 'your_db_password', }, } }

and then if you get error like this: [SSL: CERTIFICATE_VERIFY_FAILED] you can follow this: https://stackoverflow.com/questions/52805115/certificate-verify-failed-unable-to-get-local-issuer-certificate

HeptaDecane commented 4 years ago

I resolved this issue with the following steps:

  1. pip install djongo (i suppose this is already done)
  2. pip install dnspython
  3. edits in settings.py
    
    # import
    from pymongo.mongo_client import MongoClient

replace default DATABASES dictionary

DATABASES = { 'default': { 'ENGINE': "djongo", 'NAME': , } }

copy connection string provided by atlas only upto dns name

i.e exclude: "?retryWrites=true&w=majority"

following is my case

MongoClient.HOST = "mongodb+srv://:@mongodb.vsega.mongodb.net/"

replace <placeholder(s)> with your credentials (obvio)



>Django==3.0.5
>djongo==1.3.3
>dnspython==2.0.0
>Atlas: MongoDB Version 4.2.10
AASunasara commented 3 years ago

I resolved this issue with the following steps:

  1. pip install djongo (i suppose this is already done)
  2. pip install dnspython
  3. edits in settings.py
# import
from pymongo.mongo_client import MongoClient

# replace default DATABASES dictionary
DATABASES = {
    'default': {
        'ENGINE': "djongo",
        'NAME': <dbname>,
    }
}
# copy connection string provided by atlas only upto dns name 
# i.e exclude: "<dbname>?retryWrites=true&w=majority"
# following is my case
MongoClient.HOST = "mongodb+srv://<username>:<password>@mongodb.vsega.mongodb.net/"

# replace <placeholder(s)> with your credentials (obvio)

Django==3.0.5 djongo==1.3.3 dnspython==2.0.0 Atlas: MongoDB Version 4.2.10

this is really helpful

iamthehimansh commented 3 years ago

pip install djongo pip install dnspython

in settings.py

DATABASES = { 'default': { 'ENGINE': 'djongo', 'NAME': 'dbname', 'HOST': 'mongodb+srv://username:password@cluster0-4oblc.gcp.mongodb.net/test?ssl=true&ssl_cert_reqs=CERT_NONE&retryWrites=true', } }

./manage.py makemigrations ./manage.py migrate

Thanks a lot this worked for me