在 docker 容器中时无法连接到 MySQL(python MySql-连接器)
Cannot connect to MySQL (python MySql-Connector) when in docker container
我有一个 Python 基于 Flask 的应用程序需要连接到数据库
version: "3.7"
networks:
localdev:
driver: bridge
services:
sysman-db:
image: mysql:8.0
container_name: sysman-db
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "4000:3306"
environment:
MYSQL_ROOT_PASSWORD: xxxxx
volumes:
- ./database/docker:/etc/mysql/conf.d
- ./database/schema.sql:/docker-entrypoint-initdb.d/dump0.sql
networks:
- localdev
sysman:
build:
context: .
dockerfile: Dockerfile.sysman
container_name: sysman
depends_on:
- sysman-db
ports:
- "3030:3030"
networks:
- localdev
links:
- lsm-db
Flask 应用程序连接到 MySql,它有一个重试方法,因为 Docker 在数据库初始化完成之前启动了 sysman。
即使数据库已启动,我仍然得到:
sysman | 2020-02-11 13:55:08 [ERROR] Unable to connect to db, reason: 2003: Can't connect to MySQL server on 'sysman-db:4000' (111 Connection refused)
sysman | 2020-02-11 13:55:18 [ERROR] Unable to connect to db, reason: 2003: Can't connect to MySQL server on 'sysman-db:4000' (111 Connection refused)
sysman | 2020-02-11 13:55:28 [ERROR] Unable to connect to db, reason: 2003: Can't connect to MySQL server on 'sysman-db:4000' (111 Connection refused)
sysman | 2020-02-11 13:55:38 [ERROR] Unable to connect to db, reason: 2003: Can't connect to MySQL server on 'sysman-db:4000' (111 Connection refused)
数据库连接是:
dbConfig = { 'host' : '127.0.0.1', 'port' : 4000, 'user' : user,
'password' : password, 'database' : database }
connPool = mysql.connector.pooling.MySQLConnectionPool(pool_name='myPool',
pool_size=3,
pool_reset_session=True,
**dbConfig)
如果我自己启动 MySql 容器然后使用 mysql -u root -p -P 4000 我可以连接。
您的 sysman 容器应使用 sysman-db:3306(而非 4000)连接到数据库容器。请记住,这是 2 个服务之间的通信,因此已发布的端口不算数。
您正在尝试连接没有 运行 mysqld 的烧瓶容器的本地主机。已发布端口的 4000 个端口计数。 2 个容器应该通过 3306 端口通信
试试这个:
dbConfig = { 'host' : 'sysman-db', 'port' : 3306, 'user' : user,
'password' : password, 'database' : database }
我有一个 Python 基于 Flask 的应用程序需要连接到数据库
version: "3.7"
networks:
localdev:
driver: bridge
services:
sysman-db:
image: mysql:8.0
container_name: sysman-db
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "4000:3306"
environment:
MYSQL_ROOT_PASSWORD: xxxxx
volumes:
- ./database/docker:/etc/mysql/conf.d
- ./database/schema.sql:/docker-entrypoint-initdb.d/dump0.sql
networks:
- localdev
sysman:
build:
context: .
dockerfile: Dockerfile.sysman
container_name: sysman
depends_on:
- sysman-db
ports:
- "3030:3030"
networks:
- localdev
links:
- lsm-db
Flask 应用程序连接到 MySql,它有一个重试方法,因为 Docker 在数据库初始化完成之前启动了 sysman。
即使数据库已启动,我仍然得到:
sysman | 2020-02-11 13:55:08 [ERROR] Unable to connect to db, reason: 2003: Can't connect to MySQL server on 'sysman-db:4000' (111 Connection refused)
sysman | 2020-02-11 13:55:18 [ERROR] Unable to connect to db, reason: 2003: Can't connect to MySQL server on 'sysman-db:4000' (111 Connection refused)
sysman | 2020-02-11 13:55:28 [ERROR] Unable to connect to db, reason: 2003: Can't connect to MySQL server on 'sysman-db:4000' (111 Connection refused)
sysman | 2020-02-11 13:55:38 [ERROR] Unable to connect to db, reason: 2003: Can't connect to MySQL server on 'sysman-db:4000' (111 Connection refused)
数据库连接是:
dbConfig = { 'host' : '127.0.0.1', 'port' : 4000, 'user' : user,
'password' : password, 'database' : database }
connPool = mysql.connector.pooling.MySQLConnectionPool(pool_name='myPool',
pool_size=3,
pool_reset_session=True,
**dbConfig)
如果我自己启动 MySql 容器然后使用 mysql -u root -p -P 4000 我可以连接。
您的 sysman 容器应使用 sysman-db:3306(而非 4000)连接到数据库容器。请记住,这是 2 个服务之间的通信,因此已发布的端口不算数。
您正在尝试连接没有 运行 mysqld 的烧瓶容器的本地主机。已发布端口的 4000 个端口计数。 2 个容器应该通过 3306 端口通信
试试这个:
dbConfig = { 'host' : 'sysman-db', 'port' : 3306, 'user' : user,
'password' : password, 'database' : database }