I use the following to see if MySQL is responding. If not then check to see if the timestamp of a text file is less than 1 minute ago; if so then I restart MySQL, then touch the file to update the timestamp.
$dbh = @mysqli_connect('localhost', $user, $pass, $database);
if (!$dbh) {
if (time() - filemtime('/home/account/data/mysql_restart.dat') > 60) {
mail('me@gmail.com',
'MySQL Restarted',
mysqli_connect_error() . "\n\n" .
'IP: ' . $_SERVER['REMOTE_ADDR']);
exec('/etc/rc.d/init.d/mysql restart');
touch('/home/account/data/mysql_restart.dat');
}
exit;
}
At 3:52am, though, I received 52 emails! So it appears that
time() - filemtime(...) > 60 didn't work as expected. The $_SERVER['REMOTE_ADDR'] for each of them was different so it doesn't appear to be an issue of browser caching.
I tested the timestamp of the file using
date('YmdHis', filemtime(...)), and the actual timestamp is 035219. So it appears that those 52 emails were sent in less than 19s.
Considering we're talking about 3:52am, I'm guessing that these are bots that are hitting faster than the system can modify the text file.
Any suggestions on what I can do to prevent the system from trying to restart MySQL 52 times in 19 seconds?