MySQL server has gone away Solution

Source: Internet
Author: User
Tags mysql manual

1. applications (such as PHP) execute MySQL statements in batches for a long time. The most common is collection or conversion of New and Old data.

Solution:

Add or modify the following two variables in the my. CNF file:

Wait_timeout = 2880000
Interactive_timeout = 2880000
For details about the two variables, refer to Google or the official manual. If you cannot modify my. CNF, you can set client_interactive when connecting to the database. For example:

SQL = "set interactive_timeout = 24*3600 ";
Mysql_real_query (...)

2. Execute an SQL statement, but the SQL statement is too large or the statement contains blob or longblob fields. For example, processing image data

Solution:

Add or modify the following variables in the my. CNF file:

Max_allowed_packet = 10 m
(You can also set the desired size)

Max_allowed_packet
The parameter is used to control the maximum length of the Communication buffer.

MySQL: the strange MySQL server has gone away and Its Solution
From: http://fz9493.blog.sohu.com/38472203.html

Jimmy | March, 2007

Execute show status in MySQL, which usually focuses on the cache effect and number of processes, and usually ignores two values:

Variable_name Value
Aborted_clients 3792
Aborted_connects 376


It usually accounts for only 0.0x % of query, so it is not paid much attention. In addition, in traditional Web applications, query errors do not affect users much, but it is okay to refresh the page again. In the recent basic transformation, many applications run as services and users cannot be prompted to refresh them. In this case, the service quality may be affected.

The main error message is "MySQL server has gone away" in log tracking of program scripts ". The official explanation is:

The most common reason for the MySQL server has gone away error is that the server timed out and closed the connection.

Some other common reasons for the MySQL server has gone away error are:

You (or the DB administrator) has killed the running thread with a kill statement or a mysqladmin kill command.

You tried to run a query after closing the connection to the server.
This indicates a logic error in the application that should be
Corrected.

A client application running on a different host does not have
Necessary privileges to connect to the MySQL server from that host.

You got a timeout from the TCP/IP connection on the client side.
This may happen if you have been using the commands: mysql_options (...,
Mysql_opt_read_timeout,...) or mysql_options (...,
Mysql_opt_write_timeout,...). In this case increasing the timeout may
Help solve the problem.

You have encountered a timeout on the server side and the automatic
Reconnection in the client is disabled (the reconnect flag in the MySQL
Structure is equal to 0 ).

You are using a Windows client and the server had dropped
Connection (probably because wait_timeout expired) before the command
Was issued.

The problem on Windows is that in some cases MySQL doesn' t get
Error from the OS when writing to the TCP/IP connection to the server,
But instead gets the error when trying to read the answer from
Connection.

In this case, even if the reconnect flag in the MySQL structure is
Equal to 1, MySQL does not automatically reconnect and re-issue
Query as it doesn' t know if the server did get the original query or
Not.

The solution to this is to either do a mysql_ping on the connection
If there has been a long time since the last query (this is what myodbc
Does) or set wait_timeout on the mysqld server so high that it in
Practice never times out.

You can also get these errors if you send a query to the server that
Is incorrect or too large. If mysqld has es a packet that is too
Large or out of order, it assumes that something has gone wrong
The client and closes the connection. If you need big queries (
Example, if you are working with big blob columns), you can increase
The query limit by setting the server's max_allowed_packet variable,
Which has a default value of 1 MB. You may also need to increase
Maximum packet size on the client end. More information on setting
Packet size is given in section a.1.2.9, "Packet Too large ".

An insert or replace statement that inserts a great tables rows can
Also cause these sorts of errors. Either one of these statements sends
A single request to the server irrespective of the number of rows to be
Inserted; thus, you can often avoid the error by grouping the number
Rows sent per insert or replace.

You also get a lost connection if you are sending a packet 16 MB or
Larger if your client is older than 4.0.8 and your server is 4.0.8 and
Above, or the other way around.

It is also possible to see this error if hostname lookups fail (
Example, if the DNS server on which your server or network relies goes
Down). This is because MySQL is dependent on the host system for name
Resolution, but has no way of knowing whether it is working-from
MySQL's point of view the problem is indistinguishable from any other
Network timeout.

