Skip to content

MySQL主要的文件

1. 配置文件

  1. 最早my.cnf文件是放在/etc/my.cnf位置,也可以是my.ini, MySQL支持多个配置文件配置,可以执行下面命令查看配置文件有哪些:
sh
[jack@hadoop104 ~]$ mysql --help |grep my.cnf
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf /opt/module/mysql-8.0.39/my.cnf ~/.my.cnf
  1. 不同文件里面配置的相同变量会按照优先级顺序覆盖替换

1.1 配置文件内容组成

在my.cnf文件中,可以使用include指令指定文件将其内容合并到当前my.cnf文件中。my.cnf文件一共分为6大部分:

  • [client]: mysql客户端配置连接配置。
  • [mysql]: mysql命令行的相关配置。
  • [mysqld]: mysql服务的相关配置。
  • [mysqldump]: 用于配置mysqldump备份工具的参数。
  • [mysqladmin]: 配置mysqladmin管理工具的参数。
  • [mysqld_multi]: 配置m多实例管理的参数。
  • [mysqld-x]: mysql特定版本配置信息,在启动符合版本的MySQL会配置生效。
  • [mysqldx]: 多实例节点的配置信息。

2. 表结构定义文件

在MySQL5.7之前,存储MySQL数据的文件目录中,表结构文件存储为xxx.frm。 Alt text 在MySQL8之后,在innoDB引擎的每张表都会对应xxx.ibd这样一个表空间文件,存储该表的表结构、数据、索引信息,通过变量innodb_file_per_table控制:

sql
show variables like 'innodb_file_per_table';

如果该参数开启后,每一张表都对应一个ibd文件。 Alt text ibd文件是基于二进制存储的,不能直接基于记事本打开,我们可以使用mysql提供的指令ibd2sdi, 通过该指令就可以从ibd文件中提取sdi信息,而sdi数据字典信息中就包含该表的表结构, 如下返回json格式数据:

