The relationship between Mysql and the file system

I am participating in the "Nuggets · Sailing Program"

The relationship between Mysql and the file system

In Linux, the file system is used to manage the disk, while the commonly used storage engines of Mysql, such as InnoDB and MyISAM, use file storage, which is naturally linked to the file system. So where does Mysql use the file system? Let's talk

Mysql data directory

Mysql system startup file (default my.cnf) may specify a global system variable datadir of Mysql, this variable can specify the data storage directory of Mysql, as follows

image-20220906193958788.png

Or get it in the form of a query variable (all the demo screenshots below are from this directory)

mysql> show variables like '%datadir%';
+---------------+-------------------------------+
| Variable_name | Value                         |
+---------------+-------------------------------+
| datadir       | /usr/local/mysql-5.7.26/data/ |
+---------------+-------------------------------+
1 row in set (0.00 sec)

复制代码

Data directory related structure

database storage structure

Each time a database is created, a folder with the same name as the database will be created under the predefined datadir path.

mysql> create database test_data;
Query OK, 1 row affected (0.00 sec)

复制代码

image-20220906195252088.png

After the test_data folder is created, a db.opt file will be created in the folder, which is used to store database attributes such as character set and comparison rules.

image-20220906195330216.png

One thing we need to pay attention to are four databases that come with the system: mysql (stores information such as Mysql user permissions), information_schema (description information of other databases), performance_schema (stores Mysql runtime status, etc.), sys (server performance view), A folder will be created except for information_schema.

table structure storage

For example, create a new table for the test_data database created above

CREATE TABLE test_table (
  name varchar(100) DEFAULT NULL
) ENGINE=InnoDB

复制代码

Then Mysql will create a new file test_table.frm with the same table name on the database folder with the same name, that is, test_data. This file is used to describe the fields, indexes, constraints, etc. of the table.

image-20220906200615556.png

table data storage

Table data is stored in different storage engines, and the storage files are different. Take the commonly used InnoDB and MyISAM as examples.

InnoDB storage engine table data storage

The InnoDB storage engine manages storage space in units of pages. The size of a data page is 16k. InnoDB introduces the concept of table space for the convenience of management. A table space may contain multiple data pages. Of course, the table space is divided into Several are as follows.

system tablespace

From Mysql version 5.5.7 to 5.6.6, the table data is put into the system table space by default. By default, the location of the data directory will automatically generate a file named ibdata1. The file size is 12M. as follows

image-20220906224307504.png

The size of this file is not fixed and can grow automatically, which means that the file can automatically expand in size when it cannot be stored.

independent tablespace

After Mysql version 5.6.6, it is no longer stored in the system table space, but is stored in an independent table space. Independence means that the data between tables and tables are independent. Mysql will create an independent space for each table. There are as many independent spaces as there are tables, so when we execute the table creation statement

-- 默认InnoDB存储引擎
CREATE TABLE test_table (
  name varchar(100) DEFAULT NULL
) ENGINE=InnoDB

复制代码

A new idb file with the same table name will be created in the directory with the same name as the current database.

image-20220906225024509.png

As for whether the user wants to choose the system table space or the independent table space, the user can decide by himself. innodb_file_per_tableIt can be achieved by modifying the system variables. If it is 0, it means using the system table space, and if it is 1, it means using the independent table space. What needs to be noted here is to modify the system parameters. It only takes effect for newly created tables. Tables before modifying system parameters need to be modified manually.

MyISAM storage engine table data storage

The biggest difference between the MyISAM storage engine and the InnoDB storage engine on the file system is that MyISAM separates the index and the data file. InnoDB emphasizes that the index is both the data, which is why the table of the MyISAM storage engine is described by three files.

  • TableName.frm: Describes the table structure, indexes, constraints, etc.

  • TableName.MYD: Stores table data.

  • tablename.MYI: Storage table index.

Create a test table as follows

CREATE TABLE `test_table_myisam` (
  `name` varchar(100) DEFAULT NULL
) ENGINE=MyISAM

复制代码

image-20220906230410960.png

special view storage

create view view_test as select * from test_table;

复制代码

Because a view is essentially a virtual table, which is just an alias for a query statement, the data of the view is not saved during storage, but only the table structure of the view, so creating a view will only create a frm file with the same name as the view.

image-20220906201709888.png

Guess you like

Origin juejin.im/post/7147657045678751752