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

前端 未结 4 1345
陌清茗
陌清茗 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:13

    The idea of the solution is clear: reconnect to mysql if the current connection is broken.

    Please check this out:

    def make_sure_mysql_usable():
        from django.db import connection, connections
        # mysql is lazily connected to in django.
        # connection.connection is None means
        # you have not connected to mysql before
        if connection.connection and not connection.is_usable():
            # destroy the default mysql connection
            # after this line, when you use ORM methods
            # django will reconnect to the default mysql
            del connections._connections.default
    

提交回复
热议问题