简体   繁体   English

无法使用 Flask mongoalchemy 连接到 mongodb atlas 集群

[英]Unable to connect to mongodb atlas cluster using Flask mongoalchemy

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.我使用下面的 stackoverflow 问题链接尝试使用 Flask MongoAlchemy 连接到 MongoDb atlas 集群,但这似乎不起作用。 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:我尝试从 REPL 导入此 Python 文件,但出现以下错误:

(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.我也没有为此找到适当的文档。

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM