[英]Cannot connect to remote MongoDB server using flask-mongoengine
Trying to connect to a MongoDB cluster hosted on a remote server using flask-mongoengine but the following error is thrown: 尝试使用flask-mongoengine连接到远程服务器上托管的MongoDB集群,但抛出以下错误:
File "test.py", line 9, in <module>
inserted = Something(some='whatever').save()
File "/home/lokesh/Desktop/Work/Survaider_Apps/new_survaider/survaider-env/lib/python3.5/site-packages/mongoengine/document.py", line 323, in save
object_id = collection.save(doc, **write_concern)
File "/home/lokesh/Desktop/Work/Survaider_Apps/new_survaider/survaider-env/lib/python3.5/site-packages/pymongo/collection.py", line 2186, in save
with self._socket_for_writes() as sock_info:
File "/usr/lib/python3.5/contextlib.py", line 59, in __enter__
return next(self.gen)
File "/home/lokesh/Desktop/Work/Survaider_Apps/new_survaider/survaider-env/lib/python3.5/site-packages/pymongo/mongo_client.py", line 762, in _get_socket
server = self._get_topology().select_server(selector)
File "/home/lokesh/Desktop/Work/Survaider_Apps/new_survaider/survaider-env/lib/python3.5/site-packages/pymongo/topology.py", line 210, in select_server
address))
File "/home/lokesh/Desktop/Work/Survaider_Apps/new_survaider/survaider-env/lib/python3.5/site-packages/pymongo/topology.py", line 186, in select_servers
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: admin:27017: [Errno -2] Name or service not known
Below is the code I am using: 以下是我正在使用的代码:
# test.py
from my_app_module import app
from flask_mongoengine import MongoEngine
db = MongoEngine(app)
class Something(db.Document):
some = db.StringField()
inserted = Something(some='whatever').save()
print(inserted)
for obj in Something.objects:
print(obj)
My config.py
file contains: 我的
config.py
文件包含:
# config.py
MONGODB_SETTINGS = {
'db': 'testdb',
'host': 'mongodb://<my_username>:<my_password>@<my_cluster_replica_1>.mongodb.net:27017,<my_cluster_replica_2>.mongodb.net:27017,<my_cluster_replica_3>.mongodb.net:27017/admin?ssl=true&replicaSet=<my_cluster>&authSource=admin',
}
But I can connect using pymongo
using the following code. 但是我可以使用
pymongo
通过以下代码进行连接。
from pymongo import MongoClient
uri = 'mongodb://<my_username>:<my_password>@<my_cluster_replica_1>.mongodb.net:27017,<my_cluster_replica_2>.mongodb.net:27017,<my_cluster_replica_3>.mongodb.net:27017/admin?ssl=true&replicaSet=<my_cluster>&authSource=admin'
client = MongoClient(uri)
db = client['testdb']
db.test_collection.insert({'some_key': 'some_value'})
for col in db.test_collection.find():
print(col)
# Prints {'some_key': 'some_value', '_id': ObjectId('57ec35d9312f911329e54d5e')}
I tried to find a solution but nobody seems to have come across the problem before. 我试图找到一个解决方案,但以前似乎没有人遇到过这个问题。 I am using MongoDB's Atlas solution to host the MongoDB cluster.
我正在使用MongoDB的Atlas解决方案来托管MongoDB集群。
我发现这是flask-mongoengine版本0.8
中的一个错误,并已在此处报告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.