Centos crond regular monitoring of MYSQL and automatically restart the service

Configuring student computers a little low, deployed on top of a number of projects, concurrent large that MYSQL is easy to kill off, so write a script to automatically monitor and hung up after the service to automatically start the MYSQL

process

以下操作都是基于root用户
  1. New directory, logging and scripts
cd /home
mkdir check
touch mysql.log
touch restart_mysql.sh
chmod 777 mysql.log restart_mysql.sh
  1. Scripting
vim restart_mysql.sh
#!/bin/bash
cur_time="`date +%y-%m-%d,%H:%M:%S`"
pgrep -x mysqld &> /dev/null

if [ $? -ne 0 ]
then
echo "At time:$cur_time MYSQL is stop">> /home/check/mysql.log
systemctl start mysqld

else
echo "MYSQL server is running..."

fi
  1. Write crond monitoring
crontabl -e //进入编写

*/2 * * * * /home/check/restart_mysql.sh

Save and exit after the execution service crond restartcan

to sum up

In the course of the second and third point above, there are several points to note

  1. Write mysql.log and the path to write the script execution restart_mysql.sh should be an absolute path, I started to write all relative path, there is no problem when the direct ./restart_mysql.sh, mysql.log can be written in, but when crond to monitor and perform, no response, and later had been turned into an absolute path can
    modify the recommendations:

    1. Use a relative path: In the beginning of the script to cd to the directory where mysql_log
    2. Using an absolute path (highly recommended): use absolute paths on the process of writing and the like, in addition, at the beginning of the script, as far as possible before the introduction of an environment variable source / etc / profile; source ~ / .bash.profile, because you added cron scheduled task after, it is not executed as root, it may be time to test you with the root of some of the commands can be used, but those added to the cron command can not be used, because the user's PATH variable is different.
  2. Restart MySQL service there can not be written service mysql start, otherwise it will prompt: Failed to start mysqld.service: Unit not founderror, the reason is no longer supported in CentOS7 mysql, even if you have installed. .
    So instead systemctl start mysqldyou can

Additional

MYSQL OOM
reduce the chance of being MYSQL kill the method:

  1. Rational planning MySQL memory usage.
  2. OOM_adj adjustment parameters, MySQL will be locked OOM_Killer lower priority.
  3. Strengthen monitoring and alarm memory, once an alarm, DBA should quickly intervene, Kill off some take up more memory connection.
Published 48 original articles · won praise 56 · views 20000 +

Guess you like

Origin blog.csdn.net/zhetmdoubeizhanyong/article/details/100181478