简体   繁体   English

Python Flask mongoengine/pymongo:mongo 的端口 27018 上的连接被拒绝

[英]Python Flask mongoengine/pymongo: Connection refused on port 27018 of mongo

The error is:错误是:

pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 601833aec47f2f6e0a5ca109, topology_type: Single, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('localhost:27017: [Errno 111] Connection refused')>]>

I created an image with docker with port 27018 .我使用端口为27018的 docker 创建了一个图像。 Like this:像这样:

docker run --name test -e MONGODB_DATABASE=ms-content-test -e MONGODB_USER=ms-content-test -e MONGODB_PASS=ms-content-test  **-p 27018:27017** -d mongo

On my config.py file for tests, I created the connection, like this:在用于测试的 config.py 文件中,我创建了连接,如下所示:

class TestingConfig:
    TESTING = True
    DEBUG = True

    # Database Test
    DB = os.environ.get('DB', 'ms-content-test')
    USERNAME = os.environ.get('USERNAME', 'ms-content-test')
    PASSWORD = os.environ.get('PASSWORD', 'ms-content-test')
    HOST = os.environ.get('HOST', 'localhost')
    PORT = int(os.environ.get('PORT', 27018))

    MONGO_URI = 'mongodb://{username}:{password}@{host}:{port}/{database}'.format(
        username=USERNAME,
        password=PASSWORD,
        host=HOST,
        port=PORT,
        database=DB
    )

Now, when I try to test the application, the port 27018 is not working.现在,当我尝试测试应用程序时,端口27018无法正常工作。 Using the port 27017 , works normally.使用端口27017 ,可以正常工作。 On MONGO_URI, I have already tried to pass some arguments like authSource="admin" or using pymongo variables to connect, but nothing works.在 MONGO_URI 上,我已经尝试过传递一些 arguments 之类的authSource="admin"或使用 pymongo 变量进行连接,但没有任何效果。

Internally is like the Mongodb is always forcing use the 27017 port.内部就像 Mongodb 总是强制使用27017端口。 This is the lib of mongoengine: https://github.com/MongoEngine/mongoengine/blob/master/mongoengine/connection.py这是mongoengine的库: https://github.com/MongoEngine/mongoengine/blob/master/mongoengine/connection.py

Your docker run command is incorrect.您的docker run命令不正确。 The format of the -p argument is -p CONTAINER_PORT:HOST_PORT . -p参数的格式是-p CONTAINER_PORT:HOST_PORT

Your -p 27018:27017 argument is redirecting all request from port 27017 on your development machine to port 27018 on your docker container.您的-p 27018:27017参数将所有请求从开发机器上的端口 27017 重定向到 docker 容器上的端口 27018。

If you were trying to publish a range of ports you would have to have individual -p arguments for each of them.如果您尝试发布一系列端口,则必须为每个端口设置单独的-p arguments。

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

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