mysql 系统库

发布时间:2025-12-09 20:38:18 浏览次数:4

当我们安装完mysql server时,可以查看用户和版本信息:

mysql> select version();+-----------+| version() |+-----------+| 8.0.27 |+-----------+1 row in set (0.00 sec)mysql> select user();+----------------+| user() |+----------------+| root@localhost |+----------------+1 row in set (0.00 sec)

我们查看数据库系统库可查看到如下信息。

mysql> show databases;+--------------------+| Database |+--------------------+| information_schema | -- 数据库元数据信息,保存着所有其他数据库的信息,如数据库名和表| mysql | -- 数据库构建信息,由mysql_install_db脚本初始化,存储权限的表| performance_schema | -- 数据库执行信息,在my.cnf中通过performance_schema使能进行性能监控, 5.7之后默认启用,可通过show variables like 'performance_schema'进行查看| sys | -- 数据库配置信息,主要是系统配置变量+--------------------+4 rows in set (0.01 sec)

根据版本不同,有时还会有test数据库。

我们可以用系统指令如'show table status from performance_schema;'查看这些系统数据库表情况。 我看到的表情况为:information_schema有79个表,mysql有37个表, performance_schema有110个表,sys有101个表。须知版本不同表的个数会有不同。

每个库中的表信息众多,我们仅介绍系统库中的一些关键表

information_schema中的关键表

schemata:当前mysql中所有数据库的信息tables:关于数据库中的表的信息columns:关于表中的列信息user_privileges:关于用户权限的信息

mysql中的关键表

user:用户列、权限列、安全列、资源控制列的信息db表:库级别权限表tables_priv:表级权限columns_priv:列级别全新表procs_priv:存储过程与函数权限proxies_priv:代理用户的权限event:事件与任务调度表gtid:与GTID主从复制有关的表innodb_index_stats:innodb索引统计信息innodb_table_stats:innodb表统计信息

一些常见的查询指令

统计MySQL实例下每个库占用空间总大小,并以MB为单位显示

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 | 0.00000000 || mysql | 7.40478897 || performance_schema | 0.00000000 || sys | 0.01562500 |+--------------------+-------------------------------------------------------+4 rows in set (0.07 sec)

查找出MySQL实例下非InnoDB的表

mysql> SELECT table_schema,table_name FROM information_schema.tables WHERE ENGINE !='InnoDB' AND table_schema NOT IN ('sys','performance_schema','information_schema','mysql');Empty set (0.00 sec)

统计出MySQL实例下所有用户

mysql> SELECT CONCAT(USER,'@',HOST) FROM mysql.user;+----------------------------+| CONCAT(USER,'@',HOST) |+----------------------------+| healthchecker@localhost || mysql.infoschema@localhost || mysql.session@localhost || mysql.sys@localhost || root@localhost |+----------------------------+5 rows in set (0.01 sec)

统计有全表扫描的SQL

mysql> SELECT * FROM sys.statements_with_full_table_scans LIMIT 1;+-----------------------------------+-------+------------+---------------+---------------------+--------------------------+-------------------+-----------+---------------+---------------+-------------------+----------------------------+----------------------------+------------------------------------------------------------------+| query | db | exec_count | total_latency | no_index_used_count | no_good_index_used_count | no_index_used_pct | rows_sent | rows_examined | rows_sent_avg | rows_examined_avg | first_seen | last_seen | digest |+-----------------------------------+-------+------------+---------------+---------------------+--------------------------+-------------------+-----------+---------------+---------------+-------------------+----------------------------+----------------------------+------------------------------------------------------------------+| SELECT * FROM SYSTEM_USER LIMIT ? | mysql | 1 | 372.29 us | 1 | 0 | 100 | 1 | 1 | 1 | 1 | 2021-12-30 04:10:18.010672 | 2021-12-30 04:10:18.010672 | 9d743fabf75ba82c4039c405b9ace68b72cf66e35f11bff16b9fc2ce7919012d |+-----------------------------------+-------+------------+---------------+---------------------+--------------------------+-------------------+-----------+---------------+---------------+-------------------+----------------------------+----------------------------+------------------------------------------------------------------+1 row in set (0.00 sec)

查询系统库常用的系统指令有:

常用系统命令desc tableName , describe tableName, show columns from tableName [from databaesName],show columns from databaseName.tableName --表字段信息show create table tableName --表创建语句show database databaseName --数据库创建语句show table status from databaseName --显示库中表的情况show tables [from databaseName] --显示数据库中所有表的表名show databases --显示所有数据库名称show processlist --显示系统中正在进行的所有进程,也就是正在执行的查询。show table status [from databaseName] -- 显示数据库中所有表的详情。show grants for userName@localhost -- 显示用户的授权语句show index from [databaseName.]tableName --显示表的索引show status --显示系统特定资源的信息,例如,正在运行的线程数量show errors --显示最后一个执行语句产生的错误show warngings --显示最后一个执行语句产生的错误,警告和通知。show character set --显示可用字符集信息。其他show status like 'last_query_cost' --查询上一次查询SQL执行的开销,包括IO和CPU开销
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477