元数据的获取
1.元数据包含什么?
数据字典信息(表属性、列、列属性)、状态、系统参数、权限等。
ibdata1 、 frm 、 mysql库(权限表、状态表、统计信息) 、 P_S、SYS表
- 查询换数据方法
show 语句
help show
show databases;
show tables [from DB];
show create database world;
show craete table world.city;
show full processlist;
show engines;
show charset;
show collation;
show variables [like '%%']
show status [like '%%']
show grants for
SHOW OPEN TABLES
SHOW INDEX FROM tbl_name
SHOW MASTER STATUS
SHOW BINLOG EVENTS
SHOW RELAYLOG EVENTS
SHOW SLAVE STATUS
SHOW SLAVE HOSTS
information_schema 视图库
每次数据库启动,自动在内存中生成的“虚拟表”(视图)。
保存了各种常用元数据查询方法的视图,只能查询不能修改和删除。
-- TABLES 使用
-- 1. 结构介绍
作用:存储了整个MySQL中所有表相关属性信息
desc tables;
TABLE_SCHEMA : 所在库
TABLE_NAME : 表名
ENGINE : 存储引擎
TABLE_ROWS : 数据行
AVG_ROW_LENGTH : 平均行长度
INDEX_LENGTH : 索引的长度
DATA_FREE : 碎片的情况
-- 2. 应用案例
-- 例子1: 统计MySQL所有业务库:库名、表个数、表名
select table_schema,count(*),group_concat(table_name)
from information_schema.tables
where table_schema not in ('mysql','sys','information_schema','performance_schema')
group by table_schema ;
-- 例子2: 统计MySQL所有业务库:库名、数据总量(单张表:
TABLE_ROWS*AVG_ROW_LENGTH+INDEX_LENGTH)
select table_schema,sum(TABLE_ROWS*AVG_ROW_LENGTH+INDEX_LENGTH)/1024/1024 as sum_mb
from information_schema.tables
where table_schema not in ('mysql','sys','information_schema','performance_schema')
group by table_schema ;
-- 例子3:
生产案例:客户MySQL系统 经历的很多个版本 5.1 --》 5.5 ---》 5.6。。。
系统中有2000-3000张表,其中有myisam、innodb两种存储引擎类型。
需求1: 查找业务库中,所有非InnoDB表
select table_schema,table_name,engine
from information_schema.tables
where table_schema not in ('mysql','sys','information_schema','performance_schema') and engine !='innodb';
需求2: 将所有这些表备份走
mysqldump -uroot -p123 test t1 >/data/test_t1.sql
select concat("mysqldump -uroot -p123 ",table_schema," ",table_name," >/data/",table_schema,"_",table_name,".sql")
from information_schema.tables
where table_schema not in ('mysql','sys','information_schema','performance_schema') and engine !='innodb'
into outfile '/tmp/dump.sh';
需求3: 将以上表替换为InnoDB引擎
alter table test.t1 engine=innodb;
select concat("alter table ",table_schema,".",table_name," engine=innodb;")
from information_schema.tables
where table_schema not in ('mysql','sys','information_schema','performance_schema') and engine !='innodb'
into outfile '/tmp/alter.sql';
[root@db01 ~]# sh /tmp/dump.sh
[root@db01 ~]# cd /data/
[root@db01 data]# ll
-rw-r--r-- 1 root root 1741 Jul 2 18:30 test_t1.sql
-rw-r--r-- 1 root root 1741 Jul 2 18:30 world_t2.sql
[root@db01 data]# mysql -uroot -p123 </tmp/alter.sql
mysql> select table_schema,table_name,engine from information_schema.tables where table_schema not in ('mysql','sys','information_schema','performance_schema') and engine !='innodb';
Empty set (0.00 sec)
网友评论