Database connect timeout issue

I have a website I built from scratch some years ago using html and php, and with a MySQL database. As of two or three months ago, intermittently when a page is loaded, it hangs up for 60 seconds and then reports a database connection timeout. This is a fatal error where the page exits, but if I reload the page manually, it loads again, usually successfully. This problem behavior occurs somewhere between the 2nd and 20th times that any script using the database is loaded. It makes the site very difficult to use.

The hosting company has been very helpful. For example, they told me the site is not exceeding the allowable loading limits for shared hosting. They even transferred the database to a different MySQL server, which indicates the problem is not in that server. And they have adjusted parameters in the php.ini file to allow scripts plenty of freedom.

I’ve been watching the apache error log. At first I noticed numerous crawlers interrogating the site, and they also were experiencing the database connection timeout. I subsequently eliminated most crawlers by either disallowing them in the robots.txt file, or denying them access via the .htaccess file.

The site is now very quiet. So I wrote a simple php script (no CSS, etc.) that does nothing except connect to the database and then immediately close the connection. However, when I reload it repeatedly, it intermittently triggers the connection timeout, similar to what I’ve described.

I believe the 60 seconds timeout is the default for MySQL connect Of course, it should not need 60 seconds. And generally there is nothing else going on than my reloading the test script every few seconds.

I would much appreciate any thoughts about this issue. For example, might it be that there is a virus consuming the MySQL bandwidth? (Of course, one might then ask why the virus script itself doesn’t experience the timeout issue.)

I have two other similarly constructed websites at the same hosting company that do not exhibit this problem.

Thanks for any ideas about what might be causing this. Or how best to go about locating the cause.

What version of PHP is your site using? If the site was written a long time ago it is probably using an outdated version that is no longer supported. It could be part of the issue.

Do you have a local copy of the site that you can run in a local development environment?
That may help diagnose any problem not related to the host server.

Yes, that’s a good point. It’s true that the site originated over 10 years ago, but the hosting company has been diligent in making sure customers don’t get behind. The site was upgraded to PHP 8.1 in April.

I was becoming concerned that the site may have a virus, and I was preparing to take down the entire site and rebuild it with files that I knew were uninfected, but fortunately that unpleasant exercise was not necessary. This morning the hosting company’s tech support people, with whom I’ve been corresponding for two or three weeks, responded that they had escalated the issue to a higher group of technical people, who were able to replicate the problem and subsequently fix something in their server. So now I can work on restoring the site fully.

Thank you for your response. I’m grateful to have forums like this one where one can talk about the problem and get inspiration leading to solutions.


Thank you for your reply. I do in fact have a separate development system based on the XAMPP apache server, running PHP 8.2 on a Windows 11 system, which is immensely helpful with this kind of issue, and where the problem does not occur. I also had run a test script on two other sites hosted by the same company, without issue. This was making me think that the problem site may be impacted by a virus, although it would need to be a very clever one.

As I reported in my reply to the previous responder, the hosting company’s tech support people, after appearing to have done all they could, responded in the early hours of this morning that they had traced the problem to something in their server, which they have now fixed.

Thank you for your response. It confirmed that I was thinking about the problem correctly, and that is significant.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.