Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm running mysql on Debian.

Is there a way to monitor mysql and restart it automatically if it locks up? For example sometimes the server starts to take 100% of cpu and starts running very slowly. If I restart mysql, things clear up and the server starts working fine, but I'm not always present to restart it manually.

Is there a way to monitor mysql and if the cpu is above 95% for more than 10 minutes straight then then mysql will automatically be restarted

share|improve this question
Apart from any answer recommending solutions to the issue of restarting the MySQL server, I strongly recommend you to investigate where those 100% CPU load spikes come from. That's the "real problem", and restarting the server will, IMVHO, never be a permanent and proper solution. –  Francisco Jan 22 '11 at 22:58
Indeed, that's the tension between 'get it fixed now' and 'get it fixed right'. –  Phil Hollenback Jan 28 '11 at 22:14
For future wanderers: my advice in this situation is to login while it's acting up (if you can) and run show full processlist; that will tell you what the server is doing at the moment. –  Eli Jun 10 '14 at 22:59

3 Answers 3

up vote 2 down vote accepted

You can do this with monit. For example, to alert & restart mysql, assuming you run monit on a 60 second cycle:

check process mysqld
  with pidfile /var/run/
if cpu usage > 99% for 10 cycles then alert
if cpu usage > 99% for 10 cycles then restart

monit is very flexible and can do pretty much any sort of monitoring of processes, memory, etc. you can think of.

This could probably be done with ps-watcher too, but it's hard to make ps-watcher remember this sort of state and act on it. monit is the right tool to use.

share|improve this answer
mgsqld? is it mysqld? –  naxa Sep 6 at 16:07

I belive you're looking for and the Auto Nice Daemon.

share|improve this answer
No, that doesn't solve the problem. If the process locks up, reducing it's priority won't help. –  Sven Jan 22 '11 at 23:18

Seconding Francisco, I'd advise not to restart MySQL this way. Because:

  1. CPU spike indicate activity, which means that restarting it would pretty much can whatever operation the DB is doing.
  2. Using a CPU up to 100% is not a bad thing, this is what the computing power of it is for :-) In fact, you could rather look at the load of the server more than the CPU usage in % that doesn't give any indication whatsoever about something going wrong.
  3. Depending on which engine you're using, failed transaction might not be replayed upon restart, which means you'll lose some of them.

With that being said, some things you can look for:

  1. Is MySQL "well" configured for the server; as in, does it have enough room for the size of the database and the operations it runs?
  2. Are there any other other services that cause this load, if so, why is that?
  3. Finally, if that "slows" down the running of other services on the server, you should consider rebalancing the resources per service, so that the computing capacity of the server is shared amongst all services.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.