---
title: "mysql出现大量Sleep连接时，进行清理的脚本"
author: "yinxvxv"
date: "2015-12-03T10:42:32Z"
tags: []
categories: ["数据库"]
slug: "mysql-sleep"
draft: false
lastmod: "2015-12-03T10:42:20Z"
id: 2261328
url: "https://yinxvxv.iteye.com/blog/2261328"
---

#! /bin/sh  
\#  
\# clear\_sleep.sh  
\# Copyright (C) 2015 root &lt;root@db01&gt;  
\#  
\# Distributed under terms of the MIT license.  
\#

mysql -u root -p123456 -e "SET GLOBAL group\_concat\_max\_len=102400;"  
mysql -u root -p123456 -e "select  
case count(1)&gt;50  
when true  
then  
(select group\_concat(concat('KILL ',id,';') separator '') from information\_schema.processlist where Command = 'Sleep' AND user = 'root' and host!='localhost')  
else ''  
end  
from information\_schema.processlist where Command = 'Sleep' AND user = 'root' and host!='localhost'  
into outfile '/tmp/sleep\_processes.txt'"  
mysql -u root -p123456 -e "source /tmp/sleep\_processes.txt;"  
rm -rf /tmp/sleep\_processes.txt