sh
[root@hadoop104 mysql-8.0.39]# ./bin/ibd2sdi data1/test/product.ibd
["ibd2sdi"
,
{
        "type": 1,
        "id": 644,
        "object":
                {
    "mysqld_version_id": 80039,
    "dd_version": 80023,
    "sdi_version": 80019,
    "dd_object_type": "Table",
    "dd_object": {
        "name": "product",
        "mysql_version_id": 80039,
        "created": 20250406092319,
        "last_altered": 20250406092319,
        "hidden": 1,
        "options": "avg_row_length=0;encrypt_type=N;key_block_size=0;keys_disabled=0;pack_record=0;stats_auto_recalc=0;stats_sample_pages=0;",
        "columns": [
            {
                "name": "category",
                "type": 4,
                "is_nullable": false,
                "is_zerofill": false,
                "is_unsigned": false,
                "is_auto_increment": false,
                "is_virtual": false,
                "hidden": 1,
                "ordinal_position": 1,
                "char_length": 11,
                "numeric_precision": 10,
                "numeric_scale": 0,
                "numeric_scale_null": false,
                "datetime_precision": 0,
                "datetime_precision_null": 1,
                "has_no_default": true,
                "default_value_null": false,
                "srs_id_null": true,
                "srs_id": 0,
                "default_value": "AAAAAA==",
                "default_value_utf8_null": true,
                "default_value_utf8": "",
                "default_option": "",
                "update_option": "",
                "comment": "",
                "generation_expression": "",
                "generation_expression_utf8": "",
                "options": "interval_count=0;",
                "se_private_data": "table_id=1329;",
                "engine_attribute": "",
                "secondary_engine_attribute": "",
                "column_key": 2,
                "column_type_utf8": "int",
                "elements": [],
                "collation_id": 45,
                "is_explicit_collation": false
            },
            {
                "name": "id",
                "type": 4,
                "is_nullable": false,
                "is_zerofill": false,
                "is_unsigned": false,
                "is_auto_increment": false,
                "is_virtual": false,
                "hidden": 1,
                "ordinal_position": 2,
                "char_length": 11,
                "numeric_precision": 10,
                "numeric_scale": 0,
                "numeric_scale_null": false,
                "datetime_precision": 0,
                "datetime_precision_null": 1,
                "has_no_default": true,
                "default_value_null": false,
                "srs_id_null": true,
                "srs_id": 0,
                "default_value": "AAAAAA==",
                "default_value_utf8_null": true,
                "default_value_utf8": "",
                "default_option": "",
                "update_option": "",
                "comment": "",
                "generation_expression": "",
                "generation_expression_utf8": "",
                "options": "interval_count=0;",
                "se_private_data": "table_id=1329;",
                "engine_attribute": "",
                "secondary_engine_attribute": "",
                "column_key": 2,
                "column_type_utf8": "int",
                "elements": [],
                "collation_id": 45,
                "is_explicit_collation": false
            },
            {
                "name": "price",
                "type": 21,
                "is_nullable": true,
                "is_zerofill": false,
                "is_unsigned": false,
                "is_auto_increment": false,
                "is_virtual": false,
                "hidden": 1,
                "ordinal_position": 3,
                "char_length": 11,
                "numeric_precision": 10,
                "numeric_scale": 0,
                "numeric_scale_null": false,
                "datetime_precision": 0,
                "datetime_precision_null": 1,
                "has_no_default": false,
                "default_value_null": true,
                "srs_id_null": true,
                "srs_id": 0,
                "default_value": "",
                "default_value_utf8_null": true,
                "default_value_utf8": "",
                "default_option": "",
                "update_option": "",
                "comment": "",
                "generation_expression": "",
                "generation_expression_utf8": "",
                "options": "interval_count=0;",
                "se_private_data": "table_id=1329;",
                "engine_attribute": "",
                "secondary_engine_attribute": "",
                "column_key": 1,
                "column_type_utf8": "decimal(10,0)",
                "elements": [],
                "collation_id": 45,
                "is_explicit_collation": false
            },
            {
                "name": "DB_TRX_ID",
                "type": 10,
                "is_nullable": false,
                "is_zerofill": false,
                "is_unsigned": false,
                "is_auto_increment": false,
                "is_virtual": false,
                "hidden": 2,
                "ordinal_position": 4,
                "char_length": 6,
                "numeric_precision": 0,
                "numeric_scale": 0,
                "numeric_scale_null": true,
                "datetime_precision": 0,
                "datetime_precision_null": 1,
                "has_no_default": false,
                "default_value_null": true,
                "srs_id_null": true,
                "srs_id": 0,
                "default_value": "",
                "default_value_utf8_null": true,
                "default_value_utf8": "",
                "default_option": "",
                "update_option": "",
                "comment": "",
                "generation_expression": "",
                "generation_expression_utf8": "",
                "options": "",
                "se_private_data": "table_id=1329;",
                "engine_attribute": "",
                "secondary_engine_attribute": "",
                "column_key": 1,
                "column_type_utf8": "",
                "elements": [],
                "collation_id": 63,
                "is_explicit_collation": false
            },
            {
                "name": "DB_ROLL_PTR",
                "type": 9,
                "is_nullable": false,
                "is_zerofill": false,
                "is_unsigned": false,
                "is_auto_increment": false,
                "is_virtual": false,
                "hidden": 2,
                "ordinal_position": 5,
                "char_length": 7,
                "numeric_precision": 0,
                "numeric_scale": 0,
                "numeric_scale_null": true,
                "datetime_precision": 0,
                "datetime_precision_null": 1,
                "has_no_default": false,
                "default_value_null": true,
                "srs_id_null": true,
                "srs_id": 0,
                "default_value": "",
                "default_value_utf8_null": true,
                "default_value_utf8": "",
                "default_option": "",
                "update_option": "",
                "comment": "",
                "generation_expression": "",
                "generation_expression_utf8": "",
                "options": "",
                "se_private_data": "table_id=1329;",
                "engine_attribute": "",
                "secondary_engine_attribute": "",
                "column_key": 1,
                "column_type_utf8": "",
                "elements": [],
                "collation_id": 63,
                "is_explicit_collation": false
            }
        ],
        "schema_ref": "test",
        "se_private_id": 1329,
        "engine": "InnoDB",
        "last_checked_for_upgrade_version_id": 0,
        "comment": "",
        "se_private_data": "",
        "engine_attribute": "",
        "secondary_engine_attribute": "",
        "row_format": 2,
        "partition_type": 0,
        "partition_expression": "",
        "partition_expression_utf8": "",
        "default_partitioning": 0,
        "subpartition_type": 0,
        "subpartition_expression": "",
        "subpartition_expression_utf8": "",
        "default_subpartitioning": 0,
        "indexes": [
            {
                "name": "PRIMARY",
                "hidden": false,
                "is_generated": false,
                "ordinal_position": 1,
                "comment": "",
                "options": "flags=0;",
                "se_private_data": "id=461;root=4;space_id=267;table_id=1329;trx_id=22127;",
                "type": 1,
                "algorithm": 2,
                "is_algorithm_explicit": false,
                "is_visible": true,
                "engine": "InnoDB",
                "engine_attribute": "",
                "secondary_engine_attribute": "",
                "elements": [
                    {
                        "ordinal_position": 1,
                        "length": 4,
                        "order": 2,
                        "hidden": false,
                        "column_opx": 0
                    },
                    {
                        "ordinal_position": 2,
                        "length": 4,
                        "order": 2,
                        "hidden": false,
                        "column_opx": 1
                    },
                    {
                        "ordinal_position": 3,
                        "length": 4294967295,
                        "order": 2,
                        "hidden": true,
                        "column_opx": 3
                    },
                    {
                        "ordinal_position": 4,
                        "length": 4294967295,
                        "order": 2,
                        "hidden": true,
                        "column_opx": 4
                    },
                    {
                        "ordinal_position": 5,
                        "length": 4294967295,
                        "order": 2,
                        "hidden": true,
                        "column_opx": 2
                    }
                ],
                "tablespace_ref": "test/product"
            }
        ],
        "foreign_keys": [],
        "check_constraints": [],
        "partitions": [],
        "collation_id": 45
    }
}
}
,
{
        "type": 2,
        "id": 272,
        "object":
                {
    "mysqld_version_id": 80039,
    "dd_version": 80023,
    "sdi_version": 80019,
    "dd_object_type": "Tablespace",
    "dd_object": {
        "name": "test/product",
        "comment": "",
        "options": "autoextend_size=0;encryption=N;",
        "se_private_data": "flags=16417;id=267;server_version=80039;space_version=1;state=normal;",
        "engine": "InnoDB",
        "engine_attribute": "",
        "files": [
            {
                "ordinal_position": 1,
                "filename": "./test/product.ibd",
                "se_private_data": "id=267;"
            }
        ]
    }
}
}
]

