- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在努力使用 Django、Djongo、Mongo 和 Docker Compose 运行一个简单的应用程序。我的设置如下所示:
docker-compose.yml
services:
mongodb:
image: mongo:latest
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: mongoadmin
MONGO_INITDB_DATABASE: django_mongodb_docker
ports:
- 27017:27017
web:
build: ./src
restart: always
command: python src/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- 8000:8000
links:
- mongodb
docker 文件
FROM python:3
ENV PYTHONUNBUFFERED=1
RUN mkdir /code
COPY . /code/
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
设置.py
DATABASES = {
'default': {
'ENGINE': 'djongo',
'HOST': 'mongodb',
'PORT': 27017,
'USER': 'root',
'PASSWORD': 'mongoadmin',
'AUTH_SOURCE': 'admin',
'AUTH_MECHANISM': 'SCRAM-SHA-1',
}
}
令人恼火的是,我能够从我的 Web 容器中使用 pymongo 连接到运行 mongo 的容器。其工作方式如下。
from pymongo import MongoClient
c = MongoClient(
'mongodb://mongodb:27017',
username='root',
password='mongoadmin',
authSource='admin',
authMechanism='SCRAM-SHA-1')
print(c.server_info())
问题是,当我从我的 Web 容器中运行迁移时,出现以下错误。
Traceback (most recent call last):
File "/code/src/manage.py", line 22, in <module>
main()
File "/code/src/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 92, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 53, in __init__
self.build_graph()
File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 216, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
if self.has_table():
File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 56, in has_table
tables = self.connection.introspection.table_names(cursor)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/introspection.py", line 48, in table_names
return get_names(cursor)
File "/usr/local/lib/python3.9/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 "/usr/local/lib/python3.9/site-packages/djongo/introspection.py", line 47, in get_table_list
for c in cursor.db_conn.list_collection_names()
File "/usr/local/lib/python3.9/site-packages/pymongo/database.py", line 863, in list_collection_names
for result in self.list_collections(session=session, **kwargs)]
File "/usr/local/lib/python3.9/site-packages/pymongo/database.py", line 825, in list_collections
return self.__client._retryable_read(
File "/usr/local/lib/python3.9/site-packages/pymongo/mongo_client.py", line 1460, in _retryable_read
server = self._select_server(
File "/usr/local/lib/python3.9/site-packages/pymongo/mongo_client.py", line 1278, in _select_server
server = topology.select_server(server_selector)
File "/usr/local/lib/python3.9/site-packages/pymongo/topology.py", line 241, in select_server
return random.choice(self.select_servers(selector,
File "/usr/local/lib/python3.9/site-packages/pymongo/topology.py", line 199, in select_servers
server_descriptions = self._select_servers_loop(
File "/usr/local/lib/python3.9/site-packages/pymongo/topology.py", line 215, in _select_servers_loop
raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 5f9ecaa5bbdc0433baa13966, topology_type: Single, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('localhost:27017: [Errno 111] Connection refused')>]>
此外,我尝试创建一个 Djongo 模型并保存它以查看问题是否可能特定于迁移(本质上只是尝试使用 djongo 与 mongo 建立任何连接)。
模型.py
from djongo import models
class Blog(models.Model):
name = models.CharField(max_length=100)
测试.py
b = Blog(name='test')
b.save()
返回以下错误:
The above exception was the direct cause of the following exception:
web_1 |
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.9/site-packages/djongo/cursor.py", line 51, in execute
web_1 | self.result = Query(
web_1 | File "/usr/local/lib/python3.9/site-packages/djongo/sql2mongo/query.py", line 783, in __init__
web_1 | self._query = self.parse()
web_1 | File "/usr/local/lib/python3.9/site-packages/djongo/sql2mongo/query.py", line 884, in parse
web_1 | raise exe from e
web_1 | djongo.exceptions.SQLDecodeError:
web_1 |
web_1 | Keyword: None
web_1 | Sub SQL: None
web_1 | FAILED SQL: INSERT INTO "game_blog" ("name") VALUES (%(0)s)
web_1 | Params: ['test']
web_1 | Version: 1.3.3
web_1 |
web_1 | The above exception was the direct cause of the following exception:
web_1 |
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
web_1 | return self.cursor.execute(sql, params)
web_1 | File "/usr/local/lib/python3.9/site-packages/djongo/cursor.py", line 59, in execute
web_1 | raise db_exe from e
web_1 | djongo.database.DatabaseError
web_1 |
web_1 | The above exception was the direct cause of the following exception:
web_1 |
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
web_1 | response = get_response(request)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/core/handlers/base.py", line 179, in _get_response
web_1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/views/generic/base.py", line 70, in view
web_1 | return self.dispatch(request, *args, **kwargs)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/views/generic/base.py", line 98, in dispatch
web_1 | return handler(request, *args, **kwargs)
web_1 | File "/code/src/game/views.py", line 9, in get
web_1 | b.save(using='mongo')
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 753, in save
web_1 | self.save_base(using=using, force_insert=force_insert,
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 790, in save_base
web_1 | updated = self._save_table(
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 895, in _save_table
web_1 | results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 933, in _do_insert
web_1 | return manager._insert(
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
web_1 | return getattr(self.get_queryset(), name)(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 1254, in _insert
web_1 | return query.get_compiler(using=using).execute_sql(returning_fields)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1397, in execute_sql
web_1 | cursor.execute(sql, params)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute
web_1 | return super().execute(sql, params)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
web_1 | return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
web_1 | return executor(sql, params, many, context)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
web_1 | return self.cursor.execute(sql, params)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
web_1 | raise dj_exc_value.with_traceback(traceback) from exc_value
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
web_1 | return self.cursor.execute(sql, params)
web_1 | File "/usr/local/lib/python3.9/site-packages/djongo/cursor.py", line 59, in execute
web_1 | raise db_exe from e
web_1 | django.db.utils.DatabaseError
如有任何帮助,我们将不胜感激。
我试过像 this 一样编辑我的 mongodb.conf已经。
最佳答案
根据 this document ,settings.py
应该有一个 CLIENT
部分,其中包含:
A set of key-value pairs that will be passed directly to MongoClient as kwargs while creating a new client connection.
因此请尝试将您的 settings.py
设置为:
DATABASE = {
'default': {
'ENGINE': 'djongo',
'NAME': 'your-database-name',
'CLIENT': {
'host': 'mongodb://mongodb:27017',
'username': 'root',
'password': 'mongoadmin',
'authSource': 'admin',
'authMechanism': 'SCRAM-SHA-1',
}
}
}
关于Django + Mongo + Docker 获取 pymongo.errors.ServerSelectionTimeoutError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64616386/
我正在尝试扩展我的第一个 Flask 应用程序,但不了解在多个模块中使用 pymongo 数据库所需的结构。例如,这是我的新结构: run.py app/ ├── __init__.py ├── fo
Pymongo 返回一个游标,我可以迭代结果并将其文档附加到列表中。有没有办法直接获取列表中的结果文档?谢谢 最佳答案 以下代码会将整个结果集(光标)转换为列表: myresults = list(m
我使用pip3成功安装了pymongo Aleeshas-MacBook-Air:project 2 aleesha$ sudo pip3 install pymongo The directory
我正在尝试运行非常简单的代码来弄清楚如何将 pymongo 与 MongoDB Atlas Cloud 结合使用。 这是示例代码 import pymongo client = pymongo.Mon
我想找到集合 (mycol) 中的文档的 _id,其中 "name":"John"。我已插入文档,但想找到文档的 _id。可能吗 ?我正在尝试 result = db.mycol.find({"_id
我是 PyMongo 和 Flask 的新手,不过我已经完成了 Flask 的教程,并且对它感觉很舒服。我现在正在尝试使用 MongoDb 实现 flask 服务器,但我不确定如何进行。 我看到有两个
我正在尝试在 mongodb 集合中批量插入一些文档。 我对我收藏的链接字段施加了唯一约束。 bulkUrls = db.urls.initialize_ordered_bulk_op() for i
简单介绍一下背景知识,我之前曾在 Node.js 和 mongoose.js 中使用过 MongoDB。现在我决定尝试使用 python 和 pymongo。但是,当我尝试将文档插入到我的集合中时,我
我正在获取 MongoDB 中某个集合的全部数据,一段时间后(比如 30 或 60 分钟),脚本会引发以下错误: pymongo.errors.CursorNotFound: cursor id 18
我在 mongodb 数据库中消费了一堆推文。我想使用 pymongo 查询这些推文。例如,我想查询 screen_name。但是,当我尝试这样做时,python 不会返回推文,而是返回有关 pymo
我有一个 合作伙伴集合,我正在使用 pymongo 来检索数据 当我使用 MongoDB 查询集合时,我看到以下结果 db.partner.find({'unique_key': 'c89dbe313
我正在尝试使用 Pymongo 将字典插入到 MongoDB 集合中。代码段为 newdict = {'id': a, 'usr_id': i.get('user_id'), 'reach_value
我尝试使用 pymongo 连接 MongoDB。但是遇到了dnspython必须安装的错误即使在我安装了 pymongo 和 dnspython 之后。 我的代码是: import pymongo
我正在使用 pymongo 将数亿条格式为 {'id_str': , 'created_at': , 'text': } 的推文从文本文件迁移到 MongoDB。为每个用户创建一个集合来存储他/她的推
我知道这是一个相当普遍的问题。我正在编写一个小型 Flask 应用程序,并试图将一些查询反馈给 View 。 我已经连接到我的本地 MongoDB 设置,并进行了成功的查询 - 但我无法用它生成 js
我想从 ObjectId 对象中获取字符串字符。我用的是pymongo。例如:ObjectId("543b591d91b9e510a06a42e2"),我想获取"543b591d91b9e510a06
我想实现一个函数,该函数需要以与插入相反的顺序从 pymongo 集合中获取值。 我可以想到几个方法: cursor = collection.find(skip=collection.count()
从pymongo文档: MongoDB以BSON格式存储数据。 BSON字符串采用UTF-8编码,因此PyMongo必须确保 它存储的任何字符串仅包含有效的UTF-8数据。常规字符串()>已验证,并且
目录 查询数据 设置查询条件 更多查询操作 PS:pymongo最大查询限制 解决方案 查询数据 往
修订问题。将很快更新。 最佳答案 演示 - https://mongoplayground.net/p/ksay82IaGHs 按 TeacherID 分组和 TeacherID并获得组合的出现,$s
我是一名优秀的程序员,十分优秀!