Home>Article>Database> MySQL data directory (organized summary)

MySQL data directory (organized summary)

WBOY
WBOY forward
2023-01-24 06:30:01 1429browse

This article brings you relevant knowledge about mysql, which mainly introduces related issues about the mysql data directory. Let’s take a look at it together. I hope it will be helpful to everyone.

MySQL data directory (organized summary)

1. The main directory structure of MySQL 8

After installing MySQL 8, we view the following Directory structure:

1. 1 Storage path of database files

## Storage path of MySQL database files: /var/lib/mysql/

As can be seen from the results, the MySQL data directory on my computer is /var/lib/mysql/.

1. 2 Related command directory

Related command directory: /usr/bin (mysqladmin, mysqlbinlog, mysqldump and other commands) and /usr /sbin.

1. 3 Configuration file directory

Configuration file directory:/usr/share/mysql-8.0 (command and configuration file),/ etc/mysql (such as my.cnf)

[root@atguigu01 ~]# find / -name mysql
mysql> show variables like 'datadir'; +---------------+-----------------+ | Variable_name | Value | +---------------+-----------------+ | datadir | /var/lib/mysql/ | +---------------+-----------------+ 1 row in set (0.04 sec)

2. The relationship between the database and the file system

2. 1 View the default Database

Check which databases are currently on my computer:

You can see that 4 databases belong to MySQL Comes with system database.

mysql

The core database that comes with the MySQL system stores MySQL user account and permission information, as well as some stored procedures and events. Definition information, some log information generated during operation, some help information and time zone information, etc.

information_schema

The database that comes with the MySQL system. This database stores information about all other databases maintained by the MySQL server, such as

Which tables, which views, which triggers, which columns, and which indexes. This information is not real user data, but some descriptive information, sometimes called metadata. Some tables starting with

innodb_sys are provided in the system database information_schema to represent internal system tables.

mysql> SHOW DATABASES;
mysql> USE information_schema; Database changed
mysql> SHOW TABLES LIKE 'innodb_sys%'; +--------------------------------------------+ | Tables_in_information_schema (innodb_sys%) |

performance_schema

The database that comes with the MySQL system. This database mainly saves some status information during the operation of the MySQL server. It can

Used to monitor various performance indicators of the MySQL service. Including statistics on which statements have been executed recently, how long it took at each stage of the execution process, memory usage and other information.

sys

The database that comes with the MySQL system. This database mainly combines information_schema and

# in the form of views.

##Performance_schema is combined to help system administrators and developers monitor the technical performance of MySQL.

2. 2 The representation of the database in the file system

Look at the data directory on my computer Content:

