Method 1:
Solve the Problem of MySQL server has gone away. Of course, it is to increase your wait-timeout value. This parameter is in my. cnf (my. ini), my database tutorial load is slightly larger, so I set the value to 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 tutorial _ 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.
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 its communication buffer zone.
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.
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.
$ Str = file_get_contents ('HTTP: // www.tianqiyugao.net ');
$ Db-> ping (); // after the previous web page is crawled, the database connection may be closed, checked, and reconnected.
$ Db-> query ('select * from table ');