Django - OperationalError: (2006, 'MySQL server has gone away')

前端 未结 4 1352
陌清茗
陌清茗 2021-02-19 04:56

Bottom line first: How do you refresh the MySQL connection in django?

Following a MySQL server has gone away error I found that MySQL docum

4条回答
  •  眼角桃花
    2021-02-19 05:17

    The main reason that leads to this exception is mostly due to client ideal longer than wait_timeout on mysql server.

    In order to prevent that kind of error, django supports an option named CONN_MAX_AGE which allow django to recreate new connection if old connections are ideal too long. So you should make sure that CONN_MAX_AGE value is smaller than wait_timout value.

    One important thing is that, django with wsgi handles checking CONN_MAX_AGE every requests by calling close_old_connections. So you mainly don't need to care about that. However if you are using django in standard alone application, there is no trigger to run that function. So you have to call it manually. So let call close_old_connections in your code base.

    Note: close_old_connections will keep old connections if they're not expired yet. Your connections are still reused in case of high frequency query.

提交回复
热议问题