mysql的cpu飙升原因及处理

 Mysql 批量杀死进程

正常情况下kill id,即可,但是有时候某一异常连接特别多的时候如此操作会让人抓狂,下面记录下小方法:

use information_schema;

select concat('kill ',id,';') from processlist where host like '192.168.0.1%' into outfile '/tmp/kill.sql';
        函数                                          字段        ip地址                  
source /tmp/kill.sql

 ###################################################

1.查看所有数据库容量大小

1
2
3
4
5
6
7
8
select
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)',
sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)'
from information_schema.tables
group by table_schema
order by sum(data_length) desc, sum(index_length) desc;

2.查看所有数据库各表容量大小

1
2
3
4
5
6
7
8
select
table_schema as '数据库',
table_name as '表名',
table_rows as '记录数',
truncate(data_length/1024/1024, 2) as '数据容量(MB)',
truncate(index_length/1024/1024, 2) as '索引容量(MB)'
from information_schema.tables
order by data_length desc, index_length desc;
 

3.查看指定数据库容量大小

例:查看mysql库容量大小

1
2
3
4
5
6
7
select
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)',
sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)'
from information_schema.tables
where table_schema='mysql';

  

4.查看指定数据库各表容量大小

例:查看mysql库各表容量大小

1
2
3
4
5
6
7
8
9
select
table_schema as '数据库',
table_name as '表名',
table_rows as '记录数',
truncate(data_length/1024/1024, 2) as '数据容量(MB)',
truncate(index_length/1024/1024, 2) as '索引容量(MB)'
from information_schema.tables
where table_schema='mysql'
order by data_length desc, index_length desc;

#################################################################

Mysql 备份单表数据

DROP TABLE IF EXISTS B;
CREATE TABLE B LIKE A;
INSERT INTO B SELECT * FROM A;

Mysql 统计表的行数

# mysqlshow -uroot -p  mysql -v -v
统计mysql数据库所有表的列数和行数

select count(*) from B.t1
统计B数据库t1表的行数

Mysql 插入一个字段 id自增并设置为主键

alter table t1 ADD id INT(16) NOT NULL PRIMARY KEY AUTO_INCREMENT first;
原文地址:https://www.cnblogs.com/wuhg/p/11730664.html