gpt4 book ai didi

mysql - 为什么无法访问mysql服务?

转载 作者:行者123 更新时间:2023-11-29 03:47:22 26 4
gpt4 key购买 nike

在建立连接期间,服务名称未解析为 mysql 的主机。服务名称是 mysqldb 但是当我在连接数据库时将它用作主机时,它会产生错误。

已解决:问题出在容器的启动上。 flask 容器没有等待 mysql 容器就绪。这就是它早早退出的原因。我通过在失败时对 Flask 容器使用重启策略进行了快速修复。

docker-compose.yml 文件:

version: '3.7'
services:
mysqldb:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: myapp
MYSQL_PASSWORD: root
ports:
- "3333:3306"
volumes:
- ./db:/docker-entrypoint-initdb.d/:ro
flask-app:
build: .
ports:
- "5000:5000"
depends_on:
- mysqldb

使用 mysql.connector

mydb = mysql.connector.connect(host="mysqldb", user="root",password="root", database="myapp", port="3306")

这是我遇到的错误

flask-app_1  | Traceback (most recent call last):
flask-app_1 | File "/usr/local/lib/python3.7/site-packages/mysql/connector/network.py", line 509, in open_connection
flask-app_1 | self.sock.connect(sockaddr)
flask-app_1 | ConnectionRefusedError: [Errno 111] Connection refused
flask-app_1 |
flask-app_1 | During handling of the above exception, another exception occurred:
flask-app_1 |
flask-app_1 | Traceback (most recent call last):
flask-app_1 | File "app.py", line 10, in <module>
flask-app_1 | mydb = mysql.connector.connect(host="mysqldb", user="root",password="root", database="myapp", port="3306")
flask-app_1 | File "/usr/local/lib/python3.7/site-packages/mysql/connector/__init__.py", line 179, in connect
flask-app_1 | return MySQLConnection(*args, **kwargs)
flask-app_1 | File "/usr/local/lib/python3.7/site-packages/mysql/connector/connection.py", line 95, in __init__
flask-app_1 | self.connect(**kwargs)
flask-app_1 | File "/usr/local/lib/python3.7/site-packages/mysql/connector/abstracts.py", line 716, in connect
flask-app_1 | self._open_connection()
flask-app_1 | File "/usr/local/lib/python3.7/site-packages/mysql/connector/connection.py", line 206, in _open_connection
flask-app_1 | self._socket.open_connection()
flask-app_1 | File "/usr/local/lib/python3.7/site-packages/mysql/connector/network.py", line 512, in open_connection
flask-app_1 | errno=2003, values=(self.get_address(), _strioerror(err)))
flask-app_1 | mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'mysqldb:3306' (111 Connection refused)
flaskapp_flask-app_1 exited with code 1


这是 docker-compose ps

Name                     Command             State                 Ports              
----------------------------------------------------------------------------------------------
flaskapp_flask-app_1 python3 app.py Up 0.0.0.0:5000->5000/tcp
flaskapp_mysqldb_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3333->3306/tcp, 33060/tcp

docker 网络 ls

NETWORK ID          NAME                DRIVER              SCOPE
509b1e597bc4 bridge bridge local
b841295e0dc6 flaskapp_default bridge local
2cd96006df91 host host local
260025d73006 none null local

最佳答案

正如有人在您的评论中所述,您需要配置networkslinks

这是一个如何使用 docker-compose.yml 示例配置网络的示例:

version: '3.7'
services:
mysqldb:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: myapp
MYSQL_PASSWORD: root
ports:
- "3333:3306"
volumes:
- ./db:/docker-entrypoint-initdb.d/:ro
networks:
- mynetwork
flask-app:
build: .
ports:
- "5000:5000"
depends_on:
- mysqldb
networks:
- mynetwork
networks:
mynetwork:

mysqldbflask-app 都将加入mynetwork 网络。

我通常会过度测试,所以我也会 docker-compose exec flask-app bash 并运行 ping mysql 命令(假设安装了 ping)来确保你在您的两个容器之间建立网络连接。

此外,虽然我提到了 links,但您应该避免使用它,因为它是一个已弃用的功能,可能会在未来的 docker-compose 版本中被删除。

关于mysql - 为什么无法访问mysql服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57554083/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com