Solution to MySQLserverhasgoneaway

Source: Internet
Author: User
Tags mysql manual php mysql
For more information about how to solve the MySQLserverhasgoneaway problem, see.

MySQL server has gone away problem solution. For more information, see.

Applications (such as PHP) execute MYSQL statements in batches for a long time. Execute an SQL statement, but the SQL statement is too large or the statement contains BLOB or longblob fields. For example, processing image data. MySQL server has gone away is easy to cause.


In a similar situation today, MySQL is just a cold saying: MySQL server has gone away.

You probably browsed it for the following reasons:
One possibility is that the SQL statement sent is too long and exceeds the size of max_allowed_packet. For this reason, you only need to modify my. cnf and increase the value of max_allowed_packet.

There is another possibility that timeout may be caused due to some reasons. For example, Singleton is used in the program to obtain the database connection. Although the program connects to the database multiple times, it actually uses the same connection, in addition, if the interval between two database operations in the program exceeds wait_timeout (this setting can be seen in show status), a problem may occur. The simplest way to deal with it is to increase wait_timeout. Of course, you can also use mysql_ping () in the program from time to time, so that MySQL knows that it is not a battle.

Solve MySQL server has gone away

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 size you need)
The max_allowed_packet parameter is used to control the maximum length of the Communication buffer.


Recently, a website was designed to use the WEB page collector function. When a PHP script requests a URL, the requested webpage may be slow, exceeding the wait-timeout time of mysql, then, when the webpage content is captured and you are about to insert it to MySQL, you will find that the MySQL connection times out and is closed, so an error message such as "MySQL server has gone away" appears, to solve this problem, my experience may be helpful to you in the following two aspects:
Method 1:
Of course, it is to increase your wait-timeout value. This parameter is in my. cnf (my. ini), my database load is slightly larger, so the value I set is 10 (The unit of this value is seconds, this means that when a database connection does not perform any operation within 10 seconds, it will be forcibly closed. I am not using a permanent link (mysql_pconnect), and I am using mysql_connect, you can see the effect of this wait-timeout in the MySQL process list (show processlist). You can set this wait-timeout to a larger value, such as 300 seconds, generally, 300 seconds is enough. In fact, you do not need to set it. The default value of MySQL is 8 hours. The situation depends on your server and site.
Method 2:
This is also the best method I personally think, that is, to check the connection status of MySQL and reconnect it.
We may all know that there is a function like mysql_ping. In many materials, this mysql_ping API will check whether the database is connected. If it is disconnected, it will try again, however, I found that this is not the case in my test. It is conditional and must pass relevant parameters through the C API mysql_options, MYSQL has the option to disconnect the automatic connection (MySQL does not automatically connect by default), but I found that this function is not included in the PHP MySQL API during the test. Edit MySQL again, haha. However, the mysql_ping function can still be used at last, but there is a small operation skill in it:
This is a function in the middle of my database operations class.
The Code is as follows:
Function ping (){
If (! Mysql_ping ($ this-> link )){
Mysql_close ($ this-> link); // Note: you must first disable the database. this is the key.
$ This-> connect ();
}
}

The code that I need to call this function may be like this.
The Code is as follows:
$ Str = file_get_contents ('HTTP: // www.jb51.net ');
$ Db-> ping (); // after the previous web page is crawled, the database connection may be closed, checked, and reconnected.
$ Db-> query ('select * from table ');

Ping () This function first checks whether the data connection is normal. If it is closed, it closes the MYSQL instance of the current script and then reconnects.
After such processing, it can effectively solve problems such as MySQL server has gone away without causing additional overhead to the system.
__________________________________________________________________________________________________
In a similar situation today, MySQL is just a cold saying: MySQL server has gone away.
You probably browsed it for the following reasons:
One possibility is that the SQL statement sent is too long and exceeds the size of max_allowed_packet. For this reason, you only need to modify my. cnf and increase the value of max_allowed_packet.
There is another possibility that timeout may be caused due to some reasons. For example, Singleton is used in the program to obtain the database connection. Although the program connects to the database multiple times, it actually uses the same connection, in addition, if the interval between two database operations in the program exceeds wait_timeout (this setting can be seen in show status), a problem may occur. The simplest way to deal with it is to increase wait_timeout. Of course, you can also use mysql_ping () in the program from time to time, so that MySQL knows that it is not a battle.
Solve MySQL server has gone away
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, google or read 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 size you need)
The max_allowed_packet parameter is used to control the maximum length of the Communication buffer.
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 file:


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:


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 file:
(You can also set the desired size)

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

------------The following are network search materials-------------------


Maybe other people may encounter this problem, not necessarily because of this, so I will find a more comprehensive analysis on the Internet below:
There are two articles: the first one is more intuitive and the second one is more profound.
Solve MySQL server has gone away 16:23:22
From: http://www.webjx.com/database/mysql-8817.html
In a similar situation today, MySQL is just a cold saying: MySQL server has gone away.
You probably browsed it for the following reasons:
One possibility is that the SQL statement sent is too long and exceeds the size of max_allowed_packet. For this reason, you only need to modify my. cnf and increase the value of max_allowed_packet.
There is another possibility that timeout may be caused due to some reasons. For example, Singleton is used in the program to obtain the database connection. Although the program connects to the database multiple times, it actually uses the same connection, in addition, if the interval between two database operations in the program exceeds wait_timeout (this setting can be seen in show status), a problem may occur. The simplest way to deal with it is to increase wait_timeout. Of course, you can also use mysql_ping () in the program from time to time, so that MySQL knows that it is not a battle.
Solve MySQL server has gone away

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 file:


(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 shoshould be corrected.

A client application running on a different host does not have the 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 the connection (probably because wait_timeout expired) before the command was issued.

The problem on Windows is that in some cases MySQL doesn't get an 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 the 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 the 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 with the client and closes the connection. if you need big queries (for 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 the maximum packet size on the client end. more information on setting the 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 of 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 (for 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 the 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 to 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 of 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 to 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 the 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 max_allowed_packet = 10 M at the end of the file (you can also set the desired size ). The max_allowed_packet parameter is used to control the maximum length of the Communication buffer.

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.