3. 错误文件

参数: log.error

  • 默认名:机器名.err
  • 建议统一修改成一个固定的名称,例如mysql.err

4. 慢查询日志

将运行超过某个时间阈值的SQL语句记录到文件中。从MySQL5.1开始可以以毫秒为单位记录运行的SQL语句,MySQL5.5开始可以将慢查询保存到表中。在MySQL5.6开始可以更细粒度的记录查询慢查询。
查询当前慢日志开启情况:

sql
mysql> SHOW variables LIKE 'slow%';
+---------------------+----------------------------------------------+
| Variable_name       | Value                                        |
+---------------------+----------------------------------------------+
| slow_launch_time    | 2                                            |
| slow_query_log      | ON                                           |
| slow_query_log_file | /opt/module/mysql-5.7.44/logs/mysql-slow.log |
+---------------------+----------------------------------------------+
3 rows in set (0.01 sec)

可以看到配置的慢查询是超过2s就记录慢查询日志。慢日志相关参数如下:

参数说明版本说明
slow_query_log是否开启慢查询日志
slow_query_log_file慢查询日志文件名
long_query_time指定慢查询阈值5.5 支持毫秒
min_examined_row_limit扫描记录少于该值的SQL不记录到慢查询日志
log-queries-not-using-indexes将没有使用索引的SQL记录到慢查询日志中
log_throttle_queries_not_using_indexes限制每分钟记录没有使用索引SQL语句的次数5.6
log-slow-admin-statment记录管理操作,如ALTER/ANALYZE TABLE
log_output慢查询日志的格式5.5
log_slow_slave_statements在从服务器上开启慢日志查询
log_timestamps写入时区信息5.7

打开slow.log文件,可以看到有一条插入sql比较慢:
Alt text 清理慢查询日志文件:

sh
# 重命名文件后,mysql慢查询日志还是会往老文件slow.log.20250211中写入
[root@hadoop104 mysql-8.0.39]# mv slow.log slow.log.20250211
# mysql释放文件句柄,开始往slow.log中写
mysql>flush slow logs

需要注意的是记录慢查询日志并不会记录sql锁住的情况。

5. 通用日志

可以记录数据库所有相关操作,开启后会性能下降明显。参数为:general_log, 默认文件保存为:机器名.log

sh
mysql> show variables like 'general%';
+------------------+----------------------------------------------+
| Variable_name    | Value                                        |
+------------------+----------------------------------------------+
| general_log      | OFF                                          |
| general_log_file | /opt/module/mysql-8.0.39/data1/hadoop104.log |
+------------------+----------------------------------------------+
2 rows in set (0.00 sec)