【华为云MySQL技术专栏】MySQL 8.0数据字典的初始化与启动

【华为云MySQL技术专栏】MySQL 8.0数据字典的初始化与启动-1


本文主要介绍MySQL 8.0数据字典的基本概念和数据字典的初始化与启动加载的主要流程。

MySQL 8.0数据字典简介


数据字典(Data Dictionary, DD)用来存储数据库内部对象的信息,这些信息也被称为元数据(Metadata),包括schema名称、表结构、存储过程的定义等。
【华为云MySQL技术专栏】MySQL 8.0数据字典的初始化与启动-每日运维图1 MySQL 8.0之前的数据字典图片来源:MySQL 8.0 Data Dictionary: Background and Motivation
如图1所示,MySQL 8.0之前的元数据,分散存储在许多不同的位置,包括各种元数据文件,不支持事务的表和存储引擎特有的数据字典等;Server层和存储引擎层有各自的数据字典,其中一部分是重复的。
以上的设计导致支持原子的DDL变得很困难,因此MySQL 8.0之前,如果DDL过程中发生crash,后期的恢复很容易出现各种问题,导致表无法访问、复制异常等。
如图2所示,MySQL 8.0使用支持事务的InnoDB存储引擎作来存储元数据,实现数据字典的统一管理。这个改进消除了元数据存储的冗余,通过支持原子DDL,实现了DDL的crash safe。
【华为云MySQL技术专栏】MySQL 8.0数据字典的初始化与启动-每日运维图2 MySQL 8.0数据字典图片来源:MySQL 8.0: Data Dictionary Architecture and Design
数据字典表都是隐藏的,只有在debug编译模式下,可以通过设置开关set debug='+d,skip_dd_table_access_check'来直接查看数据字典表。

    mysql> set debug='+d,skip_dd_table_access_check';
    Query OK, 0 rows affected (0.01 sec)
    <br>
    mysql> SELECT name, schema_id, hidden, type FROM mysql.tables where schema_id=1 AND hidden='System';
    +------------------------------+-----------+--------+------------+
    | name | schema_id | hidden | type |
    +------------------------------+-----------+--------+------------+
    | catalogs | 1 | System | BASE TABLE |
    | character_sets | 1 | System | BASE TABLE |
    | check_constraints | 1 | System | BASE TABLE |
    | collations | 1 | System | BASE TABLE |
    | column_statistics | 1 | System | BASE TABLE |
    | column_type_elements | 1 | System | BASE TABLE |
    | columns | 1 | System | BASE TABLE |
    | dd_properties | 1 | System | BASE TABLE |
    | events | 1 | System | BASE TABLE |
    | foreign_key_column_usage | 1 | System | BASE TABLE |
    | foreign_keys | 1 | System | BASE TABLE |
    | index_column_usage | 1 | System | BASE TABLE |
    | index_partitions | 1 | System | BASE TABLE |
    | index_stats | 1 | System | BASE TABLE |
    | indexes | 1 | System | BASE TABLE |
    | innodb_ddl_log | 1 | System | BASE TABLE |
    | innodb_dynamic_metadata | 1 | System | BASE TABLE |
    | parameter_type_elements | 1 | System | BASE TABLE |
    | parameters | 1 | System | BASE TABLE |
    | resource_groups | 1 | System | BASE TABLE |
    | routines | 1 | System | BASE TABLE |
    | schemata | 1 | System | BASE TABLE |
    | st_spatial_reference_systems | 1 | System | BASE TABLE |
    | table_partition_values | 1 | System | BASE TABLE |
    | table_partitions | 1 | System | BASE TABLE |
    | table_stats | 1 | System | BASE TABLE |
    | tables | 1 | System | BASE TABLE |
    | tablespace_files | 1 | System | BASE TABLE |
    | tablespaces | 1 | System | BASE TABLE |
    | triggers | 1 | System | BASE TABLE |
    | view_routine_usage | 1 | System | BASE TABLE |
    | view_table_usage | 1 | System | BASE TABLE |
    +------------------------------+-----------+--------+------------+
    32 rows in set (0.01 sec)
    上面查询得到的表就是隐藏的数据字典表,MySQL的元数据存储在这些表中。
    在release编译模式下,如果要查看数据字典信息,只能通过INFORMATION_SCHEMA中的视图来查询。例如,可以通过视图information_schema.tables查询数据字典表mysql.tables。