redstang423
redstang423

Reputation: 133

Handling and Notification of Failed DB Connection in PHP

My web host is currently having an issue with one of the databases on my website, and I'm getting an error message when a connection is attempted. I happened to be on my site, so I learned about it nearly instantly, but now I'm wondering what the best way is to get some sort of notification for this type of issue in the future. My first instinct was to do an (abbreviated) code block like this:

try {  
  $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);  
  $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);  
}  
catch(PDOException $e) {  
 $msg = $e->getMessage();
 mail($to,$subject,$msg,$headers); 
}  

But then I quickly saw the flaw was an email every single time my page was hit. I had a brief thought about entering the error in a database, but if there's a database connection issue, that doesn't seem like it would be particularly useful method of learning about the error.

Any other thoughts on how I can handle this scenario? Real time notification is preferred, but at notification beyond me accessing the website will work better than what I have now.

Upvotes: 1

Views: 146

Answers (4)

redstang423
redstang423

Reputation: 133

The suggestions in other comments struck an idea for me that I've now implemented on my site. I simply created a cron job that attempts to connect to the databases, and if that has an error, the cron job script sends me an email. Since I have the job running every thirty minutes, it won't overwhelm me with excessive notifications. It obviously isn't instantaneous, but works well for my needs on the site.

Upvotes: 0

Max Alexander Hanna
Max Alexander Hanna

Reputation: 3871

Have a file you read to and write to on the server. With php: Flag downed databases with code that checks first if the database is flagged in the file. So in the catch block you could scan the file in question for the databases name. If it isnt flagged already and it is down then send an email. Also, write the database name in the file. When the server restarts an admin should clear the flag.

Upvotes: 0

Douglas Phillips
Douglas Phillips

Reputation: 116

The easiest solution is a canary / status page that is checked by a site uptime monitor, which can alert you. If the page returns an error, you get a text or email.

There are of course more complex routes to take but they'd be overkill for what you're needing.

Upvotes: 1

Junaid
Junaid

Reputation: 1300

How about creating a file as your flag? Something like this

try {  
    $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
}
catch(PDOException $e) {
    $flag_file = 'db_error_flag.txt';
    if ( !file_exists( $flag_file ) ) {
        $msg = $e->getMessage();
        mail($to,$subject,$msg,$headers);

        $file = fopen( $flag_file, 'w' );
        fwrite( $file, $msg );
        fclose( $file );
    }
}

Upvotes: 0

Related Questions