+--------------------------------------------+ | INNODB_SYS_DATAFILES | | INNODB_SYS_VIRTUAL | | INNODB_SYS_INDEXES | | INNODB_SYS_TABLES | | INNODB_SYS_FIELDS | | INNODB_SYS_TABLESPACES | | INNODB_SYS_FOREIGN_COLS | | INNODB_SYS_COLUMNS | | INNODB_SYS_FOREIGN | | INNODB_SYS_TABLESTATS | +--------------------------------------------+ 10 rows in set (0.00 sec)
[root@atguigu01 mysql]# cd /var/lib/mysql [root@atguigu01 mysql]# ll 总用量 189980 -rw-r-----. 1 mysql mysql 56 7 月 28 00 :27 auto.cnf -rw-r-----. 1 mysql mysql 179 7 月 28 00 :27 binlog. -rw-r-----. 1 mysql mysql 820 7 月 28 01 :00 binlog. -rw-r-----. 1 mysql mysql 179 7 月 29 14 :08 binlog. -rw-r-----. 1 mysql mysql 582 7 月 29 16 :47 binlog. -rw-r-----. 1 mysql mysql 179 7 月 29 16 :51 binlog. -rw-r-----. 1 mysql mysql 179 7 月 29 16 :56 binlog. -rw-r-----. 1 mysql mysql 179 7 月 29 17 :37 binlog. -rw-r-----. 1 mysql mysql 24555 7 月 30 00 :28 binlog. -rw-r-----. 1 mysql mysql 179 8 月 1 11 :57 binlog. -rw-r-----. 1 mysql mysql 156 8 月 1 23 :21 binlog. -rw-r-----. 1 mysql mysql 156 8 月 2 09 :25 binlog. -rw-r-----. 1 mysql mysql 1469 8 月 4 01 :40 binlog. -rw-r-----. 1 mysql mysql 156 8 月 6 00 :24 binlog. -rw-r-----. 1 mysql mysql 179 8 月 6 08 :43 binlog. -rw-r-----. 1 mysql mysql 156 8 月 6 10 :56 binlog. -rw-r-----. 1 mysql mysql 240 8 月 6 10 :56 binlog.index -rw-------. 1 mysql mysql 1676 7 月 28 00 :27 ca-key.pem -rw-r--r--. 1 mysql mysql 1112 7 月 28 00 :27 ca.pem -rw-r--r--. 1 mysql mysql 1112 7 月 28 00 :27 client-cert.pem -rw-------. 1 mysql mysql 1676 7 月 28 00 :27 client-key.pem drwxr-x---. 2 mysql mysql 4096 7 月 29 16 :34 dbtest -rw-r-----. 1 mysql mysql 196608 8 月 6 10 :58 #ib_16384_0.dblwr -rw-r-----. 1 mysql mysql 8585216 7 月 28 00 :27 #ib_16384_1.dblwr -rw-r-----. 1 mysql mysql 3486 8 月 6 08 :43 ib_buffer_pool -rw-r-----. 1 mysql mysql 12582912 8 月 6 10 :56 ibdata -rw-r-----. 1 mysql mysql 50331648 8 月 6 10 :58 ib_logfile

There are many files and subdirectories in this data directory. In addition to the information_schema system database, other databases

are in There are corresponding subdirectories under the data directory.

Take my temp database as an example, open it in MySQL 5.7:

Open it in MySQL 8.0:

-rw-r-----. 1 mysql mysql 50331648 7 月 28 00 :27 ib_logfile -rw-r-----. 1 mysql mysql 12582912 8 月 6 10 :56 ibtmp drwxr-x---. 2 mysql mysql 4096 8 月 6 10 :56 #innodb_temp drwxr-x---. 2 mysql mysql 4096 7 月 28 00 :27 mysql -rw-r-----. 1 mysql mysql 26214400 8 月 6 10 :56 mysql.ibd srwxrwxrwx. 1 mysql mysql 0 8 月 6 10 :56 mysql.sock -rw-------. 1 mysql mysql 5 8 月 6 10 :56 mysql.sock.lock drwxr-x---. 2 mysql mysql 4096 7 月 28 00 :27 performance_schema -rw-------. 1 mysql mysql 1680 7 月 28 00 :27 private_key.pem -rw-r--r--. 1 mysql mysql 452 7 月 28 00 :27 public_key.pem -rw-r--r--. 1 mysql mysql 1112 7 月 28 00 :27 server-cert.pem -rw-------. 1 mysql mysql 1680 7 月 28 00 :27 server-key.pem drwxr-x---. 2 mysql mysql 4096 7 月 28 00 :27 sys drwxr-x---. 2 mysql mysql 4096 7 月 29 23 :10 temp -rw-r-----. 1 mysql mysql 16777216 8 月 6 10 :58 undo_ -rw-r-----. 1 mysql mysql 16777216 8 月 6 10 :58 undo_
[root@atguigu02 mysql]# cd ./temp [root@atguigu02 temp]# ll 总用量 1144 -rw-r-----. 1 mysql mysql 8658 8 月 18 11 :32 countries.frm -rw-r-----. 1 mysql mysql 114688 8 月 18 11 :32 countries.ibd -rw-r-----. 1 mysql mysql 61 8 月 18 11 :32 db.opt -rw-r-----. 1 mysql mysql 8716 8 月 18 11 :32 departments.frm -rw-r-----. 1 mysql mysql 147456 8 月 18 11 :32 departments.ibd -rw-r-----. 1 mysql mysql 3017 8 月 18 11 :32 emp_details_view.frm -rw-r-----. 1 mysql mysql 8982 8 月 18 11 :32 employees.frm -rw-r-----. 1 mysql mysql 180224 8 月 18 11 :32 employees.ibd -rw-r-----. 1 mysql mysql 8660 8 月 18 11 :32 job_grades.frm -rw-r-----. 1 mysql mysql 98304 8 月 18 11 :32 job_grades.ibd -rw-r-----. 1 mysql mysql 8736 8 月 18 11 :32 job_history.frm -rw-r-----. 1 mysql mysql 147456 8 月 18 11 :32 job_history.ibd -rw-r-----. 1 mysql mysql 8688 8 月 18 11 :32 jobs.frm -rw-r-----. 1 mysql mysql 114688 8 月 18 11 :32 jobs.ibd -rw-r-----. 1 mysql mysql 8790 8 月 18 11 :32 locations.frm -rw-r-----. 1 mysql mysql 131072 8 月 18 11 :32 locations.ibd -rw-r-----. 1 mysql mysql 8614 8 月 18 11 :32 regions.frm -rw-r-----. 1 mysql mysql 114688 8 月 18 11 :32 regions.ibd

