Recently, I just entered a new company and took over a site here. The architecture design of this site is not reasonable, so MySQL is always under great pressure, the Locked process often times out, so a Linux Shell script is compiled to kill these processes at regular intervals.
The script is as follows:
Copy codeThe Code is as follows:
#! /Bin/bash
Mysql_pwd = "xxxxxx" # mysql root Password
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" # 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 # execute a temporary script
> $ File_sh # Clear the temporary script
Finally, add the script to crontab and execute it regularly.