Pratibha
Pratibha

Reputation: 1786

Django connection to Mongo Atlas Cluster

I am trying to connect my Django1.9 application with cloud DB instance at Mongo Atlas using Pymongo. Our DB name is accounts.

So far I was connecting my local mongodb instance using

mongoengine.connect(
    username=MONGODB_DATABASES[db]['user'], 
    password=MONGODB_DATABASES[db]['password'], 
    host=MONGODB_DATABASES[db]['host'], 
    port=MONGODB_DATABASES[db]['port'], 
    db=MONGODB_DATABASES[db]['name']
)

According to example, I have to use below string to connect.

import pymongo

client = pymongo.MongoClient("mongodb+srv://<DB USER>:<password>@clustername-osaot.mongodb.net/test?retryWrites=true&w=majority")

db = client.test

I am able to connect to cluster via mongo shell. But when I run above code, I get errors


>>> client = pymongo.MongoClient("mongodb://BackendUser:[email protected]/test?retryWrites=true&w=majority")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/site-packages/pymongo/mongo_client.py", line 248, in __init__
    res = uri_parser.parse_uri(entity, port)
  File "/usr/lib/python3.6/site-packages/pymongo/uri_parser.py", line 308, in parse_uri
    options = split_options(opts)
  File "/usr/lib/python3.6/site-packages/pymongo/uri_parser.py", line 211, in split_options
    return validate_options(options)
  File "/usr/lib/python3.6/site-packages/pymongo/uri_parser.py", line 153, in validate_options
    option, value = validate(option, value)
  File "/usr/lib/python3.6/site-packages/pymongo/common.py", line 306, in validate
    value = validator(option, value)
  File "/usr/lib/python3.6/site-packages/pymongo/common.py", line 53, in raise_config_error
    raise ConfigurationError("Unknown option %s" % (key,))
pymongo.errors.ConfigurationError: Unknown option retryWrites

>>> client = pymongo.MongoClient("mongodb://BackendUser:[email protected]/test?w=majority")
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/pymongo/mongo_client.py", line 363, in __init__
    self._ensure_connected(True)
  File "/usr/lib/python3.6/site-packages/pymongo/mongo_client.py", line 924, in _ensure_connected
    self.__ensure_member()
  File "/usr/lib/python3.6/site-packages/pymongo/mongo_client.py", line 797, in __ensure_member
    member, nodes = self.__find_node()
  File "/usr/lib/python3.6/site-packages/pymongo/mongo_client.py", line 888, in __find_node
    raise AutoReconnect(', '.join(errors))
pymongo.errors.AutoReconnect: [Errno -5] No address associated with hostname

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/site-packages/pymongo/mongo_client.py", line 366, in __init__
    raise ConnectionFailure(str(e))
pymongo.errors.ConnectionFailure: [Errno -5] No address associated with hostname
>>>

I am not able to connect even if I remove everything after clustername.

Can someone please help me?

Upvotes: 0

Views: 1195

Answers (1)

chuck_sum
chuck_sum

Reputation: 111

Per the documentation - you will need to upgrade to version 3.4 or later. This documentation can be found under the Atlas Cluster -- Connect menu (same location where you found the connection string).

Upvotes: 1

Related Questions