2. 3 Representation of the table in the file system

2. 3. 1 InnoDB storage engine mode

1. Table structure

In order to save the table structure, InnoDB corresponding in the data directory A file specifically used to describe the table structure is created in the database subdirectory. The file name is like this:

For example, we are Create a table named test under the atguigu database:

Then a file named test.frm describing the table structure will be created in the subdirectory corresponding to the atguigu database. The format of .frm files

# is the same across different platforms. This suffix is .frm and is stored in binary format. If we open it directly, it will be garbled

.

2. Data and indexes in the table

① System tablespace

By default, InnoDB will create a file named ibdata1 with a size of 12M in the data directory. This file corresponds to

的系统表空间在文件系统上的表示。怎么才 12 M?注意这个文件是自扩展文件,当不够用的时候它会自

己增加文件大小。

当然,如果你想让系统表空间对应文件系统上多个实际文件,或者仅仅觉得原来的ibdata1这个文件名

难听,那可以在MySQL启动时配置对应的文件路径以及它们的大小,比如我们这样修改一下my.cnf 配置

文件:

② 独立表空间(file-per-table tablespace)

[root@atguigu01 mysql]# cd ./temp [root@atguigu01 temp]# ll 总用量 1080 -rw-r-----. 1 mysql mysql 131072 7 月 29 23 :10 countries.ibd -rw-r-----. 1 mysql mysql 163840 7 月 29 23 :10 departments.ibd -rw-r-----. 1 mysql mysql 196608 7 月 29 23 :10 employees.ibd -rw-r-----. 1 mysql mysql 114688 7 月 29 23 :10 job_grades.ibd -rw-r-----. 1 mysql mysql 163840 7 月 29 23 :10 job_history.ibd -rw-r-----. 1 mysql mysql 131072 7 月 29 23 :10 jobs.ibd -rw-r-----. 1 mysql mysql 147456 7 月 29 23 :10 locations.ibd -rw-r-----. 1 mysql mysql 131072 7 月 29 23 :10 regions.ibd
表名.frm
mysql> USE atguigu; Database changed
mysql> CREATE TABLE test ( -> c1 INT -> ); Query OK, 0 rows affected (0.03 sec)
[server] innodb_data_file_path=data1:512M;data2:512M:autoextend

