简体   繁体   English

pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] 连接被拒绝

[英]pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused

I am trying to use docker-compose with my django-rest app.我正在尝试将 docker-compose 与我的 django-rest 应用程序一起使用。 When I run it myself python manage.py runserver it works well.当我自己运行它时, python manage.py runserver它运行良好。

If I am trying to use docker-compose sudo docker-compose up it also runs the server but when I open the page in the browser I get an error.如果我尝试使用 docker-compose sudo docker-compose up它也会运行服务器,但是当我在浏览器中打开页面时出现错误。

pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused

I already have db, so I am just using this lines in settings.py我已经有 db,所以我只是在settings.py中使用这些行

MONGODB_DATABASES = {
    "default": {
        "name": 'api',
        "host": 'localhost',
       "port": 27017
    },
}

Here is my Dockerfile:这是我的 Dockerfile:

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY . /code
RUN pip install -r requirements.txt

My docker-compose.yml:我的 docker-compose.yml:

version: '3.0'
services:
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
  mongo:
    image: mongo

Already tried this:已经尝试过这个:

Pymongo keeps refusing the connection at 27017 Pymongo 在 27017 一直拒绝连接

By default Compose sets up a single network for your app.默认情况下,Compose 会为您的应用程序设置一个网络。 Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.服务的每个容器都加入默认网络,并且可以被该网络上的其他容器访问,并且可以通过与容器名称相同的主机名被它们发现。

According to the docker-compose.yaml file, mongo container is accessible on mongo:27017 from web container, so settings.py should be changed to:根据docker-compose.yaml文件, mongo容器可以通过web容器在mongo:27017上访问,因此settings.py应更改为:

MONGODB_DATABASES = {
    "default": {
        "name": 'api',
        "host": 'mongo',
       "port": 27017
    },
}

I had the same problem.我有同样的问题。

The issue was that I used the wrong port forwarding and had an old container which was stopped but not removed.问题是我使用了错误的端口转发并且有一个旧容器已停止但未删除。

I restarted the old container.我重新启动了旧容器。 Now it's working fine.现在它工作正常。

As described in the other answers the problem is related to the mongo container name in the docker-compose.yml file so the Django settings must be changed to find the correct database host.如其他答案中所述,问题与docker-compose.yml文件中的mongo容器名称有关,因此必须更改 Django 设置才能找到正确的数据库主机。

In later versions Djongo changed the DATABASE options in the settings.py .在以后的版本中,Djongo 更改了settings.py中的DATABASE选项。 This will result in the following new settings.py part:这将产生以下新的settings.py部分:

DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': 'api',
        'CLIENT': {
            'host': 'mongo',
            'port': 27017,
        }
    }
}

Notice that host and port are now childs of CLIENT .请注意, hostport现在是CLIENT的子项。

More information can be found in the Djongo documentation .更多信息可以在Djongo 文档中找到。

What worked for me was simply list all the containers with对我有用的只是列出所有容器

docker ps -a

CONTAINER ID        
q624462b055d        
f9324d078256        
r0888c63d21d        
tf5a32702bee        
e68d1ae9b74c

and delete the mongodb related ones using their IDs并使用他们的 ID 删除与 mongodb 相关的那些

docker rm q624462b055d f9324d078256

and change the host and port in the docker-compose file with并更改 docker-compose 文件中的主机和端口

DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': 'mongo,',
        'HOST': 'mongo',
        'PORT': 27017,
    }
}

暂无
暂无

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

相关问题 pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] 连接拒绝 django 到 heroku - pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused django to heroku Mongodb pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection denied, Timeout: 30s, - Mongodb pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused, Timeout: 30s, pymongo.errors.ServerSelectionTimeoutError:本地主机:27017:[Errno 61]连接被拒绝 - pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 61] Connection refused pymongo.errors.ServerSelectionTimeoutError:localhost:27017:[WinError 10061]无法建立连接,因为目标机器主动拒绝 - pymongo.errors.ServerSelectionTimeoutError:localhost:27017:[WinError 10061]No connection could be made because the target machine actively refused it 连接到 mongo 图集给出 pymongo.errors.ServerSelectionTimeoutError: localhost:27017 - Connecting to mongo atlas gives pymongo.errors.ServerSelectionTimeoutError: localhost:27017 pymongo.errors.ServerSelectionTimeoutError - pymongo.errors.ServerSelectionTimeoutError 烧瓶 - pymongo.errors.ServerSelectionTimeoutError - Flask - pymongo.errors.ServerSelectionTimeoutError pymongo.errors.ServerSelectionTimeoutError:db:27017:超时,超时:30s, - pymongo.errors.ServerSelectionTimeoutError: db:27017: timed out, Timeout: 30s, 在连接到Heroku上的mongoDB时出现错误:“ localhost:27017:[Errno 111]连接被拒绝”,但在我的计算机上工作正常 - Getting error: “localhost:27017: [Errno 111] Connection refused” while connecting to mongoDB on Heroku but works fine on my computer pymongo.errors.ServerSelectionTimeoutError: RepliceSetNoPrimary - MongoDB Atlas - pymongo.errors.ServerSelectionTimeoutError: RepliceSetNoPrimary - MongoDB Atlas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM