Flask-Mongoengine,始终采用默认的连接信息而不是config(localhost:27017:[Errno 61] ECONNREFUSED)

时间:2019-02-24 14:07:09

标签: python flask mongoengine flask-mongoengine

在创建烧瓶应用程序时,我正在配置数据库连接,

def create_app(config_file=None):
    app = Flask(__name__)
    # app.config.from_pyfile(config_file, silent=True)
    app.config.from_object(config_file)

    # db.init_app(app, config=app.config['MONGODB_SETTINGS'])
    db.init_app(app, config={
        'db': "DB_NAME",
        'host': "docker-mongodb",
        'port': "27017",
        'username': 'myuser',
        'password': '******'
    })

但是,当访问通过Mongoengine模型对象查询db的rest入口点时,pymongo在尝试连接到本地主机时会引发错误,换句话说,它不会读取配置。

模型类:

from app import db
from app.domain.platform_integration import PlatformIntegration


class Company(db.Document):
    name = db.StringField()
    type = db.StringField()

Rest API:

(...)
results = list(Company.objects.aggregate(*pipeline))
return JSONEncoder().encode(results)

这是日志:

    def _select_servers_loop(self, selector, timeout, address):
        """select_servers() guts. Hold the lock when calling this."""
        now = _time()
        end_time = now + timeout
        server_descriptions = self._description.apply_selector(
            selector, address)

        while not server_descriptions:
            # No suitable servers.
            if timeout == 0 or now > end_time:
                raise ServerSelectionTimeoutError(
>                   self._error_message(selector))
E               pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 61] ECONNREFUSED

../../outflink-env/lib/python3.7/site-packages/pymongo/topology.py:199: ServerSelectionTimeoutError

我做错了什么?即使我在配置中传递了另一个端口,也总是通过尝试连接到默认端口而中断。

谢谢!

0 个答案:

没有答案