Home > Database > Mysql Tutorial > 使用Linux的Shell脚本定时处理MySQL超时_MySQL

使用Linux的Shell脚本定时处理MySQL超时_MySQL

WBOY
Release: 2016-06-01 13:25:51
Original
839 people have browsed it

bitsCN.com 最近一段时间,我刚刚进入一家新公司,并接手了这里的一个站点,由于这个站点的架构设计不太合理,导致MySQL的压力始终很大,经常出现超时的Locked进程,于是编写了一段Linux的Shell脚本来定时kill掉这些进程。
脚本如下:

#!/bin/bash
mysql_pwd="xxxxxx" #mysql的root密码
mysql_exec="/usr/local/mysql/bin/mysql"
tmp_dir="/tmp"
file_sh="$tmp_dir/mysql_kill_locked.sh"
file_tmp="$tmp_dir/mysql_kill_locked.tmp"
file_log="$tmp_dir/mysql_kill_locked.log" #日志
$mysql_exec -uroot -p$mysql_pwd -e "show processlist" | grep -i "Locked" > $file_tmp
cat $file_tmp >> $file_log
for line in `cat $file_tmp | awk '{print $1}'`
do
echo "$mysql_exec -uroot -p$mysql_pwd -e "kill $line"" >> $file_sh
done
chmod +x $file_sh
sh $file_sh #执行临时脚本
> $file_sh #清空临时脚本
 
最后,将这段脚本加入到crontab,定时执行即可。
bitsCN.com

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template