请教一下 flask+sqlalchemy+mysql 连接错误问题

2019-05-06 22:56:47 +08:00
 yuting0501

在本地调试 docker-compose 调试 mysql+nginx+flask 程序,报错(2003, "Can't connect to MySQL server on '127.0.0.1' ([Errno 111] Connection refused)")

线索如下:

期待回复,非常感谢!

完整的错误日志:

webapp_1  | The above exception was the direct cause of the following exception:
webapp_1  | 
webapp_1  | Traceback (most recent call last):
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 135, in handle
webapp_1  |     self.handle_request(listener, req, client, addr)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 176, in handle_request
webapp_1  |     respiter = self.wsgi(environ, resp.start_response)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__
webapp_1  |     return self.wsgi_app(environ, start_response)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
webapp_1  |     response = self.handle_exception(e)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
webapp_1  |     reraise(exc_type, exc_value, tb)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
webapp_1  |     raise value
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
webapp_1  |     response = self.full_dispatch_request()
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
webapp_1  |     rv = self.handle_user_exception(e)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
webapp_1  |     reraise(exc_type, exc_value, tb)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
webapp_1  |     raise value
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
webapp_1  |     rv = self.dispatch_request()
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
webapp_1  |     return self.view_functions[rule.endpoint](**req.view_args)
webapp_1  |   File "/iodock/app/auth/views.py", line 58, in register
webapp_1  |     if form.validate_on_submit():
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/flask_wtf/form.py", line 101, in validate_on_submit
webapp_1  |     return self.is_submitted() and self.validate()
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/wtforms/form.py", line 310, in validate
webapp_1  |     return super(Form, self).validate(extra)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/wtforms/form.py", line 152, in validate
webapp_1  |     if not field.validate(self, extra):
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/wtforms/fields/core.py", line 204, in validate
webapp_1  |     stop_validation = self._run_validation_chain(form, chain)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/wtforms/fields/core.py", line 224, in _run_validation_chain
webapp_1  |     validator(form, self)
webapp_1  |   File "/iodock/app/auth/forms.py", line 31, in validate_email
webapp_1  |     if UserBaseInfo.query.filter_by(email=field.data).first():
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 2755, in first
webapp_1  |     ret = list(self[0:1])
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 2547, in __getitem__
webapp_1  |     return list(res)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 2855, in __iter__
webapp_1  |     return self._execute_and_instances(context)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 2876, in _execute_and_instances
webapp_1  |     close_with_result=True)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 2885, in _get_bind_args
webapp_1  |     **kw
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 2867, in _connection_from_session
webapp_1  |     conn = self.session.connection(**kw)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 998, in connection
webapp_1  |     execution_options=execution_options)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1003, in _connection_for_bind
webapp_1  |     engine, execution_options)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 403, in _connection_for_bind
webapp_1  |     conn = bind.contextual_connect()
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2112, in contextual_connect
webapp_1  |     self._wrap_pool_connect(self.pool.connect, None),
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2151, in _wrap_pool_connect
webapp_1  |     e, dialect, self)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1465, in _handle_dbapi_exception_noconnection
webapp_1  |     exc_info
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
webapp_1  |     reraise(type(exception), exception, tb=exc_tb, cause=cause)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 186, in reraise
webapp_1  |     raise value.with_traceback(tb)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2147, in _wrap_pool_connect
webapp_1  |     return fn()
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 387, in connect
webapp_1  |     return _ConnectionFairy._checkout(self)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 766, in _checkout
webapp_1  |     fairy = _ConnectionRecord.checkout(pool)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 516, in checkout
webapp_1  |     rec = pool._do_get()
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1138, in _do_get
webapp_1  |     self._dec_overflow()
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
webapp_1  |     compat.reraise(exc_type, exc_value, exc_tb)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 187, in reraise
webapp_1  |     raise value
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1135, in _do_get
webapp_1  |     return self._create_connection()
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 333, in _create_connection
webapp_1  |     return _ConnectionRecord(self)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 461, in __init__
webapp_1  |     self.__connect(first_connect_check=True)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 651, in __connect
webapp_1  |     connection = pool._invoke_creator(self)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 105, in connect
webapp_1  |     return dialect.connect(*cargs, **cparams)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 393, in connect
webapp_1  |     return self.dbapi.connect(*cargs, **cparams)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/pymysql/__init__.py", line 94, in Connect
webapp_1  |     return Connection(*args, **kwargs)
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/pymysql/connections.py", line 325, in __init__
webapp_1  |     self.connect()
webapp_1  |   File "/usr/local/lib/python3.6/site-packages/pymysql/connections.py", line 630, in connect
webapp_1  |     raise exc
webapp_1  | sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on '127.0.0.1' ([Errno 111] Connection refused)")
3678 次点击
所在节点    Python
8 条回复
fngtz
2019-05-06 23:34:18 +08:00
Eds1995
2019-05-06 23:36:15 +08:00
先在部署 flask 的终端里测试 telnet 127.0.0.1 2003 能不能连通
WilliamYang
2019-05-06 23:41:16 +08:00
好好看一下 1L 的文档
toono
2019-05-07 01:11:48 +08:00
1. 确认 mysql 的 3306 端口已经映射到宿主机上;
2. 如果 flask 程序是在另一个容器内,通过 localhost 是不能访问到的,需要 docker-compose 的 link,访问对应的 hosts,或者访问宿主机的 ip 也可以。
ebingtel
2019-05-07 09:03:15 +08:00
network mode 设置成 host 模式,就能访问了……因为在多个容器里面,使用 localhost (指的是各个容器内部了), 所以不同,方案 见 1L
yuting0501
2019-05-07 09:24:35 +08:00
原来如此,谢谢大家!
yuting0501
2019-05-07 09:42:15 +08:00
更新一下状态,将`127.0.0.1`改为我的 mysql 容器名 mysql 指定网络名后解决问题:

```
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:my_password@127.0.0.1:3306/my_database?charset=utf8'
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:my_password@mysql:3306/my_database?charset=utf8'
```

希望能帮到和我一样的新手
julyclyde
2019-05-07 14:11:36 +08:00
这就是用 docker 没事找事搞出来的

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/561618

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX