Application Program (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. CopyCode 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.Copy codeThe 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:
InMy. iniAdd or modify the following two variables in the 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
InMy. CNFAdd or modify the following variables in the file:
Max_allowed_packet =10 m(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.
I browsed it for the following reasons:
One possibility is that the SQL statement sent exceeds the size of max_allowed_packet. If so, 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:
InMy. CNFAdd or modify the following variables in the 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 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.