Reputation: 417
I have hosted on of my web application on Rackspace server. It is having DB on its localhost.
I don't know why my DB shutdown unexpectedly and on my production server it displays Error establishing DB connection.
When I restart the mysql server it works fine. service mysql restart
.
Can we trigger a mail whenever our mysql server is down ?
Where I need to do the setting.
Should I write a function that check DB connection for every 5section and if not okay it will trigger a mail?
Any other best solution?
Server Configuration :
OS : Ubuntu 14.04
RAM : 8GB
PHP version : 5.3.10
MYSQL version : 5.5.40
Upvotes: 2
Views: 2450
Reputation: 42915
What you describe is called a "watchdog".
Yes, it is typically implemented by polling the service to be monitored on a regular base, but a lower frequency should be fine. You are not interested in a few seconds here. You could use a cron
job for the purpose.
Polling does not necessarily mean you have to implement a fully fledged database connection. Maybe a socket test is enough to confirm that the server is still up and responding. That would require much less resources, but obviously the resulting statement is limited too.
Another and much more powerful approach is to use a monitoring solution like nagios
or zabbix
which allow to monitor arbitrary services, systems, things, visualize the result over time and send various forms of alarms and triggers.
A third alternative for your specific situation is to implement an error handler for your application that sends the message. This is well possible, since a failing database connection is not a fatal issue, so your script continues to be executed.
My personal suggestion is to start using a monitoring solution if you act in a professional environment, if you do business online.
Upvotes: 4
Reputation: 2254
The answers above are very OK.
You could also consider uptimerobot.com or pingdom.com or any other website monitoring solutions. I personally use both of these two. I have a special url which tests db, external apis, redis and few more things and this url is called by uptime robot.
These tools will not only let you know that something is wrong, but also you may get an overall statistics about downtime(s).
They also offer alternative ways of informing you about the problem, to mention an SMS.
Upvotes: 0
Reputation: 7080
You can use pm2-mysql
pm2 install pm2-mysql
And connect it via https://keymetrics.io/ there you will be notified if it goes down
Upvotes: 0
Reputation: 1870
Try using cronjobs create a php page in which create connection to database if connection is successful then its ok. If connection is unsuccessful send email.
if(! $conn ) {
//die('Could not connect: ' . mysql_error());
// send email
}
Upvotes: -1