4.5.3 视图项目一
1. information_schema.tables应用案例-1
背景: 历史遗留问题,几十张表是MyISAM引擎.
问题: 业务稍微忙一些的时候,导致业务网站卡住.断电情况下,会有部分数据(索引)损坏.主从1年多没同步了.
分析问题:
a. 确认版本5.5版本
select version();
b. 确认业务表的引擎
select table_schema,table_name,engine from information_schema.tables
where table_schema not in ('mysql','information_schema');
# 查询业务库下的非InnoDB的表
select table_schema,table_name,engine from information_schema.tables
where table_schema not in ('mysql','information_schema') and engine !='innodb';
# 确认是否是从低版本升级过来.
c. 监控锁的情况
show status like '%lock%'
发现有很多table_lock信息.
d. 检查主从状态
show slave status \G
处理方案:
a. 将所有非InnoDB表,查出来.
select table_schema,table_name,engine from information_schema.tables
where table_schema not in ('mysql','information_schema') and engine !='innodb';
mysql> select table_schema,table_name,engine from information_schema.tables where table_schema not in ('mysql','information_schema','performance_schema','sys') and engine !='innodb';
b. 将所有非InnoDB表,替换成InnoDB
SELECT CONCAT("ALTER TABLE ",table_schema,".",table_name," ENGINE=INNODB;")
FROM information_schema.tables
WHERE table_schema
NOT IN ('mysql','information_schema','performance_schema','sys')
AND ENGINE !='innodb'
INTO OUTFILE '/tmp/alter.sql';
视图项目二
information_schema.processlist 应用案例-2
zabbix 普鲁米修斯等添加监控项
a. 监控业务用户登录的次数
mysql> select user,count(*) from information_schema.processlist where user not in ('root','event_scheduler') gropup by user;
b. 监控sleep的连接个数
mysql> select COMMAND,count(*) from information_schema.processlist where COMMAND='sleep';
c. 监控业务用户的登录总时长
mysql> select user,sum(TIME) from information_schema.processlist where user not in ('root','event_scheduler') group by user;
d. 监控业务用户的登录平均时长
mysql> select user,AVG(TIME) from information_schema.processlist where user not in ('root','event_scheduler') group by user;