MySQL 一段时间后连接超时 (Python, MySQL, FLASK)
MySQL connection times out after a while (Python, MySQL, FLASK)
我目前在 uWGI 的帮助下在 NGINX 服务器上 运行 一个烧瓶 python 应用程序 运行。静态页面始终可访问,但使用连接 (MySQL) 的页面在 2 分钟后超时。这是怎么回事?它们变得不可用。
我尝试过的事情:
- 不使用全局
- 使用池
- 关闭防火墙
.
# using python version 2.7.10
from flask import Flask, jsonify, request, session
import mysql.connector.pooling
#Make a connection with the DB
dbconfig = {
"host" : "12.34.5.78",
"database": "db",
"user": "user",
"password": "pass"
}
conn = mysql.connector.connect(pool_name = "mypool",
pool_size = 6,
**dbconfig)
#Define the root
app = Flask(__name__)
#Landings page
@app.route('/')
def index():
return "Hello World."
# return all resources by name
@app.route('/resources', methods=['GET'])
def allResourceNames():
conn1 = mysql.connector.connect(pool_name="mypool")
reader = conn1.cursor()
query = ("SELECT name FROM resources")
reader.execute(query)
resources = []
for name in reader:
resources.append({'name' : name[0]})
reader.close()
conn1.close()
return jsonify({"resources" : resources})
if __name__ == "__main__":
app.run(debug=True)
根据MySQL Python Connector Doc
您可能希望将 connection_timeout
设置为连接选项。例如,
conn = mysql.connector.connect(pool_name = "mypool",
pool_size = 6, connection_timeout=3600,
**dbconfig)
我目前在 uWGI 的帮助下在 NGINX 服务器上 运行 一个烧瓶 python 应用程序 运行。静态页面始终可访问,但使用连接 (MySQL) 的页面在 2 分钟后超时。这是怎么回事?它们变得不可用。
我尝试过的事情:
- 不使用全局
- 使用池
- 关闭防火墙
.
# using python version 2.7.10
from flask import Flask, jsonify, request, session
import mysql.connector.pooling
#Make a connection with the DB
dbconfig = {
"host" : "12.34.5.78",
"database": "db",
"user": "user",
"password": "pass"
}
conn = mysql.connector.connect(pool_name = "mypool",
pool_size = 6,
**dbconfig)
#Define the root
app = Flask(__name__)
#Landings page
@app.route('/')
def index():
return "Hello World."
# return all resources by name
@app.route('/resources', methods=['GET'])
def allResourceNames():
conn1 = mysql.connector.connect(pool_name="mypool")
reader = conn1.cursor()
query = ("SELECT name FROM resources")
reader.execute(query)
resources = []
for name in reader:
resources.append({'name' : name[0]})
reader.close()
conn1.close()
return jsonify({"resources" : resources})
if __name__ == "__main__":
app.run(debug=True)
根据MySQL Python Connector Doc
您可能希望将 connection_timeout
设置为连接选项。例如,
conn = mysql.connector.connect(pool_name = "mypool",
pool_size = 6, connection_timeout=3600,
**dbconfig)