简体   繁体   English

Django 无法连接到 mongoDB 图集

[英]Django cant connect to mongoDB atlas

Recently I started a project to make a Webserver using a Django backend with and a mongoDB database hosted on their Atlas platform so i don't have to worry about running it locally.最近我开始了一个项目,使用 Django 后端和一个托管在他们的 Atlas 平台上的 mongoDB 数据库来制作一个 Web 服务器,所以我不必担心在本地运行它。

Im still in the early stages of setting it up and encountered the this error:我还处于设置的早期阶段,遇到了这个错误:

pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [WinError 10061] No connection could be made because the target machine actively refused it

It might also be relevant to mention that i currently have no documents on the database or models in the form of Django apps, but I doubt that this is the probelm as when i run manage.py migrate i get the above error and not a "no app with label" error.可能还需要提及的是,我目前没有关于 Django 应用程序形式的数据库或模型的文档,但我怀疑这是问题,因为当我运行manage.py migrate我得到了上述错误,而不是“没有带有标签的应用程序”错误。

My database config in my settings.py currently looks like this:我的 settings.py 中的数据库配置目前如下所示:

DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': 'house-project',
        'HOST': 'mongodb+srv://<my-user-name>:<my-password>@house-project-9g5fo.gcp.mongodb.net/test?retryWrites=true&w=majority'
    }
}

I know that one of the common errors is having special characters in your password and username, i have made sure not to include any or escape them with hex characters.我知道常见错误之一是您的密码和用户名中包含特殊字符,我已确保不包含任何字符或使用十六进制字符转义它们。 I have made sure to add my ip and user on the Atlas side.我已确保在 Atlas 端添加我的 ip 和用户。 I have checked online for an exiting answer, to no avail.我在网上查了一个现有的答案,无济于事。 The closest question i found was: How to connect Django ORM to mongo atlas?我发现的最接近的问题是: 如何将 Django ORM 连接到 mongo atlas? , but this solution does not work for me sadly. ,但遗憾的是,此解决方案对我不起作用。

Any help would be great in trying to solve this problem, let me know if any additional info is needed about my setup let me know and i would be happy to provide it.任何帮助都有助于解决此问题,如果需要有关我的设置的任何其他信息,请告诉我,我很乐意提供。 I have also included a stacktrace in case it is relevant below.我还包括了一个堆栈跟踪,以防它在下面相关。

Traceback (most recent call last):
  File "manage.py", line 20, in <module>
    main()
  File "manage.py", line 16, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\core\management\commands\migrate.py", line 87, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\db\migrations\loader.py", line 49, in __init__
    self.build_graph()
  File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\db\migrations\loader.py", line 212, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\db\migrations\recorder.py", line 73, in applied_migrations
    if self.has_table():
  File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\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\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\db\backends\base\introspection.py", line 48, in table_names
    return get_names(cursor)
  File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\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\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\djongo\introspection.py", line 47, in get_table_list
    for c in cursor.db_conn.list_collection_names()
  File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\pymongo\database.py", line 856, in list_collection_names
    for result in self.list_collections(session=session, **kwargs)]
  File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\pymongo\database.py", line 818, in list_collections
    return self.__client._retryable_read(
  File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\pymongo\mongo_client.py", line 1453, in _retryable_read
    server = self._select_server(
  File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\pymongo\mongo_client.py", line 1253, in _select_server
    server = topology.select_server(server_selector)
  File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\pymongo\topology.py", line 233, in select_server
    return random.choice(self.select_servers(selector,
  File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\pymongo\topology.py", line 192, in select_servers
    server_descriptions = self._select_servers_loop(
  File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\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```

The Djongo documentation is not correct or outdated. Djongo 文档不正确或过时。 I was able to get my Django app to connect to a mongodb using the following settings:我能够使用以下设置让我的 Django 应用程序连接到 mongodb:

DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'CLIENT': {
            'host': 'mongodb+srv://<URL>',
            'username': 'something',
            'password': 'somepass',
            'authMechanism': 'SCRAM-SHA-1'
        }
    }
}

I found a very hack-y solution.我找到了一个非常hack-y的解决方案。 Jayadevan was correct, pymongo was trying to connect to my local host instead of the provided URI, despite me specifying host. Jayadevan 是正确的,尽管我指定了主机,pymongo 还是试图连接到我的本地主机而不是提供的 URI。

I was looking for why this might be, and i stumbled upon this github issue.我一直在寻找为什么会这样,我偶然发现了这个github 问题。 There was a suegestion in this thread to change the host in mongo_client.py.在这个线程中有一个 sugestion 来更改 mongo_client.py 中的主机。 This file can be found in your in the directory that your dependency files are in.这个文件可以在你的依赖文件所在的目录中找到。

Changing the HOST in this file does allow my database to connect.更改此文件中的 HOST 确实允许我的数据库连接。 This leads me to the belief that pymongo does not take into account the HOST that you specify.这让我相信 pymongo 不会考虑您指定的 HOST。

If anyone knows either what I missed, or a way that I can either fix my current setup so that I dont have to use this workaround please let me know.如果有人知道我错过了什么,或者我可以修复当前设置以便我不必使用此解决方法的方法,请告诉我。

Same issue.同样的问题。 This is indeed a very hacky way to modify the library settings in order to achieve the desired results.这确实是一种非常hacky的方式来修改库设置以达到预期的结果。 The problem could be stemming from two places: Either from the Djongo engine (most likely) which is not forwarding the specified HOST to pymongo's mongo_client.py constructor.问题可能源于两个地方:要么来自 Djongo 引擎(最有可能),它没有将指定的 HOST 转发到 pymongo 的 mongo_client.py 构造函数。 The other source could be Django itself which is not calling the constructor the right way (unlikely).另一个来源可能是 Django 本身,它没有以正确的方式调用构造函数(不太可能)。 I want to deploy my project to Heroku but this is not possible since all the dependencies I install are going to have this bug and I can't change the HOST name manually.我想将我的项目部署到 Heroku,但这是不可能的,因为我安装的所有依赖项都会有这个错误,而且我无法手动更改 HOST 名称。

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

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