I frequently see WordPress blogs encounter mysql connection lost or exceeded user connections limit error when I’m browsing their websites. This problem normally happens on a WordPress blog that hosts on a shared hosting, especially those busy server where too many users sit on the same server.
I have modified the WordPress’s DB class to put in some enhancements to reduce the occurrence of the error.
- Retry to open mysql connection when the connection is failed to open at the first time.
- Reopen the connection during failure (on connection lost error) when calling mysql_query() (for SELECT statements only).
- Currently the code will retry for 5 times and interval for next retry is 200 milliseconds.
To modify the number of retries and interval, you simply need to modify the following lines of code:
$this->retry_max = 5; // Maximum retry 5 times
$this->retry_interval = 200; // 200 miliseconds
I have installed this script on my blog and so far don’t see any problem. If you are interested to have this code, you can download it from here:
Do let me know if you found any bug in the code.
Notes:
- Use this script at your own risk!
- The WordPress DB class is from WordPress version 2.2.1. I’m not sure if it is compatible with the older versions.
- Remember to backup your original source file.
It is not recommended to tweak the total retry time to more than 5 seconds (current setting is 5 x 200ms = 1 second). If you still owest getting the connection lost or exceeded maximum user connections limit (User xxx already has more than ‘max_user_connections’ active connections) error, you might need to ask you web hosting company to change the connection limit or consider to host your website on a better server.
Leave a Reply