Flask Python Gunicorn MySql连接不可用

2024-06-16 11:20:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我读了很多关于这个错误的问题,但是没有一个能解决我的问题。你知道吗

我不太熟悉Python服务器端的东西,我在一个运行Gunicorn和Flask的api上遇到了这个错误。你知道吗

试图: -全部删除光标.关闭() -添加光标.关闭()就在api调用之后返回之前

这是我的连接器:

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有一个很大的超时设置,我不知道还有什么可以检查的。你知道吗


Tags: thetologapidbsizevarwww