Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Two effective solutions to MySQL server has gone away

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

There are two effective solutions to MySQL server has gone away. In view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

The first way, of course, is to increase your wait-timeout value, which is set in my.cnf (my.ini below the Windows step down). My database load is a little bit higher, so I set the value to 10 (in seconds, which means that when a database connection does not do anything within 10 seconds, it will be forcibly closed. I do not use permanent link (mysql_pconnect), I use mysql_connect, about the effect of this wait-timeout you can see in the process list of MySQL (show processlist), you can set this wait-timeout to be larger, such as 300s, hehe, generally speaking, 300 seconds is enough, in fact, you do not have to set it, MySQL defaults to 8 hours. The situation is determined by your server and site.

The second method:

This is also what I personally think is the best way to check the link status of MySQL and make it re-link.

But we all know that there is such a function as mysql_ping. In many materials, it is said that the API of this mysql_ping will check whether the database is linked, and if it is disconnected, it will try to reconnect, but in the course of my testing, I found that this is not the case. It is conditional. We must pass the relevant parameters through the C API of mysql_options, so that MYSQL has the option to break the automatic link (MySQL defaults to not automatically connecting). But in my test, I found that the API of PHP's MySQL does not have this function, so you can re-edit the MySQL, hehe. But the mysql_ping function can finally be used, just with a small trick in it:

This is a function in my database operation class.

Function ping () {

If (! mysql_ping ($this- > link)) {

Mysql_close ($this- > link); / / Note: database shutdown must be performed first, which is the key

$this- > connect ($this- > tasking dbhost _ recording _ this-> tasking _ dbbus _ recording _ this-> tasking _ dbpw _ t_pconnect)

}

}

The code that I need to call this function might look like this

For ($iTun0umbiping (); / / after the previous web page crawls, it may cause the database connection to be closed, check and reconnect

$db- > query ('select * from table')

The ping () function first checks whether the data connection is normal, and if it is closed, the entire MYSQL instance of the current script is closed, and then reconnected.

After this treatment, the problem such as MySQL server has gone away can be solved very effectively without causing additional overhead to the system.

This is the answer to the question about the two effective solutions of MySQL server has gone away. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report