You may also see the MySQL server has gone away error if MySQL is started with the -- skip-networking option.

Another networking issue that can cause this error occurs if
MySQL port (default 3306) is blocked by your firewall, thus preventing
Any connections at all to the MySQL server.

You can also encounter this error with applications that fork child
Processes, all of which try to use the same connection to the MySQL
Server. This can be avoided by using a separate connection for each
Child process.

You have encountered a bug where the server died while executing the query.


Based on this analysis, there may be 3 reasons:

1. the MySQL server does not match the Client Version.

2. the MySQL server configuration is defective or cannot be optimized.

3. Improve the program script

By changing multiple server and client versions, we can only partially reduce the number of errors, which cannot be completely solved. Exclude 1.

The server has been thoroughly optimized and cannot achieve the desired results. In the Value Setting of timeout, from 10 of the experience value to 60 of the default PHP value, multiple attempts were made. The official MySQL Default Value (8 hours) is obviously impossible. So as to exclude 2. (More optimization experience will be provided later)

Analyze the three program codes and find that a large number of codes similar to the following are used in the Program (for ease of understanding, use the original API description ):

$ Conn = mysql_connect (......);

............

If (! $ Conn) {// reconnect

$ Conn = mysql_connect (......);

}

Mysql_query ($ SQL, $ conn );

The meaning of this Code is consistent with the methods and ideas officially recommended by MySQL [if you have a script, you just have
Issue the query again for the client to do an automatic reconnection.
]. In actual analysis, if (! $ Conn) is not reliable. The program passes if (! $ Conn), the above error will still be returned.

Rewrite the program:

If (! Conn) {// connect ...}

Elseif (! Mysql_ping ($ conn) {// reconnect ...}

Mysql_query ($ SQL, $ conn );

As observed, the MySQL server has gone away error is basically solved.

BTW: With A Reconnect question,

In the old environment of php4x + client3x + mysql4x, The reconnet code is as follows:

$ Conn = mysql_connect (...) can work normally.

However, in the new environment of php5x + client4x + mysql4x, $ conn returned by $ conn = mysql_connect (...) is unavailable in some cases. It must be written as follows:

Mysql_close ($ conn );

$ Conn = mysql_connect (...);

The returned $ conn can be used normally. The reason is unknown. No further research has been conducted and no further discussions have been made. Maybe the official MySQL bug report will be available.

~~ Haha ~~


This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/brightsnow/archive/2009/03/17/3997705.aspx


Description:
Remember that your MySQL "max_allowed_packet" configuration setting (default 1 MB)
MySQL can process a maximum of 1 MB by default.
This problem occurs if you use large text or BLOB data in SQL. Notes on the PHP Manual

When trying to insert or update and trying to put a large amount
Text or data (BLOB) into a MySQL table you might run into problems.
In MySQL. Err you might see:
Packet Too large (73904)
To fix you just have to start up MySQL with the option-O max_allowed_packet = maxsize
You woshould just replace maxsize with the Max size you want to insert, the default is 65536

MySQL Manual

Both the client and the server have their own max_allowed_packet
Variable, so if you want to handle big packets, you must increase this
Variable both in the client and in the server.

If you are using the mysql client program, its default
Max_allowed_packet variable is 16 Mb. To set a larger value, start MySQL
Like this:

Shell> MySQL -- max_allowed_packet = 32 m that sets the packet size to 32 MB.

The server's default max_allowed_packet value is 1 MB. You can increase
This if the server needs to handle big queries (for example, if you are
Working with big blob columns). For example, to set the variable
16 MB, start the server like this:

Shell> mysqld -- max_allowed_packet = 16 m you can also use an option
File to set max_allowed_packet. For example, to set the size for
Server to 16 MB, add the following lines in an option file:

[Mysqld] max_allowed_packet = 16 m

When using MySQL for database restoration, the following error occurs due to the large data size: the MySQL server returned this
Error: MySQL error nr.2006-MySQL server has gone
Away. This error occurs when I restore a MB backup. The solution is to find the MySQL installation directory, find the my. ini file, and add
Add: max_allowed_packet = 10 m (you can also set the desired size ). Max_allowed_packet
The parameter is used to control the maximum length of its communication buffer.

Related Article

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.