我正在尝试使用 Djongo 将我的 Django 应用程序与 Atlas MongoDB 云数据库连接起来。设置文件数据库部分是,
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'mydb',
'HOST': 'mongodb+srv://user:' + urllib.parse.quote_plus('password') + '@xx-xxxxx.mongodb.net/test?retryWrites=true&w=majority',
'USER': 'user',
'PASSWORD': 'password',
}
}
但是运行后python manage.py migrate
,它会抛出以下错误,
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 364, in execute
output = self.handle(*args, **options)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\commands\migrate.py", line 87, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\migrations\loader.py", line 49, in __init__
self.build_graph()
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\migrations\loader.py", line 212, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\migrations\recorder.py", line 73, in applied_migrations
if self.has_table():
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\migrations\recorder.py", line 56, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\introspection.py", line 48, in table_names
return get_names(cursor)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\introspection.py", line 43, in get_names
return sorted(ti.name for ti in self.get_table_list(cursor)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\djongo\introspection.py", line 47, in get_table_list
for c in cursor.db_conn.list_collection_names()
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymongo\database.py", line 856, in list_collection_names
for result in self.list_collections(session=session, **kwargs)]
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymongo\database.py", line 818, in list_collections
return self.__client._retryable_read(
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymongo\mongo_client.py", line 1453, in _retryable_read
server = self._select_server(
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymongo\mongo_client.py", line 1253, in _select_server
server = topology.select_server(server_selector)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymongo\topology.py", line 233, in select_server
return random.choice(self.select_servers(selector,
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymongo\topology.py", line 192, in select_servers
server_descriptions = self._select_servers_loop(
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymongo\topology.py", line 208, in _select_servers_loop
raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [WinError 10061] No connection could be made because the target machine actively refused it
如果你看最后一行,它说 pymongo 正在尝试连接localhost:27017
。但是主人已经提到了。由于没有本地主机,因此连接失败。
现在,我打开mongo_client.py
里面C:\Users\XXXX\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\pymongo
,发现下面的代码片段,
class MongoClient(common.BaseObject):
"""
A client-side representation of a MongoDB cluster.
Instances can represent either a standalone MongoDB server, a replica
set, or a sharded cluster. Instances of this class are responsible for
maintaining up-to-date state of the cluster, and possibly cache
resources related to this, including background threads for monitoring,
and connection pools.
"""
HOST = "localhost"
PORT = 27017
# Define order to retrieve options from ClientOptions for __repr__.
# No host/port; these are retrieved from TopologySettings.
_constructor_args = ('document_class', 'tz_aware', 'connect')
def __init__(
self,
host=None,
port=None,
document_class=dict,
tz_aware=None,
connect=None,
type_registry=None,
**kwargs):
根据概述,默认主机永远不会超载。因此它总是在搜索本地主机。现在,如果我更改localhost
为'mongodb+srv://user:password@xx-xxxxx.mongodb.net/test?retryWrites=true&w=majority'
,则连接正常。
现在,这绝对不是解决方案。任何人都可以对此有所了解吗?