MySQL server has gone away error handling

Source: Internet
Author: User

Solution 1:

This is a mechanism of MySQL itself:
MySQL automatically disconnects the connection after more than 8 hours of idle time
There are two solutions:
1. Modify MySQL Configuration
Increase the value of the MySQL Wait_timeout property
2, regular access to MySQL, to maintain the idle time of MySQL connection
In the application, write a timed task to access the MySQL database regularly


The above describes the "Django database re-connect mechanism" of the problem answer, I hope to have the need to help users.
This article site link: http://www.codes51.com/itwd/2831093.html

Problem description

When using Django+celery, run a long task, start the database one time, and then operate the database again after 20 hours.

After more than more than 20 hours, again operation database times wrong.

Raised Unexpected:operationalerror (2006,' MySQL server has gone away ') Traceback (the most recent callLast): File"/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py",Line85,In Manager_methodReturn GetAttr (Self.get_queryset (), name) (*args, **kwargs) File"/usr/local/lib/python2.7/dist-packages/django/db/models/query.py",Line374,InchGetnum =Len (clone) File"/usr/local/lib/python2.7/dist-packages/django/db/models/query.py",Line232,In __len__ self._fetch_all () File"/usr/local/lib/python2.7/dist-packages/django/db/models/query.py",line 1118, in _fetch_all self. _result_cache = List (Self. _iterable_class (self)) File  "/usr/local/lib/python2.7/dist-packages/django/db/models/ query.py ", line 53, in __iter__ Results = Compiler.execute_sql (chunked_fetch=self.chunked_fetch) File  "/usr/local/lib/ python2.7/dist-packages/django/db/models/sql/compiler.py ", line 894, in Execute_sql raise Original_exceptionoperationalerror : (2006,  ' MySQL server has gone away ')    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
Reason connection timed out

That's why I'm having problems.
Go to MySQL and execute the following command:

Mysql> Show global variables like'%timeout '; +----------------------------+----------+|variable_name |Value |+----------------------------+----------+| Connect_timeout |10 | | delayed_insert_timeout | 300 | | innodb_lock_wait_timeout | 50 | | innodb_rollback_on_timeout | off | | interactive_timeout | 28800 | | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | slave_net_timeout | 3600 | | wait_timeout | 28800 |+----------------------------+----------+       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

Wait_timeout is 28,800 seconds, that is, the MySQL link is automatically closed after 28,800 seconds of no operation

Workaround takes effect immediately

Execute the following SQL command

set global wait_timeout=60*60*30;
    • 1

The command is to set Wait_timeout to 30 hours.
In this way, the modification takes effect immediately. If you restart MySQL, restore the original 28,800 seconds.

Permanent effect

After this method has been modified, you need to restart MySQL to take effect.
Edit the MySQL configuration file my.cnf, add or modify this command as follows.
(my MySQL config file path:/etc/mysql/my.cnf)

wait_timeout = 108000
    • 1
Other reasons

There are other reasons to simply describe
1:mysql Service shutdown
2: Packet size is too large
Workaround: Modify max_allowed_packet the values as in the same way.

Copyright Notice: Text for the accumulation of the past, often forget to eat waste to sleep, hope friends pass the time note to source. 78196789Article tags: mysqldjangoceleryoperationagone-away

MySQL server has gone away error handling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.