在MySQL5.6.6以及之后的版本中,InnoDB并不会默认的把各个表的数据存储到系统表空间中,而是为每

一个表建立一个独立表空间,也就是说我们创建了多少个表,就有多少个独立表空间。使用独立表空间来

存储表数据的话,会在该表所属数据库对应的子目录下创建一个表示该独立表空间的文件,文件名和表

名相同,只不过添加了一个.ibd的扩展名而已,所以完整的文件名称长这样:

比如:我们使用了独立表空间去存储atguigu数据库下的test表的话,那么在该表所在数据库对应

的atguigu目录下会为test表创建这两个文件:

其中test.ibd文件就用来存储test表中的数据和索引。

③ 系统表空间与独立表空间的设置

我们可以自己指定使用系统表空间还是独立表空间来存储数据,这个功能由启动参数

innodb_file_per_table控制,比如说我们想刻意将表数据都存储到系统表空间时,可以在启动

MySQL服务器的时候这样配置:

默认情况:

④ 其他类型的表空间

随着MySQL的发展,除了上述两种老牌表空间之外,现在还新提出了一些不同类型的表空间,比如通用

表空间(general tablespace)、临时表空间(temporary tablespace)等。

2. 3. 2 MyISAM存储引擎模式

1. 表结构

在存储表结构方面,MyISAM和InnoDB一样,也是在数据目录下对应的数据库子目录下创建了一个专

门用于描述表结构的文件:

2. 表中数据和索引

在MyISAM中的索引全部都是二级索引,该存储引擎的数据和索引是分开存放的。所以在文件系统中也是

使用不同的文件来存储数据文件和索引文件,同时表数据都存放在对应的数据库子目录下。假如test

表使用MyISAM存储引擎的话,那么在它所在数据库对应的atguigu目录下会为test表创建这三个文

件:

表名.ibd
test.frm test.ibd
[server] innodb_file_per_table=0 # 0:代表使用系统表空间; 1 :代表使用独立表空间
mysql> show variables like 'innodb_file_per_table'; +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | innodb_file_per_table | ON | +-----------------------+-------+ 1 row in set (0.01 sec)
表名.frm

举例:创建一个MyISAM 表,使用ENGINE 选项显式指定引擎。因为InnoDB是默认引擎。

2. 4 小结

举例:数据库a,表b。

1 、如果表b采用InnoDB,data\a中会产生 1 个或者 2 个文件:

b.frm :描述表结构文件,字段长度等

如果采用系统表空间模式的,数据信息和索引信息都存储在ibdata1中

如果采用独立表空间存储模式,data\a中还会产生b.ibd文件(存储数据信息和索引信息)

此外:

① MySQL5.7 中会在data/a的目录下生成db.opt文件用于保存数据库的相关配置。比如:字符集、比较

规则。而MySQL8.0不再提供db.opt文件。

② MySQL8.0中不再单独提供b.frm,而是合并在b.ibd文件中。

2 、如果表b采用MyISAM,data\a中会产生 3 个文件:

MySQL5.7 中:b.frm:描述表结构文件,字段长度等。

MySQL8.0 中 b.xxx.sdi:描述表结构文件,字段长度等

b.MYD(MYData):数据信息文件,存储数据信息(如果采用独立表存储模式)

b.MYI(MYIndex):存放索引信息文件

test.frm 存储表结构 test.MYD 存储数据 (MYData) test.MYI 存储索引 (MYIndex)
CREATE TABLE `student_myisam` ( `id` bigint NOT NULL AUTO_INCREMENT, `name` varchar( 64 ) DEFAULT NULL, `age` int DEFAULT NULL, `sex` varchar( 2 ) DEFAULT NULL, PRIMARY KEY (`id`) )ENGINE=MYISAM AUTO_INCREMENT= 0 DEFAULT CHARSET=utf8mb3;

推荐学习:mysql视频教程

The above is the detailed content of MySQL data directory (organized summary). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete