1information_schema就是一个全局虚拟库,库内有很多视图,在mysql启动时自动生成:
mysql> show databases;
+------------------------+
| Database |
+------------------------+
| information_schema |
| #mysql50#20210113-1817 |
| mysql |
| performance_schema |
| sys |
| tangTest |
| testku |
+------------------------+
7 rows in set (0.02 sec)
2视图可以创建,我们可以将某个复杂的语句定义为一个视图,
1定义视图别名
create view test<100;
2执行查询语句:
select * from test;
3这个information_schema库中有很多的表,其中有一个TABLES表,可以desc tables看一下这个表结构
mysql> use information_schema;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+---------------------------------------+
| Tables_in_information_schema |
+---------------------------------------+
| CHARACTER_SETS |
| TABLES |
| COLLATION_CHARACTER_SET_APPLICABILITY |
tables表结构里有很多方法,以下为常用方法:
mysql> desc tables;
+-----------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------------+------+-----+---------+-------+
| TABLE_SCHEMA | 表所在的库 | NO | | | |
| TABLE_NAME | 表名 | NO | | | |
| ENGINE | 表的存储引擎 | YES | | NULL | |
| TABLE_ROWS | 表的行数 | YES | | NULL | |
| AVG_ROW_LENGTH | 平均行长度 | YES | | NULL | |
| INDEX_LENGTH | 索引长度 | YES | | NULL | |
实例1:
查询所有库对应的所有的表名:
mysql> select table_schema,table_name from information_schema.tables;
+--------------------+------------------------------------------------------+
| table_schema | table_name |
+--------------------+------------------------------------------------------+
| information_schema | CHARACTER_SETS |
| information_schema | COLLATIONS |
| testku | course |
| testku | tanglinux |
| testku | tanglinuxcopy |
+--------------------+------------------------------------------------------+
查询testku和tangTest这两个库的所有表名:
mysql> select table_schema,table_name
-> from information_schema.tables
-> where table_schema='testku'
-> union all
-> select table_schema,table_name
-> from information_schema.tables
-> where table_schema='tangTest';
+--------------+---------------+
| table_schema | table_name |
+--------------+---------------+
| testku | course |
| testku | tanglinux |
| testku | tanglinuxcopy |
| tangTest | AAA |
+--------------+---------------+
4 rows in set (0.09 sec)
mysql>
以库名进行分组,让表在一行中显示:
mysql> select table_schema,group_concat(table_name)
-> from information_schema.tables
-> where table_schema='testku'
-> group by table_schema;
+--------------+--------------------------------+
| table_schema | group_concat(table_name) |
+--------------+--------------------------------+
| testku | course,tanglinux,tanglinuxcopy |
+--------------+--------------------------------+
1 row in set (0.00 sec)
mysql>
2统计一下每个库的表的个数:
mysql> select table_schema,count(table_name)
-> from information_schema.tables
-> group by table_schema;
+--------------------+-------------------+
| table_schema | count(table_name) |
+--------------------+-------------------+
| information_schema | 61 |
| mysql | 32 |
| performance_schema | 87 |
| sys | 101 |
| tangTest | 1 |
| testku | 3 |
+--------------------+-------------------+
6 rows in set (0.13 sec)
例3:
统计一下每个库的真实数据量(单位转换为M):
每张表的数据量=平均行长度 * 表的行数 + 索引的长度
每张表的数据量=avg_row_length * table_rows + 索引长度
mysql> select table_schema,sum(avg_row_length*table_rows+index_length)/1024/1024
-> from information_schema.tables
-> group by table_schema;
+--------------------+-------------------------------------------------------+
| table_schema | sum(avg_row_length*table_rows+index_length)/1024/1024 |
+--------------------+-------------------------------------------------------+
| information_schema | NULL |
| mysql | 2.25408268 |
| performance_schema | 0.00000000 |
| sys | 0.01562119 |
| tangTest | 0.00000000 |
| testku | 0.09374905 |
+--------------------+-------------------------------------------------------+
6 rows in set (0.02 sec)
生产实例演示:
对所有数据库里的每一张表进行单独备份
如果我们备份某个库里的一张表,我们可以这样写:mysqldump -uroot -p123456 world city >/tmp/world_city.sql
如果我们要利用元数据information_schema实现上面的语句就可以这样写:
mysqld -uroot -p123456 table_schema table_name >/tmp/table_schema_table_name.sql
from information_schema
上面的语句实现了查询所有的库对应所有的表,但我们需要把每张表的备份语句拼接出来,所以就用到了拼接语句concat():
concat("mysqldump -uroot -p123456 ",table_schema," ",table_name," >/tmp/",table_schema,"_",table_name,".sql")
查询出所有库对应表的拼接语句
select concat("mysqldump -uroot -p123456 ",table_schema," ",table_name," >/tmp/",table_schema,"_",table_name,".sql")
from information_schema.tables
where table_schema not in('sys','performance','information_schema')
into outfile '/tmp/bak.sh';
查看所有使用innodb引擎的表:
mysql> select table_schema,table_name,engine from information_schema.tables where engine='innodb';