我读到了很多有关此错误的问题,但没有一个能解决我的问题。
我对Python服务器方面不是很熟悉,并且在与Gunicorn和Flask一起运行的api上出现此错误。
试图: -删除所有cursor.close() -在API调用之后,在返回之前添加cursor.close()
这是我的连接器:
db = mysql.connector.connect(
host="127.0.0.1",
user="root",
port=3306,
password='password',
database='db'
)
api调用示例:
@api.route('/update-entities', methods=['POST'])
def update_entities():
try:
validation = call_controller(ApiController(request.json).update_entities, 'update_entities')
cursor = c.pearch_db.cursor()
cursor.close()
return jsonify({'status': validation.status, 'status_code': validation.status_code, 'message': validation.message}), validation.status_code
except Exception:
cursor = c.pearch_db.cursor()
cursor.close()
return jsonify(traceback.format_exc()), 500
错误:
Dec 13 11:47:43 cybertruck gunicorn[35123]: return self.wsgi_app(environ, start_response)
Dec 13 11:47:43 cybertruck gunicorn[35123]: File "/var/www/russell-python/Api/middleware.py", line 17, in __c
Dec 13 11:47:43 cybertruck gunicorn[35123]: organization = Organization.find_one_by_client_id_and_client_se
Dec 13 11:47:43 cybertruck gunicorn[35123]: File "/var/www/russell-python/Common/Model/organization.py", line
Dec 13 11:47:43 cybertruck gunicorn[35123]: results = h.query(sql)
Dec 13 11:47:43 cybertruck gunicorn[35123]: File "/var/www/russell-python/Common/helpers.py", line 19, in que
Dec 13 11:47:43 cybertruck gunicorn[35123]: cursor = db.cursor(dictionary=dictionary)
Dec 13 11:47:43 cybertruck gunicorn[35123]: File "/var/www/russell-python/pearch/local/lib/python3.6/site-pac
Dec 13 11:47:43 cybertruck gunicorn[35123]: raise errors.OperationalError("MySQL Connection not available."
Dec 13 11:47:43 cybertruck gunicorn[35123]: mysql.connector.errors.OperationalError: MySQL Connection not avail
因此,此错误迫使我每次都要重新启动负责处理gunicorn的服务,否则api将无法正常工作。重新启动该服务可再解决几分钟。该API可能同时被5个人使用。
这是ubuntu服务器中的服务配置(不幸的是,失败后重启无法解决此问题)
[Unit]
Description=Gunicorn instance to serve Pearch Python API
After=network.target
[Service]
Restart=on-failure
RestartSec=5s
#We will give our regular user account ownership of the process since it owns all of the relevant files
User=www-data
#give group ownership to the www-data group so that Nginx can communicate easily with the Gunicorn processes.
Group=www-data
# We'll then map out the working directory and set the PATH environmental variable so that the init system knows where our the executables for the process are located (within our virtual environment).
WorkingDirectory=/var/www/russell-python
Environment="PATH=/var/www/russell-python/pearch/bin"
# We'll then specify the commanded to start the service
ExecStart=/var/www/russell-python/pearch/bin/gunicorn --timeout 300 --workers 25 --bind unix:app.sock -m 007 wsgi:api
# This will tell systemd what to link this service to if we enable it to start at boot. We want this service to start when the regular multi-user system is up and running:
[Install]
WantedBy=multi-user.target
Mysql具有以下配置:
max_connections = 1000
connect_timeout = 5
wait_timeout = 600
max_allowed_packet = 16M
thread_cache_size = 128
sort_buffer_size = 4M
bulk_insert_buffer_size = 16M
tmp_table_size = 32M
max_heap_table_size = 32M
nginx网站是这样的:
server {
server_name serverher;
proxy_connect_timeout 75s;
proxy_read_timeout 300s;
location / {
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
proxy_connect_timeout 75s;
proxy_read_timeout 300s;
include proxy_params;
proxy_pass http://unix:/var/www/russell-python/app.sock;
}
access_log /var/log/nginx/russell-python-access.log;
error_log /var/log/nginx/russell-python-error.log;
}
我相信mysql和nginx都很好,而且gunicorn的超时设置很大,我不知道还有什么可以检查的。
答案 0 :(得分:0)
对于任何看过这个问题的人,我都有同样的问题,并将其追溯到另一个错误作为根本原因。
我的32个连接池中的连接用完了,因此我进行了一些研究后发现,不应该使用cnx.close()
,而应使用reset_session
,然后使用add_connection
来保留32个连接在池中为32,并且不会随着时间而减少(有效的批处理作业和API调用每个请求进行的调用超过32个)。
“ MySQL连接不可用”的原因是如果发生连接reset_session
,则使用.is_connected() # False
“ MySQL连接不可用”。
为修复这两个错误,我继续使用自己的关闭方法,如下所示:
def close(self):
try:
cnx = self.con
if self.con.reset_session:
cnx.reset_session()
except OperationalError as err:
if err.msg != 'MySQL Connection not available.':
# This close method only needs to close available connections
raise err
finally:
self.con.add_connection(cnx)
self.con = None
因此,如果您看到“ MySQL连接不可用”,请找出您是否正在使用reset_session
并在那里处理该错误。
希望这会有所帮助