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.