Detailed explanation of MySQL file storage

Detailed explanation of MySQL file storage

What is a file system

We know that storage engines such as InnoDB and MyIASM store tables on disk (persistent). When we want to read data, these storage engines will read the data from the file system and return it to us. When we want to write data, these storage engines will write the data back to the file system.

Of course, in addition to storing actual data, MySQL also stores a series of other logs, which also belong to the file system.

The address of the file stored on the storage engine

After using the client to establish a connection with the server, you can view the value of this system variable:

show variables like 'datadir';

image.png

Of course, this directory can be modified through the configuration file and specified by ourselves.

What is in the disk file?

What data will MySQL generate during its operation? Of course, it will include user data such as databases, tables, views and triggers that we created. In addition to these user data, MySQL will also create some other additional data for better program operation.

Table information under the database Date directory

Whenever we create a database using the CREATE DATABASE statement, what actually happens on the file system? It's actually very simple. Each database corresponds to a subdirectory under the data directory, or a folder. Whenever we create a new database, MySQL will help us do two things:

  • Create a subdirectory (or folder) with the same name as the database name in the data directory.
  • Create a file named db.opt in the subdirectory with the same name as the database name. This file contains various properties of the database, such as the character set and comparison rules of the database. Let's say we check what databases are currently on my computer:

image.png

You can see that there are currently 5 databases, of which the mysqladv database is our custom one, and the other 4 databases are system databases that come with MySQL. Let's take a look at the contents of the data directory:

image.png

Of course, there are many files and subdirectories under this data directory, but if you look closely, except for the information_schema system database, other databases have corresponding subdirectories under the data directory. This information_schema is quite special, and we will discuss its function later.

How InnoDB stores data

When our InnoDB adds a database, it will add a folder in the log root directory.

image.png

Each folder stores all the corresponding table data. The data in each table is generally divided according to the following two rules:

Definition of table structure

Let's take the index_condition_pushdown table in the study library we created as an example:

image.png

image.png

The table structure is these data, and the corresponding storage file name is:

image.png

The table structure includes the name of the table, how many columns are in the table, the data type of each column, the constraints and indexes, the character set and comparison rules used, etc. All this information is reflected in our table creation statement. In order to save this information, both InnoDB and MyIASM storage engines create a file specifically used to describe the table structure in the corresponding database subdirectory under the data directory. The file name is: table name.frm

Data in the table

Create a file representing the independent tablespace in the subdirectory corresponding to the database to which the table belongs. The file name is the same as the table name, except that a .ibd extension is added. Therefore, the complete file name looks like this: table name.ibd.

image.png

How MyISAM stores table data

Data and indexes in MyISAM are stored separately. Therefore, different files are used in the file system to store data files and index files. (Compared to InnoDB, the ibd file stores data + indexes, and MyISAM splits it again).

image.png

As shown in the figure, we change the storage engine.

image.png

The file directory is changed to the one shown above. It can be simply understood as splitting the InnoDB .ibd file into the following two files.

.MYD represents the data file of the table.

.MYI represents the index file of the table.

Here, we summarize an important difference between InnoDB and MyISAM. That is, InnoDB's index and data are in one folder, while MyISAM stores data files and index files separately in two files.

Log files

During the operation of the server, various logs are generated, such as regular query logs, error logs, binlog logs, redo logs, undo logs, etc. The log files record various types of activities that affect the MySQL database. Common log files include: error log, slow query log, query log, and binary log.

Error Log

The error log file records the startup, operation, and shutdown process of MySQL. When you encounter a problem, you should first check this file to locate the problem. This file not only records all error messages, but also some warning messages or correct messages. Users can view the location of the error log file through the following command:

show variables like 'log_error'\G;

image.png

When MySQL cannot start normally, the first file you must look for should be the error log file, which records the error information.

Slow query log

Slow query logs can help locate SQL statements that may have problems, thereby optimizing SQL statements.

Query log

The query log records information about all requests to the MySQL database, whether or not those requests were executed correctly.

Default file name: hostname.log

image.png

Starting from MySQL 5.1, query log records can be put into the general_log table under the mysql schema

image.png

Binary log (binlog)

image.png

The binary log records all operations that change the MySQL database. If the operation itself does not cause the database to change, the operation may also be written to the binary file. However, operations such as select and show are not included (because these operations do not modify the data itself)

Several functions of binlog

Recovery

Recovery of some data requires binary logs.

copy

The principle is similar to recovery. By copying and executing binary logs, a remote MySQL database (generally called slave or standby) is synchronized with a MySQL database (generally called master or primary) in real time.

Audit (a little unpopular, db is responsible for it)

Users can use the information in the binary log to audit and determine whether there are any injection attacks on the database.

Summarize

This is the end of this article about MySQL file storage. For more relevant MySQL file storage content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to view the storage location of MySQL data files

<<:  5 VueUse libraries that can speed up development (summary)

>>:  How to install docker and portainer in kali

Recommend

IE8 compatibility notes I encountered

1. IE8's getElementById only supports id, not ...

Summary of MySQL logical backup and recovery testing

Table of contents 1. What kind of backup is a dat...

Turn off the AutoComplete function in the input box

Now we can use an attribute of input called autoco...

Example code and method of storing arrays in mysql

In many cases, arrays are often used when writing...

About the problem of writing plugins for mounting DOM in vue3

Compared with vue2, vue3 has an additional concep...

Detailed steps for Python script self-start and scheduled start under Linux

1. Python automatically runs at startup Suppose t...

Analysis of the process of simply deploying nginx in Docker container

1. Deploy nginx service in container The centos:7...

Front-end JavaScript housekeeper package.json

Table of contents 1. Required attributes 1. name ...

Whitespace processing in HTML/CSS and how to preserve whitespace in the page

Whitespace rules in HTML In HTML, multiple spaces...

Detailed explanation of Vue's list rendering

Table of contents 1. v-for: traverse array conten...

Use pure CSS to achieve switch effect

First is the idea We use the <input type="...

Detailed explanation of scheduled tasks for ordinary users in Linux

Preface Ordinary users define crontab scheduled t...

Detailed explanation of server-id example in MySQL master-slave synchronization

Preface When we build a MySQL cluster, we natural...

Nginx Location Configuration Tutorial from Scratch

Basics The matching order of location is "ma...

Springboot+VUE to realize login and registration

This article example shares the specific code of ...