0

I have used the below stackoverflow question link to try to connect to MongoDb atlas cluster using Flask MongoAlchemy but this does not seem to be working. Below is my code:>

from flask import Flask
from flask_mongoalchemy import MongoAlchemy

app = Flask(__name__)
DB_URI = 'mongodb+srv://subhayan:<password>@mflix-jprgs.mongodb.net/test?retryWrites=true&w=majority'
app.config['MONGOALCHEMY_DATABASE'] = 'test'
app.config["MONGODB_HOST"] = DB_URI


db = MongoAlchemy(app)

class Person(db.document):
    first_name = db.StringField()
    last_name = db.StringField()
    age = db.IntField()

I have correctly replaced the with the actual password . I tried importing this Python file from the REPL but i get the below error:

(python-mongo) ~/Desktop/Studies/Codes/python-mongo:$ python
Python 3.7.3 (default, Mar 27 2019, 09:23:15) 
[Clang 10.0.1 (clang-1001.0.46.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from connect import *
Traceback (most recent call last):
  File "/Users/subhayanbhattacharya/.local/share/virtualenvs/python-mongo-adwM96Vd/lib/python3.7/site-packages/pymongo/mongo_client.py", line 375, in __init__
    self._ensure_connected(True)
  File "/Users/subhayanbhattacharya/.local/share/virtualenvs/python-mongo-adwM96Vd/lib/python3.7/site-packages/pymongo/mongo_client.py", line 940, in _ensure_connected
    self.__ensure_member()
  File "/Users/subhayanbhattacharya/.local/share/virtualenvs/python-mongo-adwM96Vd/lib/python3.7/site-packages/pymongo/mongo_client.py", line 814, in __ensure_member
    member, nodes = self.__find_node()
  File "/Users/subhayanbhattacharya/.local/share/virtualenvs/python-mongo-adwM96Vd/lib/python3.7/site-packages/pymongo/mongo_client.py", line 905, in __find_node
    raise AutoReconnect(', '.join(errors))
pymongo.errors.AutoReconnect: [Errno 61] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/subhayanbhattacharya/Desktop/Studies/Codes/python-mongo/connect.py", line 10, in <module>
    db = MongoAlchemy(app)
  File "/Users/subhayanbhattacharya/.local/share/virtualenvs/python-mongo-adwM96Vd/lib/python3.7/site-packages/flask_mongoalchemy/__init__.py", line 101, in __init__
    self.init_app(app, config_prefix)
  File "/Users/subhayanbhattacharya/.local/share/virtualenvs/python-mongo-adwM96Vd/lib/python3.7/site-packages/flask_mongoalchemy/__init__.py", line 123, in init_app
    host=uri, replicaSet=rs)
  File "/Users/subhayanbhattacharya/.local/share/virtualenvs/python-mongo-adwM96Vd/lib/python3.7/site-packages/mongoalchemy/session.py", line 126, in connect
    conn = MongoClient(*args, **kwds)
  File "/Users/subhayanbhattacharya/.local/share/virtualenvs/python-mongo-adwM96Vd/lib/python3.7/site-packages/pymongo/mongo_client.py", line 378, in __init__
    raise ConnectionFailure(str(e))
pymongo.errors.ConnectionFailure: [Errno 61] Connection refused

Can someone please help me understand what the issue is. I am also not finding proper documentation for this.

Subhayan Bhattacharya
  • 5,407
  • 7
  • 42
  • 60
  • question is answered here : https://stackoverflow.com/questions/63425102/how-to-configure-mongodb-atlas-in-flask-using-flask-mongoalchemy – chamod rathnayake Sep 27 '21 at 07:25

1 Answers1

0

It possible that your password has an escape character

try it this way

import urllib.parse

username = urllib.parse.quote_plus("UserName")
password = urllib.parse.quote_plus("Password")

client = MongoClient('mongodb+srv://%s:%s@mflix-jprgs.mongodb.net/test?retryWrites=true&w=majority' % (username, password))
Moiz Ahmed
  • 11
  • 3