使用docker-compose从Flask Application连接到MySQL [英] Connecting to MySQL from Flask Application using docker-compose
问题描述
我有一个使用Flask和MySQL的应用程序。该应用程序未从Flask应用程序连接到MySQL容器,但可以使用具有相同凭据的Sequel Pro进行访问。
I have an application using Flask and MySQL. The application does not connect to MySQL container from the Flask Application but it can be accessed using Sequel Pro with the same credentials.
Docker Compose File
version: '2'
services:
web:
build: flask-app
ports:
- "5000:5000"
volumes:
- .:/code
mysql:
build: mysql-server
environment:
MYSQL_DATABASE: test
MYSQL_ROOT_PASSWORD: root
MYSQL_ROOT_HOST: 0.0.0.0
MYSQL_USER: testing
MYSQL_PASSWORD: testing
ports:
- "3306:3306"
MySQL的Docker文件
MySQL的docker文件将从 test.dump
文件中添加架构。
The docker file for MySQL will add schema from test.dump
file.
FROM mysql/mysql-server
ADD test.sql /docker-entrypoint-initdb.d
用于Flask的Docker文件
FROM python:latest
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["app.py"]
起点 app.py
Starting point app.py
from flask import Flask, request, jsonify, Response
import json
import mysql.connector
from flask_cors import CORS, cross_origin
app = Flask(__name__)
def getMysqlConnection():
return mysql.connector.connect(user='testing', host='0.0.0.0', port='3306', password='testing', database='test')
@app.route("/")
def hello():
return "Flask inside Docker!!"
@app.route('/api/getMonths', methods=['GET'])
@cross_origin() # allow all origins all methods.
def get_months():
db = getMysqlConnection()
print(db)
try:
sqlstr = "SELECT * from retail_table"
print(sqlstr)
cur = db.cursor()
cur.execute(sqlstr)
output_json = cur.fetchall()
except Exception as e:
print("Error in SQL:\n", e)
finally:
db.close()
return jsonify(results=output_json)
if __name__ == "__main__":
app.run(debug=True,host='0.0.0.0')
当我使用REST Client在 http:// localhost:5000 /
上执行GET请求时,我得到一个有效的响应。
When I do a GET request on http://localhost:5000/
using REST Client I get a valid response.
http:// localhost:5000 / api / getMonths
上的GET请求给出错误消息:
A GET request on http://localhost:5000/api/getMonths
gives error message:
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '0.0.0.0:3306' (111 Connection refused)
在Sequel Pro上使用相同的凭据时,我可以访问数据库。
When the same credentials were used on Sequel Pro, I was able to access the database.
请向我建议如何从Flask应用程序连接MySQL容器。这是我第一次起诉Docker,如果这是我的愚蠢错误,请原谅我。
Please advice me on how to connect the MySQL container from the Flask Application. This is my first time suing Docker and do forgive me if this is a silly mistake from my part.
推荐答案
更改此
return mysql.connector.connect(user='testing', host='0.0.0.0', port='3306', password='testing', database='test')
至
return mysql.connector.connect(user='testing', host='mysql', port='3306', password='testing', database='test')
您的代码在容器内而不是在主机上运行。因此,您需要为其提供一个可以在容器网络中到达的地址。对于 docker-compose
,每个服务都可以使用其名称进行访问。因此,在您的计算机中是 mysql
,因为这是您用于服务的名称
Your code is running inside the container and not on your host. So you need to provide it a address where it can reach within container network. For docker-compose
each service is reachable using its name. So in your it is mysql
as that is name you have used for the service
这篇关于使用docker-compose从Flask Application连接到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!