Resolve #2006-mysql server has gone away error

Source: Internet
Author: User

When you use PHP to insert large data into MySQL, you encounter a problem:

2006:mysql server has gone away:: INSERT into Hu Jintao

After looking, knowing the reason, is that MySQL's max_allowed_packet value is set too small or not set, this variable is used to control the maximum length of its communication buffer.

Workaround:

[Mysqld]
Max_allowed_packet = 16M

In the my.cnf file, set the above variable to the Mysqld section, and if not, add it.

The main explanations for Max_allowed_packet are as follows:

The maximum size of the package or any generated/intermediate string. This process occurs mostly in the load data file and sometimes in Update,insert.

The package message buffer is initialized to net_buffer_length bytes, but can grow to max_allowed_packet bytes when needed. This value is small by default to capture large (possibly incorrect) packets.

If you use a large BLOB column or a long string, you must increase the value. Should be as large as the largest blob you want to use. The Max_allowed_packet protocol is limited to 1GB. This value must be a multiple of 1024, and if it is not 1024 times times the value, MySQL will automatically be rounded to the nearest multiple of 1024.

When you change the value of the Max_allowed_packet, you change the size of the message buffer, and you should also modify the client's buffer size within the scope allowed by the client. On the client side, the Max_allowed_packet default value is 1GB. You can change the Max_allowed_packet value of the client through the command line or configuration file.

In the MySQL5.0.84 version, the session Max_allowed_packet value, and is only read-only. Before the 5.0.84 version, the Max_allowed_packet value can be set in the session, but it doesn't work.

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.