The first method, of course, 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.
Everyone knows 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 to connect 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 operation 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 ($ this-> t_dbhost, $ this-> t_dbuser, $ this-> t_dbpw, $ this-> t_dbname, $ this-> t_pconnect );
}
}
The code that I need to call this function may be like this.
For ($ I = 0; $ I <10; I ++ ){
$ Str = file_get_contents ('HTTP: // www.aol.com ');
}
$ 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.
Author: "website construction technology exchange"