简体   繁体   中英

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 . 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:

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. Using the port 27017 , works normally. On MONGO_URI, I have already tried to pass some arguments like authSource="admin" or using pymongo variables to connect, but nothing works.

Internally is like the Mongodb is always forcing use the 27017 port. This is the lib of mongoengine: https://github.com/MongoEngine/mongoengine/blob/master/mongoengine/connection.py

Your docker run command is incorrect. The format of the -p argument is -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.

If you were trying to publish a range of ports you would have to have individual -p arguments for each of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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