-- 查询哪些查询时间大于20秒 select*from information_schema.processlist where COMMAND='Query'ANDtime>20;
-- 生成kill慢查询的语句(不执行kill) select concat('KILL ',id,';') from information_schema.processlist where COMMAND='Query'ANDtime>20;
自动 kill 慢 SQL 脚本,并记录日志到文件
1 2 3 4 5 6 7 8 9 10 11
#!/bin/bash
MYSQL_CMD="mysql -uroot -pxxxxx -h 192.168.10.10" NOW=$(date +'%Y-%m-%d %H:%M:%S') SLOWLOG=$(${MYSQL_CMD} -e "select * from information_schema.processlist where COMMAND='Query' AND time > 10;" 2>/dev/null | sed '1d' )
if [ -n "${SLOWLOG}" ];then echo${NOW} echo${SLOWLOG} ${MYSQL_CMD} -e "select id from information_schema.processlist where COMMAND='Query' AND time > 10;" 2>/dev/null | sed '1d' | xargs -I {} echo"${MYSQL_CMD} -e 'kill {};'" | bash 2>/dev/null fi
SELECT TABLE_SCHEMA as DbName ,TABLE_NAME as TableName ,ENGINE as Engine FROM information_schema.TABLES WHERE ENGINE='MyISAM'AND TABLE_SCHEMA NOTIN('mysql','information_schema','performance_schema');
生成 ALTER 语句来转换到 InnoDB
1
SELECT CONCAT('ALTER TABLE ', TABLE_SCHEMA,'.',TABLE_NAME, ' ENGINE = InnoDB;') FROM information_schema.TABLES WHERE ENGINE='MyISAM'AND TABLE_SCHEMA NOTIN('mysql','information_schema','performance_schema');