我已经使用下面的 stackoverflow 问题链接尝试使用 Flask MongoAlchemy 连接到 MongoDb atlas 集群,但这似乎不起作用。下面是我的代码:>
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()
我已正确替换为实际密码。我尝试从 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
有人可以帮我理解问题所在。我也没有找到合适的文档。