简体   繁体   中英

Sporadic MySQL connection errors for 5-10 seconds

I have a small portal website, with 7-8000 visitors/day, I do all the SQL queries and coding.

I find that sometimes the website can't connect to the database - instead for 5-10 seconds it shows the message 'Cannot connect to mysql...' . This is mysql_connect error , after 15 second everything returns to normal for a few hours. No login change, no hosting problems. I put mysql_close() on the footer of website, but the issue still occurs.

What could be the cause of this kind of error? Where should I search to find the problems and solve them? Could it be to many connection too the page?

I can only provide you a few general tips but I hope they help anyway:

  • You say you get cannot connect to mysql... and nothing else. That sounds like you have some code at "database_conection.php" that explicitly prints such message on connection error. PHP default errors tend to be more verbose and include error codes, file names, line number...

  • Wherever you call mysql_connect() you can enhance error handling quite easily:

    • Test the return value of the function.
    • On error, call mysql_error() to obtain the exact error message will all the details.
    • Log all the details you need to identify the piece of code that triggered the error. For instance, debug_backtrace() can tell you the precise function call chain.
  • PHP offers several error handling directives to fine tune what to do on error. Have a look at display_errors , log_errors and error_reporting .

  • I'm not sure about how you expect mysql_close() to help but this function requires a working connection and it simply closes it. Furthermore, I suppose your footer is close to the end of the script, where the connection will be closed automatically anyway.

  • If you are using a shared hosting account your site will not be the only user of the MySQL server. If it's simply timing-out due to high load it doesn't need to be your site's fault necessarily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM