Python and Django OperationalError (2006, 'MySQL server has gone away')

后端 未结 12 1740
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-14 15:25

Original: I have recently started getting MySQL OperationalErrors from some of my old code and cannot seem to trace back the problem. Since it was working before, I thought

12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-14 15:58

    Sometimes if you see "OperationalError: (2006, 'MySQL server has gone away')", it is because you are issuing a query that is too large. This can happen, for instance, if you're storing your sessions in MySQL, and you're trying to put something really big in the session. To fix the problem, you need to increase the value of the max_allowed_packet setting in MySQL.

    The default value is 1048576.

    So see the current value for the default, run the following SQL:

    select @@max_allowed_packet;
    

    To temporarily set a new value, run the following SQL:

    set global max_allowed_packet=10485760;
    

    To fix the problem more permanently, create a /etc/my.cnf file with at least the following:

    [mysqld]
    max_allowed_packet = 16M
    

    After editing /etc/my.cnf, you'll need to restart MySQL or restart your machine if you don't know how.

提交回复
热议问题