Home > Database > Mysql Tutorial > 自动清理MYSQL的Lock进程的脚本_MySQL

自动清理MYSQL的Lock进程的脚本_MySQL

WBOY
Release: 2016-06-01 13:40:50
Original
1052 people have browsed it

bitsCN.com
自动清理MYSQL的Lock进程的脚本 最近数据库服务器在某一时段进程数忽然暴增,导致数据库服务器死掉,根据日志查询,是由于Mysql大量Lock进程造成服务器进程数过高导致。 遂写了一个脚本,每分钟检查下,如果存在负载过高的情况,就杀掉一些Lock 进程。 以下脚本稍微改下,也可以完成其他的任务。 服务器负载高比较危险,可能会导致整个服务宕掉。虽然杀掉一些进程对某些用户访问可能会有一些异常,但总比服务器宕机要好。 cat killmysqlprocess.sh -------------------------------------------------------------------------------------------------------------   #!/bin/bash #*/1 * * * * /usr/local/src/killmysqlprocess.sh backdir="/usr/local/logs/"logfile="cms_mysql_killprocess_$(date +"%Y%m%d").log"mysqlbindir="/usr/local/mysql/bin/" #use commond 'vmstat' get proc num  pronum=`vmstat | awk -F" " '{if (NR==3) print $1}' vmstat.tmp` #if pronum less 20, nothing to do.......... exit if [ $pronum -lt 20 ]; then    exitfi #it's here more than 20,run kill mysql Lock process ${mysqlbindir}mysql -e "show full processlist" > processlist.tmp #get Lock mysql process idawk -F" " '/Locked/{print $1}' processlist.tmp >looplock.tmp sleep 5   for line in `cat looplock.tmp`do  /usr/local/mysql/bin/mysql -e "kill ${line}"done rm -f looplock.tmp #write kill log echo $(date +"%Y-%m-%d %H:%M:%S") >> ${backdir}${logfile}cat processlist.tmp >> ${backdir}${logfile} rm -f processlist.tmp
  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