开发者

How to monitor and automatically restart mysql?

开发者 https://www.devze.com 2023-02-06 18:21 出处:网络
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.

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 re开发者_如何学JAVAstarted


You can write a cronjob to use

show processlist;

show processlist will return column Time and Id,
you can add more logic to check,
like query stuck for more than 600 seconds and the query is SELECT,
you can use Id value to perform kill $id;

This is safer than blindly restarting your server.

And if you have segregate between read/write (meaning read only SQL will use user with read privileges only), this can even simpler.


Use this bash script to check every minute.

#!/bin/bash

#Checking whether MySQL is alive or not
if mysqladmin ping | grep "alive"; then
   echo "MySQL is up"
else
   sudo service mysql restart
fi

`

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号